From d7eb6a70f730a8af0a7af4b7cce25385a5872cd1 Mon Sep 17 00:00:00 2001 From: cangtianhuang Date: Tue, 19 Aug 2025 17:58:31 +0800 Subject: [PATCH 01/27] fix compatible in index_select, logical_* --- paddle/phi/ops/yaml/ops.yaml | 20 +++- python/paddle/_paddle_docs.py | 186 ++++++++++++++++++++++++++++++++- python/paddle/tensor/logic.py | 138 +----------------------- python/paddle/tensor/search.py | 79 +------------- 4 files changed, 205 insertions(+), 218 deletions(-) diff --git a/paddle/phi/ops/yaml/ops.yaml b/paddle/phi/ops/yaml/ops.yaml index 88d8f32949f10d..91d944581b16a8 100644 --- a/paddle/phi/ops/yaml/ops.yaml +++ b/paddle/phi/ops/yaml/ops.yaml @@ -240,7 +240,7 @@ - op : amax args : (Tensor x, int64_t[] axis={}, bool keepdim=false) python_api : - name : [paddle.amax,paddle.Tensor.amax] + name : [paddle.amax, paddle.Tensor.amax] args_alias: use_default_mapping : True output : Tensor(out) @@ -254,7 +254,7 @@ - op : amin args : (Tensor x, int64_t[] axis={}, bool keepdim=false) python_api : - name : [paddle.amin,paddle.Tensor.amin] + name : [paddle.amin, paddle.Tensor.amin] args_alias : use_default_mapping : True output : Tensor(out) @@ -2872,6 +2872,10 @@ - op : index_select args : (Tensor x, Tensor index, int axis = 0) + python_api: + name : [paddle.index_select, paddle.Tensor.index_select] + args_alias: + use_default_mapping : True output : Tensor(out) infer_meta : func : IndexSelectInferMeta @@ -3232,6 +3236,10 @@ - op : logical_and args : (Tensor x, Tensor y) + python_api: + name : [paddle.logical_and, paddle.Tensor.logical_and] + args_alias: + use_default_mapping : True output : Tensor(out) infer_meta : func : LogicalBinaryInferMeta @@ -3246,6 +3254,10 @@ - op : logical_not args : (Tensor x) + python_api: + name : [paddle.logical_not, paddle.Tensor.logical_not] + args_alias: + use_default_mapping : True output : Tensor(out) infer_meta : func : LogicalNotInferMeta @@ -3260,6 +3272,10 @@ - op : logical_or args : (Tensor x, Tensor y) + python_api: + name : [paddle.logical_or, paddle.Tensor.logical_or] + args_alias: + use_default_mapping : True output : Tensor(out) infer_meta : func : LogicalBinaryInferMeta diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index 8cc0d2f2fb25ae..d87243324ce4ed 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -316,7 +316,7 @@ def amin( [0.50000000, 0.33333333]], [[0.50000000, 0.33333333], [0. , 0. ]]]) - """, +""", """ def amax( x: Tensor, @@ -326,3 +326,187 @@ def amax( ) -> Tensor """, ) + +add_doc_and_signature( + "index_select", + """ + Returns a new tensor which indexes the ``input`` tensor along dimension ``axis`` using + the entries in ``index`` which is a Tensor. The returned tensor has the same number + of dimensions as the original ``x`` tensor. The dim-th dimension has the same + size as the length of ``index``; other dimensions have the same size as in the ``x`` tensor. + + Args: + x (Tensor): The input Tensor to be operated. The data of ``x`` can be one of float16, float32, float64, int32, int64, complex64 and complex128. + index (Tensor): The 1-D Tensor containing the indices to index. The data type of ``index`` must be int32 or int64. + axis (int, optional): The dimension in which we index. Default: if None, the ``axis`` is 0. + name (str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + + Keyword Args: + out (Tensor|optional): The output tensor. + + Returns: + Tensor, A Tensor with same data type as ``x``. + + Examples: + .. code-block:: python + + >>> import paddle + + >>> x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0], + ... [5.0, 6.0, 7.0, 8.0], + ... [9.0, 10.0, 11.0, 12.0]]) + >>> index = paddle.to_tensor([0, 1, 1], dtype='int32') + >>> out_z1 = paddle.index_select(x=x, index=index) + >>> print(out_z1.numpy()) + [[1. 2. 3. 4.] + [5. 6. 7. 8.] + [5. 6. 7. 8.]] + >>> out_z2 = paddle.index_select(x=x, index=index, axis=1) + >>> print(out_z2.numpy()) + [[ 1. 2. 2.] + [ 5. 6. 6.] + [ 9. 10. 10.]] +""", + """ +def index_select( + x: Tensor, index: Tensor, axis: int = 0, name: str | None = None +) -> Tensor +""", +) + +add_doc_and_signature( + "torch.logical_and", + """ + Compute element-wise logical AND on ``x`` and ``y``, and return ``out``. ``out`` is N-dim boolean ``Tensor``. + Each element of ``out`` is calculated by + + .. math:: + + out = x \&\& y + + Note: + ``paddle.logical_and`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ . + + .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor + + Args: + x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, bfloat16, float16, float32, float64, complex64, complex128. + y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, bfloat16, float16, float32, float64, complex64, complex128. + out(Tensor|None, optional): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + + Keyword Args: + out (Tensor|optional): The output tensor. + + Returns: + N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``. + + Examples: + .. code-block:: python + + >>> import paddle + + >>> x = paddle.to_tensor([True]) + >>> y = paddle.to_tensor([True, False, True, False]) + >>> res = paddle.logical_and(x, y) + >>> print(res) + Tensor(shape=[4], dtype=bool, place=Place(cpu), stop_gradient=True, + [True , False, True , False]) +""", + """ +def logical_and( + x: Tensor, y: Tensor, out: Tensor | None = None, name: str | None = None +) -> Tensor +""", +) + +add_doc_and_signature( + "logical_or", + """ + ``logical_or`` operator computes element-wise logical OR on ``x`` and ``y``, and returns ``out``. ``out`` is N-dim boolean ``Tensor``. + Each element of ``out`` is calculated by + + .. math:: + + out = x || y + + Note: + ``paddle.logical_or`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ . + + .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor + + Args: + x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, bfloat16, float16, float32, float64, complex64, complex128. + y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, bfloat16, float16, float32, float64, complex64, complex128. + out(Tensor|None, optional): The ``Variable`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + + Keyword Args: + out (Tensor|optional): The output tensor. + + Returns: + N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``. + + Examples: + .. code-block:: python + + >>> import paddle + + >>> x = paddle.to_tensor([True, False], dtype="bool").reshape([2, 1]) + >>> y = paddle.to_tensor([True, False, True, False], dtype="bool").reshape([2, 2]) + >>> res = paddle.logical_or(x, y) + >>> print(res) + Tensor(shape=[2, 2], dtype=bool, place=Place(cpu), stop_gradient=True, + [[True , True ], + [True , False]]) +""", + """ +def logical_or( + x: Tensor, y: Tensor, out: Tensor | None = None, name: str | None = None +) -> Tensor +""", +) + +add_doc_and_signature( + "logical_not", + """ + ``logical_not`` operator computes element-wise logical NOT on ``x``, and returns ``out``. ``out`` is N-dim boolean ``Variable``. + Each element of ``out`` is calculated by + + .. math:: + + out = !x + + Note: + ``paddle.logical_not`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ . + + .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor + + Args: + x(Tensor): Operand of logical_not operator. Must be a Tensor of type bool, int8, int16, in32, in64, bfloat16, float16, float32, or float64, complex64, complex128. + out(Tensor|None): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor` will be created to save the output. + name(str|None, optional): The default value is None. Normally there is no need for users to set this property. For more information, please refer to :ref:`api_guide_Name`. + + Keyword Args: + out (Tensor|optional): The output tensor. + + Returns: + N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``. + + Examples: + .. code-block:: python + + >>> import paddle + + >>> x = paddle.to_tensor([True, False, True, False]) + >>> res = paddle.logical_not(x) + >>> print(res) + Tensor(shape=[4], dtype=bool, place=Place(cpu), stop_gradient=True, + [False, True , False, True ]) +""", + """ +def logical_not( + x: Tensor, out: Tensor | None = None, name: str | None = None +) -> Tensor +""", +) diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index 6e02ce0d548385..fd8812c460540c 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -20,6 +20,7 @@ import paddle from paddle import _C_ops +from paddle._C_ops import logical_and, logical_not, logical_or from paddle.tensor.creation import full from paddle.tensor.math import broadcast_shape from paddle.utils.decorator_utils import ParamAliasDecorator, param_two_alias @@ -112,53 +113,6 @@ def _logical_op( return out -def logical_and( - x: Tensor, y: Tensor, out: Tensor | None = None, name: str | None = None -) -> Tensor: - r""" - - Compute element-wise logical AND on ``x`` and ``y``, and return ``out``. ``out`` is N-dim boolean ``Tensor``. - Each element of ``out`` is calculated by - - .. math:: - - out = x \&\& y - - Note: - ``paddle.logical_and`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ . - - .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor - - Args: - x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, bfloat16, float16, float32, float64, complex64, complex128. - y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, bfloat16, float16, float32, float64, complex64, complex128. - out(Tensor|None, optional): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output. - name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. - - Returns: - N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``. - - Examples: - .. code-block:: python - - >>> import paddle - - >>> x = paddle.to_tensor([True]) - >>> y = paddle.to_tensor([True, False, True, False]) - >>> res = paddle.logical_and(x, y) - >>> print(res) - Tensor(shape=[4], dtype=bool, place=Place(cpu), stop_gradient=True, - [True , False, True , False]) - - """ - if in_dynamic_or_pir_mode(): - return _C_ops.logical_and(x, y) - - return _logical_op( - op_name="logical_and", x=x, y=y, name=name, out=out, binary_op=True - ) - - @inplace_apis_in_dygraph_only def logical_and_(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: r""" @@ -174,52 +128,6 @@ def logical_and_(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: return _C_ops.logical_and_(x, y) -def logical_or( - x: Tensor, y: Tensor, out: Tensor | None = None, name: str | None = None -) -> Tensor: - """ - - ``logical_or`` operator computes element-wise logical OR on ``x`` and ``y``, and returns ``out``. ``out`` is N-dim boolean ``Tensor``. - Each element of ``out`` is calculated by - - .. math:: - - out = x || y - - Note: - ``paddle.logical_or`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ . - - .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor - - Args: - x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, bfloat16, float16, float32, float64, complex64, complex128. - y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, bfloat16, float16, float32, float64, complex64, complex128. - out(Tensor|None, optional): The ``Variable`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output. - name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. - - Returns: - N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``. - - Examples: - .. code-block:: python - - >>> import paddle - - >>> x = paddle.to_tensor([True, False], dtype="bool").reshape([2, 1]) - >>> y = paddle.to_tensor([True, False, True, False], dtype="bool").reshape([2, 2]) - >>> res = paddle.logical_or(x, y) - >>> print(res) - Tensor(shape=[2, 2], dtype=bool, place=Place(cpu), stop_gradient=True, - [[True , True ], - [True , False]]) - """ - if in_dynamic_or_pir_mode(): - return _C_ops.logical_or(x, y) - return _logical_op( - op_name="logical_or", x=x, y=y, name=name, out=out, binary_op=True - ) - - @inplace_apis_in_dygraph_only def logical_or_(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: r""" @@ -297,50 +205,6 @@ def logical_xor_(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: return _C_ops.logical_xor_(x, y) -def logical_not( - x: Tensor, out: Tensor | None = None, name: str | None = None -) -> Tensor: - """ - - ``logical_not`` operator computes element-wise logical NOT on ``x``, and returns ``out``. ``out`` is N-dim boolean ``Variable``. - Each element of ``out`` is calculated by - - .. math:: - - out = !x - - Note: - ``paddle.logical_not`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ . - - .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor - - Args: - - x(Tensor): Operand of logical_not operator. Must be a Tensor of type bool, int8, int16, in32, in64, bfloat16, float16, float32, or float64, complex64, complex128. - out(Tensor|None): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor` will be created to save the output. - name(str|None, optional): The default value is None. Normally there is no need for users to set this property. For more information, please refer to :ref:`api_guide_Name`. - - Returns: - N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``. - - Examples: - .. code-block:: python - - >>> import paddle - - >>> x = paddle.to_tensor([True, False, True, False]) - >>> res = paddle.logical_not(x) - >>> print(res) - Tensor(shape=[4], dtype=bool, place=Place(cpu), stop_gradient=True, - [False, True , False, True ]) - """ - if in_dynamic_or_pir_mode(): - return _C_ops.logical_not(x) - return _logical_op( - op_name="logical_not", x=x, y=None, name=name, out=out, binary_op=False - ) - - @inplace_apis_in_dygraph_only def logical_not_(x: Tensor, name: str | None = None) -> Tensor: r""" diff --git a/python/paddle/tensor/search.py b/python/paddle/tensor/search.py index 5a40997626ba7b..6a2182c01b18fc 100755 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -21,6 +21,7 @@ import paddle from paddle import _C_ops +from paddle._C_ops import index_select from paddle.common_ops_import import VarDesc, Variable from paddle.utils.decorator_utils import ParamAliasDecorator, param_one_alias from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only @@ -377,84 +378,6 @@ def argmin( return out -def index_select( - x: Tensor, index: Tensor, axis: int = 0, name: str | None = None -) -> Tensor: - """ - - Returns a new tensor which indexes the ``input`` tensor along dimension ``axis`` using - the entries in ``index`` which is a Tensor. The returned tensor has the same number - of dimensions as the original ``x`` tensor. The dim-th dimension has the same - size as the length of ``index``; other dimensions have the same size as in the ``x`` tensor. - - Args: - x (Tensor): The input Tensor to be operated. The data of ``x`` can be one of float16, float32, float64, int32, int64, complex64 and complex128. - index (Tensor): The 1-D Tensor containing the indices to index. The data type of ``index`` must be int32 or int64. - axis (int, optional): The dimension in which we index. Default: if None, the ``axis`` is 0. - name (str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. - - Returns: - Tensor, A Tensor with same data type as ``x``. - - Examples: - .. code-block:: python - - >>> import paddle - - >>> x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0], - ... [5.0, 6.0, 7.0, 8.0], - ... [9.0, 10.0, 11.0, 12.0]]) - >>> index = paddle.to_tensor([0, 1, 1], dtype='int32') - >>> out_z1 = paddle.index_select(x=x, index=index) - >>> print(out_z1.numpy()) - [[1. 2. 3. 4.] - [5. 6. 7. 8.] - [5. 6. 7. 8.]] - >>> out_z2 = paddle.index_select(x=x, index=index, axis=1) - >>> print(out_z2.numpy()) - [[ 1. 2. 2.] - [ 5. 6. 6.] - [ 9. 10. 10.]] - """ - - if in_dynamic_or_pir_mode(): - return _C_ops.index_select(x, index, axis) - else: - helper = LayerHelper("index_select", **locals()) - check_variable_and_dtype( - x, - 'x', - [ - 'bool', - 'uint16', - 'float16', - 'float32', - 'float64', - 'int32', - 'int64', - 'complex64', - 'complex128', - ], - 'paddle.tensor.search.index_select', - ) - check_variable_and_dtype( - index, - 'index', - ['int32', 'int64'], - 'paddle.tensor.search.index_select', - ) - - out = helper.create_variable_for_type_inference(x.dtype) - - helper.append_op( - type='index_select', - inputs={'X': x, 'Index': index}, - outputs={'Out': out}, - attrs={'dim': axis}, - ) - return out - - @overload def nonzero(x: Tensor, as_tuple: Literal[False] = ...) -> Tensor: ... From c85eaa1ac516d63cfe7a72924e86c44b834d3b63 Mon Sep 17 00:00:00 2001 From: cangtianhuang Date: Tue, 19 Aug 2025 20:42:44 +0800 Subject: [PATCH 02/27] fix format --- python/paddle/_paddle_docs.py | 2 +- python/paddle/tensor/logic.py | 2 +- python/paddle/tensor/search.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index d87243324ce4ed..731e6eb580eeee 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -376,7 +376,7 @@ def index_select( add_doc_and_signature( "torch.logical_and", - """ + r""" Compute element-wise logical AND on ``x`` and ``y``, and return ``out``. ``out`` is N-dim boolean ``Tensor``. Each element of ``out`` is calculated by diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index fd8812c460540c..23bb399012ed0a 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -20,7 +20,7 @@ import paddle from paddle import _C_ops -from paddle._C_ops import logical_and, logical_not, logical_or +from paddle._C_ops import logical_and, logical_not, logical_or # noqa: F401 from paddle.tensor.creation import full from paddle.tensor.math import broadcast_shape from paddle.utils.decorator_utils import ParamAliasDecorator, param_two_alias diff --git a/python/paddle/tensor/search.py b/python/paddle/tensor/search.py index 6a2182c01b18fc..db7ae9b1fbf6df 100755 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -21,7 +21,7 @@ import paddle from paddle import _C_ops -from paddle._C_ops import index_select +from paddle._C_ops import index_select # noqa: F401 from paddle.common_ops_import import VarDesc, Variable from paddle.utils.decorator_utils import ParamAliasDecorator, param_one_alias from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only From b4e13cabf5431d5edfa0a4976a6356f29e58e3a3 Mon Sep 17 00:00:00 2001 From: cangtianhuang Date: Wed, 20 Aug 2025 11:16:11 +0800 Subject: [PATCH 03/27] refine --- python/paddle/_paddle_docs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index 731e6eb580eeee..3b642c843dd1a4 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -375,7 +375,7 @@ def index_select( ) add_doc_and_signature( - "torch.logical_and", + "logical_and", r""" Compute element-wise logical AND on ``x`` and ``y``, and return ``out``. ``out`` is N-dim boolean ``Tensor``. Each element of ``out`` is calculated by From be756dd1864c8246829ee34d0dd6adf9743b9df1 Mon Sep 17 00:00:00 2001 From: cangtianhuang Date: Wed, 20 Aug 2025 16:08:48 +0800 Subject: [PATCH 04/27] fix tests --- paddle/phi/ops/yaml/ops.yaml | 4 + python/paddle/_paddle_docs.py | 53 +- python/paddle/tensor/logic.py | 54 +- .../test_multi_precision_fp16_train.py | 43 - test/deprecated/legacy_test/CMakeLists.txt | 15 - .../legacy_test/test_bfgs_deprecated.py | 175 -- .../legacy_test/test_desc_clone_deprecated.py | 302 ---- .../legacy_test/test_ema_deprecated.py | 102 -- .../legacy_test/test_ema_fleet_deprecated.py | 115 -- ..._get_inputs_outputs_in_block_deprecated.py | 81 - .../legacy_test/test_layers_deprecated.py | 1466 ----------------- .../legacy_test/test_lbfgs_deprecated.py | 169 -- ...test_learning_rate_scheduler_deprecated.py | 620 ------- .../test_math_op_patch_deprecated.py | 58 - ...st_optimizer_in_control_flow_deprecated.py | 250 --- .../test_program_code_deprecated.py | 74 - .../test_program_prune_backward_deprecated.py | 592 ------- .../test_static_pylayer_deprecated.py | 751 --------- .../legacy_test/test_switch_deprecated.py | 108 -- test/ir/pir/test_special_op_translator.py | 29 - test/legacy_test/test_index_select_op.py | 94 ++ test/legacy_test/test_logical_op.py | 146 ++ test/legacy_test/test_while_loop_op.py | 50 - tools/parallel_UT_rule.py | 5 - tools/static_mode_white_list.py | 3 - tools/windows/run_unittests.sh | 2 - 26 files changed, 294 insertions(+), 5067 deletions(-) delete mode 100644 test/deprecated/legacy_test/test_bfgs_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_desc_clone_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_ema_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_ema_fleet_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_get_inputs_outputs_in_block_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_layers_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_lbfgs_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_learning_rate_scheduler_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_math_op_patch_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_optimizer_in_control_flow_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_program_code_deprecated.py delete mode 100755 test/deprecated/legacy_test/test_program_prune_backward_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_static_pylayer_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_switch_deprecated.py diff --git a/paddle/phi/ops/yaml/ops.yaml b/paddle/phi/ops/yaml/ops.yaml index 91d944581b16a8..c774b91a917fe0 100644 --- a/paddle/phi/ops/yaml/ops.yaml +++ b/paddle/phi/ops/yaml/ops.yaml @@ -3290,6 +3290,10 @@ - op : logical_xor args : (Tensor x, Tensor y) + python_api: + name : [paddle.logical_xor, paddle.Tensor.logical_xor] + args_alias: + use_default_mapping : True output : Tensor(out) infer_meta : func : LogicalBinaryInferMeta diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index 3b642c843dd1a4..e9f5a241a9018c 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -395,9 +395,6 @@ def index_select( out(Tensor|None, optional): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output. name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. - Keyword Args: - out (Tensor|optional): The output tensor. - Returns: N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``. @@ -441,9 +438,6 @@ def logical_and( out(Tensor|None, optional): The ``Variable`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output. name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. - Keyword Args: - out (Tensor|optional): The output tensor. - Returns: N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``. @@ -487,9 +481,6 @@ def logical_or( out(Tensor|None): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor` will be created to save the output. name(str|None, optional): The default value is None. Normally there is no need for users to set this property. For more information, please refer to :ref:`api_guide_Name`. - Keyword Args: - out (Tensor|optional): The output tensor. - Returns: N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``. @@ -510,3 +501,47 @@ def logical_not( ) -> Tensor """, ) + +add_doc_and_signature( + "logical_xor", + r""" + ``logical_xor`` operator computes element-wise logical XOR on ``x`` and ``y``, and returns ``out``. ``out`` is N-dim boolean ``Tensor``. + Each element of ``out`` is calculated by + + .. math:: + + out = (x || y) \&\& !(x \&\& y) + + Note: + ``paddle.logical_xor`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ . + + .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor + + Args: + x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, bfloat16, float16, float32, float64, complex64, complex128. + y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, bfloat16, float16, float32, float64, complex64, complex128. + out(Tensor|None, optional): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + + Returns: + N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``. + + Examples: + .. code-block:: python + + >>> import paddle + + >>> x = paddle.to_tensor([True, False], dtype="bool").reshape([2, 1]) + >>> y = paddle.to_tensor([True, False, True, False], dtype="bool").reshape([2, 2]) + >>> res = paddle.logical_xor(x, y) + >>> print(res) + Tensor(shape=[2, 2], dtype=bool, place=Place(cpu), stop_gradient=True, + [[False, True ], + [True , False]]) +""", + """ +def logical_xor( + x: Tensor, y: Tensor, out: Tensor | None = None, name: str | None = None +) -> Tensor +""", +) diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index 23bb399012ed0a..13cca6c49a6d1f 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -20,7 +20,12 @@ import paddle from paddle import _C_ops -from paddle._C_ops import logical_and, logical_not, logical_or # noqa: F401 +from paddle._C_ops import ( # noqa: F401 + logical_and, + logical_not, + logical_or, + logical_xor, +) from paddle.tensor.creation import full from paddle.tensor.math import broadcast_shape from paddle.utils.decorator_utils import ParamAliasDecorator, param_two_alias @@ -143,53 +148,6 @@ def logical_or_(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: return _C_ops.logical_or_(x, y) -def logical_xor( - x: Tensor, y: Tensor, out: Tensor | None = None, name: str | None = None -) -> Tensor: - r""" - - ``logical_xor`` operator computes element-wise logical XOR on ``x`` and ``y``, and returns ``out``. ``out`` is N-dim boolean ``Tensor``. - Each element of ``out`` is calculated by - - .. math:: - - out = (x || y) \&\& !(x \&\& y) - - Note: - ``paddle.logical_xor`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ . - - .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor - - Args: - x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, bfloat16, float16, float32, float64, complex64, complex128. - y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, bfloat16, float16, float32, float64, complex64, complex128. - out(Tensor|None, optional): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output. - name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. - - Returns: - N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``. - - Examples: - .. code-block:: python - - >>> import paddle - - >>> x = paddle.to_tensor([True, False], dtype="bool").reshape([2, 1]) - >>> y = paddle.to_tensor([True, False, True, False], dtype="bool").reshape([2, 2]) - >>> res = paddle.logical_xor(x, y) - >>> print(res) - Tensor(shape=[2, 2], dtype=bool, place=Place(cpu), stop_gradient=True, - [[False, True ], - [True , False]]) - """ - if in_dynamic_or_pir_mode(): - return _C_ops.logical_xor(x, y) - - return _logical_op( - op_name="logical_xor", x=x, y=y, name=name, out=out, binary_op=True - ) - - @inplace_apis_in_dygraph_only def logical_xor_(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: r""" diff --git a/test/contrib/test_multi_precision_fp16_train.py b/test/contrib/test_multi_precision_fp16_train.py index 945acdb0298db8..26fd48ecd76dc6 100644 --- a/test/contrib/test_multi_precision_fp16_train.py +++ b/test/contrib/test_multi_precision_fp16_train.py @@ -21,7 +21,6 @@ from paddle import base from paddle.io import Dataset from paddle.nn import Layer -from paddle.static.amp.fp16_utils import cast_model_to_fp16 paddle.enable_static() @@ -313,47 +312,5 @@ def scope_prog_guard(self): yield -class TestAmpWithNonIterableDataLoader(unittest.TestCase): - def decorate_with_data_loader(self): - main_prog = paddle.static.Program() - start_prog = paddle.static.Program() - with ( - paddle.static.program_guard(main_prog, start_prog), - paddle.base.unique_name.guard(), - ): - image = paddle.static.data( - name='image', shape=[-1, 3, 224, 224], dtype='float32' - ) - label = paddle.static.data( - name='label', shape=[-1, 1], dtype='int64' - ) - zero_var = paddle.tensor.fill_constant( - shape=[1], dtype='int64', value=0 - ) - one_var = paddle.tensor.fill_constant( - shape=[1], dtype='int64', value=1 - ) - label_val = paddle.static.nn.cond( - label != zero_var, lambda: zero_var, lambda: one_var - ) - paddle.assign(label_val, output=label) - net = resnet_cifar10(image) - logits = paddle.static.nn.fc(x=net, size=10, activation="softmax") - - block = main_prog.global_block() - for op in block.ops: - if op.type == "mul": - op._set_attr('in_dtype', base.core.VarDesc.VarType.FP32) - op._set_attr('out_dtype', base.core.VarDesc.VarType.FP32) - op._set_attr('dtype', base.core.VarDesc.VarType.FP32) - - cast_model_to_fp16(main_prog, use_fp16_guard=False) - - def test_non_iterable_dataloader(self): - if base.core.is_compiled_with_cuda(): - with paddle.pir_utils.OldIrGuard(): - self.decorate_with_data_loader() - - if __name__ == '__main__': unittest.main() diff --git a/test/deprecated/legacy_test/CMakeLists.txt b/test/deprecated/legacy_test/CMakeLists.txt index 031b78132e9e58..9875d65bc60853 100644 --- a/test/deprecated/legacy_test/CMakeLists.txt +++ b/test/deprecated/legacy_test/CMakeLists.txt @@ -186,10 +186,6 @@ list(REMOVE_ITEM TEST_OPS decorator_helper) # decorator_helper is a helper python file, not a test if(APPLE) - if(NOT WITH_DISTRIBUTE) - list(REMOVE_ITEM TEST_OPS test_desc_clone_deprecated) - list(REMOVE_ITEM TEST_OPS test_program_code_deprecated) - endif() message( WARNING "These tests has been disabled in OSX before being fixed:\n test_fuse_elewise_add_act_pass_deprecated \n test_dist_se_resnext_*" @@ -389,7 +385,6 @@ function(parallel_bash_test_modules TARGET_NAME) endfunction() list(REMOVE_ITEM TEST_OPS test_feed_data_check_shape_type_deprecated) -list(REMOVE_ITEM TEST_OPS test_layers_deprecated) list(REMOVE_ITEM TEST_OPS test_basic_gru_api) list(REMOVE_ITEM TEST_OPS test_basic_gru_unit_op) list(REMOVE_ITEM TEST_OPS test_basic_lstm_api) @@ -577,12 +572,6 @@ py_test_modules( FLAGS_cudnn_batchnorm_spatial_persistent=1 FLAGS_conv_workspace_size_limit=1000) -if(NOT WIN32) - # TODO: fix these unittests failure on Windows - py_test_modules(test_layers_deprecated MODULES test_layers_deprecated ENVS - FLAGS_cudnn_deterministic=1) -endif() - set_tests_properties( test_dataloader_keep_order_deprecated test_dataloader_unkeep_order_deprecated PROPERTIES LABELS "RUN_TYPE=DIST") @@ -590,7 +579,6 @@ set_tests_properties( if(NOT WIN32) set_tests_properties(test_multiprocess_reader_exception_deprecated PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") - set_tests_properties(test_layers_deprecated PROPERTIES TIMEOUT 120) endif() # setting timeout value as 15S @@ -622,8 +610,6 @@ set_tests_properties(test_argsort_op_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_sgd_op_deprecated PROPERTIES TIMEOUT 250) set_tests_properties(test_generator_dataloader_deprecated PROPERTIES TIMEOUT 120) -set_tests_properties(test_program_prune_backward_deprecated PROPERTIES TIMEOUT - 120) set_tests_properties(test_decoupled_py_reader_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_fuse_bn_act_pass_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_conv2d_api_deprecated PROPERTIES TIMEOUT 120) @@ -718,5 +704,4 @@ set_tests_properties(test_apply_pass_to_program_deprecated PROPERTIES TIMEOUT set_tests_properties(test_conv3d_layer_deprecated PROPERTIES TIMEOUT 100) set_tests_properties(test_attribute_var_deprecated PROPERTIES TIMEOUT 100) set_tests_properties(test_inference_api_deprecated PROPERTIES TIMEOUT 100) -set_tests_properties(test_lbfgs_deprecated PROPERTIES TIMEOUT 100) set_tests_properties(test_group_norm_op_deprecated PROPERTIES TIMEOUT 1000) diff --git a/test/deprecated/legacy_test/test_bfgs_deprecated.py b/test/deprecated/legacy_test/test_bfgs_deprecated.py deleted file mode 100644 index a24f9b1617702d..00000000000000 --- a/test/deprecated/legacy_test/test_bfgs_deprecated.py +++ /dev/null @@ -1,175 +0,0 @@ -# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest - -import numpy as np - -import paddle -import paddle.nn.functional as F -from paddle.incubate.optimizer.functional.bfgs import minimize_bfgs - -np.random.seed(123) - - -def test_static_graph(func, x0, line_search_fn='strong_wolfe', dtype='float32'): - dimension = x0.shape[0] - paddle.enable_static() - main = paddle.static.Program() - startup = paddle.static.Program() - with paddle.static.program_guard(main, startup): - X = paddle.static.data(name='x', shape=[dimension], dtype=dtype) - Y = minimize_bfgs(func, X, line_search_fn=line_search_fn, dtype=dtype) - - exe = paddle.static.Executor() - exe.run(startup) - return exe.run(main, feed={'x': x0}, fetch_list=[Y]) - - -def test_static_graph_H0(func, x0, H0, dtype='float32'): - paddle.enable_static() - main = paddle.static.Program() - startup = paddle.static.Program() - with paddle.static.program_guard(main, startup): - X = paddle.static.data(name='x', shape=[x0.shape[0]], dtype=dtype) - H = paddle.static.data( - name='h', shape=[H0.shape[0], H0.shape[1]], dtype=dtype - ) - Y = minimize_bfgs( - func, X, initial_inverse_hessian_estimate=H, dtype=dtype - ) - - exe = paddle.static.Executor() - exe.run(startup) - return exe.run(main, feed={'x': x0, 'h': H0}, fetch_list=[Y]) - - -def test_dynamic_graph( - func, x0, H0=None, line_search_fn='strong_wolfe', dtype='float32' -): - paddle.disable_static() - x0 = paddle.to_tensor(x0) - if H0 is not None: - H0 = paddle.to_tensor(H0) - return minimize_bfgs( - func, - x0, - initial_inverse_hessian_estimate=H0, - line_search_fn=line_search_fn, - dtype=dtype, - ) - - -class TestBfgs(unittest.TestCase): - def test_quadratic_nd(self): - for dimension in [1, 10]: - minimum = np.random.random(size=[dimension]).astype('float32') - scale = np.exp(np.random.random(size=[dimension]).astype('float32')) - - def func(x): - minimum_ = paddle.assign(minimum) - scale_ = paddle.assign(scale) - return paddle.sum( - paddle.multiply(scale_, (F.square_error_cost(x, minimum_))) - ) - - x0 = np.random.random(size=[dimension]).astype('float32') - results = test_static_graph(func=func, x0=x0) - np.testing.assert_allclose( - minimum, results[2], rtol=1e-05, atol=1e-8 - ) - - results = test_dynamic_graph(func=func, x0=x0) - np.testing.assert_allclose( - minimum, results[2].numpy(), rtol=1e-05, atol=1e-8 - ) - - def test_inf_minima(self): - extreme_point = np.array([-1, 2]).astype('float32') - - def func(x): - # df = 3(x - 1.01)(x - 0.99) - # f = x^3 - 3x^2 + 3*1.01*0.99x - return ( - x * x * x / 3.0 - - (extreme_point[0] + extreme_point[1]) * x * x / 2 - + extreme_point[0] * extreme_point[1] * x - ) - - x0 = np.array([-1.7]).astype('float32') - results = test_static_graph(func, x0) - self.assertFalse(results[0][0]) - - def test_multi_minima(self): - def func(x): - # df = 12(x + 1.1)(x - 0.2)(x - 0.8) - # f = 3*x^4+0.4*x^3-5.46*x^2+2.112*x - # minimum = -1.1 or 0.8. - # All these minima may be reached from appropriate starting points. - return 3 * x**4 + 0.4 * x**3 - 5.64 * x**2 + 2.112 * x - - x0 = np.array([0.82], dtype='float64') - - results = test_static_graph(func, x0, dtype='float64') - np.testing.assert_allclose(0.8, results[2], rtol=1e-05, atol=1e-8) - - def test_rosenbrock(self): - # The Rosenbrock function is a standard optimization test case. - a = np.random.random(size=[1]).astype('float32') - minimum = [a.item(), (a**2).item()] - b = np.random.random(size=[1]).astype('float32') - - def func(position): - # f(x, y) = (a - x)^2 + b (y - x^2)^2 - # minimum = (a, a^2) - x, y = position[0], position[1] - c = (a - x) ** 2 + b * (y - x**2) ** 2 - # the return can't be np array[1], or in jacobin will cause flat error - return c[0] - - x0 = np.random.random(size=[2]).astype('float32') - - results = test_dynamic_graph(func, x0) - np.testing.assert_allclose(minimum, results[2], rtol=1e-05, atol=1e-8) - - def test_exception(self): - def func(x): - return paddle.dot(x, x) - - x0 = np.random.random(size=[2]).astype('float32') - H0 = np.array([[2.0, 0.0], [0.0, 0.9]]).astype('float32') - - # test initial_inverse_hessian_estimate is good - results = test_static_graph_H0(func, x0, H0, dtype='float32') - np.testing.assert_allclose( - [0.0, 0.0], results[2], rtol=1e-05, atol=1e-8 - ) - self.assertTrue(results[0][0]) - - # test initial_inverse_hessian_estimate is bad - H1 = np.array([[1.0, 2.0], [2.0, 1.0]]).astype('float32') - self.assertRaises(ValueError, test_dynamic_graph, func, x0, H0=H1) - - # test line_search_fn is bad - self.assertRaises( - NotImplementedError, - test_static_graph, - func, - x0, - line_search_fn='other', - ) - - -if __name__ == '__main__': - unittest.main() diff --git a/test/deprecated/legacy_test/test_desc_clone_deprecated.py b/test/deprecated/legacy_test/test_desc_clone_deprecated.py deleted file mode 100644 index 114740c4a528c8..00000000000000 --- a/test/deprecated/legacy_test/test_desc_clone_deprecated.py +++ /dev/null @@ -1,302 +0,0 @@ -# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import collections -import functools -import sys -import unittest - -sys.path.append("../../legacy_test") -import nets - -import paddle -from paddle import base -from paddle.base import core - -SEED = 1 -DTYPE = "float32" -paddle.dataset.mnist.fetch() -paddle.enable_static() - - -def cnn_model(data): - conv_pool_1 = nets.simple_img_conv_pool( - input=data, - filter_size=5, - num_filters=20, - pool_size=2, - pool_stride=2, - act="relu", - ) - conv_pool_2 = nets.simple_img_conv_pool( - input=conv_pool_1, - filter_size=5, - num_filters=50, - pool_size=2, - pool_stride=2, - act="relu", - ) - - # TODO(dzhwinter) : refine the initializer and random seed setting - SIZE = 10 - input_shape = conv_pool_2.shape - param_shape = [ - functools.reduce(lambda a, b: a * b, input_shape[1:], 1), - SIZE, - ] - scale = (2.0 / (param_shape[0] ** 2 * SIZE)) ** 0.5 - - predict = paddle.static.nn.fc( - x=conv_pool_2, - size=SIZE, - activation="softmax", - weight_attr=base.param_attr.ParamAttr( - initializer=paddle.nn.initializer.Normal(loc=0.0, scale=scale) - ), - ) - return predict - - -def get_model(batch_size): - # Input data - images = paddle.static.data( - name='pixel', shape=[-1, 1, 28, 28], dtype=DTYPE - ) - label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64') - - # Train program - predict = cnn_model(images) - cost = paddle.nn.functional.cross_entropy( - input=predict, label=label, reduction='none', use_softmax=False - ) - avg_cost = paddle.mean(x=cost) - - # Evaluator - batch_size_tensor = paddle.tensor.create_tensor(dtype='int64') - batch_acc = paddle.static.accuracy( - input=predict, label=label, total=batch_size_tensor - ) - - inference_program = base.default_main_program().clone() - # Optimization - opt = paddle.optimizer.Adam(learning_rate=0.001, beta1=0.9, beta2=0.999) - - # Reader - train_reader = paddle.batch( - paddle.dataset.mnist.train(), batch_size=batch_size - ) - test_reader = paddle.batch( - paddle.dataset.mnist.test(), batch_size=batch_size - ) - opt.minimize(avg_cost) - return ( - inference_program, - avg_cost, - train_reader, - test_reader, - batch_acc, - predict, - ) - - -def operator_equal(a, b): - if a.__str__() != b.__str__(): - raise ValueError("In operator_equal not equal\n") - - for k, v in a.__dict__.items(): - if isinstance(v, (base.framework.Program, base.framework.Block)): - continue - - elif isinstance(v, core.OpDesc): - continue - - elif isinstance(v, collections.OrderedDict): - v0 = sorted(v.items(), key=lambda x: x[0]) - v1 = sorted(b.__dict__[k].items(), key=lambda x: x[0]) - - if v0 != v1: - raise ValueError(f"In operator_equal not equal:{k}\n") - - elif v != b.__dict__[k]: - raise ValueError(f"In operator_equal not equal:{k}\n") - - return True - - -def block_equal(a, b): - for k, v in a.__dict__.items(): - if isinstance( - v, (core.ProgramDesc, base.framework.Program, core.BlockDesc) - ): - continue - elif k == "ops": - assert len(a.ops) == len(b.ops) - for i in range(0, len(a.ops)): - if not operator_equal(a.ops[i], b.ops[i]): - raise ValueError(f"In block_equal not equal:{k}\n") - - elif isinstance(v, collections.OrderedDict): - for key, value in v.items(): - if str(value) != str(b.__dict__[k][key]): - raise ValueError(f"In block_equal not equal:{k}\n") - - elif v != b.__dict__[k]: - raise ValueError(f"In block_equal not equal:{k}\n") - - return True - - -def program_equal(a, b): - for k, v in a.__dict__.items(): - if isinstance(v, core.ProgramDesc): - continue - - elif k == 'blocks': - for i in range(0, len(a.blocks)): - if not block_equal(a.blocks[i], b.blocks[i]): - raise ValueError(f"In operator_equal not equal:{k}\n") - return False - assert len(a.blocks) == len(b.blocks) - elif k == '_auto_checkpoint_name': - continue - elif v != b.__dict__[k]: - raise ValueError(f"In program_equal not equal:{k}\n") - - return True - - -class TestCloneWithStopGradient(unittest.TestCase): - def test_clone_with_stop_gradient(self): - train_program = base.Program() - startup_program = base.Program() - with base.program_guard(train_program, startup_program): - img = paddle.static.data(name='image', shape=[-1, 784]) - hidden1 = paddle.static.nn.fc(x=img, size=200, activation='relu') - hidden1.stop_gradient = True - hidden2 = paddle.nn.functional.dropout(hidden1, p=0.5) - loss = paddle.nn.functional.cross_entropy( - input=paddle.static.nn.fc( - hidden2, size=10, activation='softmax' - ), - label=paddle.static.data( - name='label', shape=[-1, 1], dtype='int64' - ), - reduction='none', - use_softmax=False, - ) - avg_loss = paddle.mean(loss) - test_program = train_program.clone(for_test=False) - - self.assertEqual( - test_program.block(0).var(hidden1.name).stop_gradient, True - ) - self.assertEqual( - test_program.block(0).var(hidden2.name).stop_gradient, True - ) - - -class TestCloneWithStopGradientInSubBlock(unittest.TestCase): - def test_clone_with_stop_gradient(self): - train_program = base.Program() - startup_program = base.Program() - with base.program_guard(train_program, startup_program): - img = paddle.static.data(name='image', shape=[-1, 784]) - true = paddle.ones(shape=[1], dtype="float32") - hidden1 = paddle.static.nn.fc(x=img, size=200, activation='relu') - hidden1.stop_gradient = True - - cond = paddle.equal(true, true) - - def true_fn(): - hidden2 = paddle.nn.functional.dropout(hidden1, p=0.5) - hidden2.stop_gradient = True - return hidden2 - - def false_fn(): - hidden2 = paddle.nn.functional.dropout(hidden1, p=0.6) - return hidden2 - - hidden2 = paddle.static.nn.cond(cond, true_fn, false_fn) - - loss = paddle.nn.functional.cross_entropy( - input=paddle.static.nn.fc( - hidden2, size=10, activation='softmax' - ), - label=paddle.static.data( - name='label', shape=[-1, 1], dtype='int64' - ), - reduction='none', - use_softmax=False, - ) - avg_loss = paddle.mean(loss) - test_program = train_program.clone(for_test=False) - - self.assertEqual( - test_program.block(0).var(hidden1.name).stop_gradient, True - ) - for var in test_program.block(1).vars.values(): - var2 = train_program.block(1).var(var.name) - self.assertEqual(var.stop_gradient, var2.stop_gradient) - for var in test_program.block(2).vars.values(): - var2 = train_program.block(2).var(var.name) - self.assertEqual(var.stop_gradient, var2.stop_gradient) - - -class TestCloneWithRaise(unittest.TestCase): - def test_clone_with_stop_gradient(self): - train_program = base.Program() - startup_program = base.Program() - with base.program_guard(train_program, startup_program): - img = paddle.static.data(name='image', shape=[-1, 784]) - true = paddle.ones(shape=[1], dtype="float32") - hidden1 = paddle.static.nn.fc(x=img, size=200, activation='relu') - hidden1.stop_gradient = True - - cond = paddle.equal(true, true) - - def true_fn(): - hidden2 = paddle.nn.functional.dropout(hidden1, p=0.5) - hidden2.stop_gradient = True - return hidden2 - - def false_fn(): - hidden2 = paddle.nn.functional.dropout(hidden1, p=0.6) - return hidden2 - - hidden2 = paddle.static.nn.cond(cond, true_fn, false_fn) - loss = paddle.nn.functional.cross_entropy( - input=paddle.static.nn.fc( - hidden2, size=10, activation='softmax' - ), - label=paddle.static.data( - name='label', shape=[-1, 1], dtype='int64' - ), - reduction='none', - use_softmax=False, - ) - avg_loss = paddle.mean(loss) - test_program = train_program.clone(for_test=False) - - self.assertRaises( - ValueError, train_program._copy_data_info_from, startup_program - ) - self.assertRaises( - TypeError, - train_program._copy_data_info_from, - startup_program.block(0), - ) - - -if __name__ == "__main__": - unittest.main() diff --git a/test/deprecated/legacy_test/test_ema_deprecated.py b/test/deprecated/legacy_test/test_ema_deprecated.py deleted file mode 100644 index 6f8ce9750b342d..00000000000000 --- a/test/deprecated/legacy_test/test_ema_deprecated.py +++ /dev/null @@ -1,102 +0,0 @@ -# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os -import unittest - -import numpy as np - -import paddle -from paddle import base - -paddle.enable_static() - - -class TestExponentialMovingAverage(unittest.TestCase): - def setUp(self): - self._places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not base.core.is_compiled_with_cuda() - ): - self._places.append(base.CPUPlace()) - if base.core.is_compiled_with_cuda(): - self._places.append(base.CUDAPlace(0)) - self._ema_decay = 0.999 - self._param_name = "fc.weight" - - self._train_program = base.Program() - self._startup_prog = base.Program() - with ( - base.program_guard(self._train_program, self._startup_prog), - base.unique_name.guard(), - ): - data = paddle.static.data(name='x', shape=[-1, 5], dtype='float32') - hidden = paddle.static.nn.fc( - x=data, size=10, weight_attr=self._param_name - ) - cost = paddle.mean(hidden) - - self._test_program = base.default_main_program().clone( - for_test=True - ) - - optimizer = paddle.optimizer.Adam(learning_rate=0.001) - optimizer.minimize(cost) - - self._ema = paddle.static.ExponentialMovingAverage(self._ema_decay) - self._ema.update() - - def train(self, place): - exe = base.Executor(place) - exe.run(self._startup_prog) - - params = [] - for pass_id in range(2): - for batch_id in range(3): - data = np.random.random(size=(10, 5)).astype('float32') - tmp_param = np.array( - base.global_scope().find_var(self._param_name).get_tensor() - ) - exe.run(program=self._train_program, feed={'x': data}) - tmp_param = np.array( - base.global_scope().find_var(self._param_name).get_tensor() - ) - params.append(tmp_param) - - with self._ema.apply(exe): - final_ema = np.array( - base.global_scope().find_var(self._param_name).get_tensor() - ) - data = np.random.random(size=(10, 5)).astype('float32') - exe.run(program=self._test_program, feed={'x': data}) - return params, final_ema - - def test_check_ema(self): - for place in self._places: - params, final_ema = self.train(place) - manu_ema = np.zeros_like(final_ema) - if len(params) > 0: - for param in params: - manu_ema = ( - self._ema_decay * manu_ema - + (1 - self._ema_decay) * param - ) - manu_ema = manu_ema / (1.0 - self._ema_decay ** len(params)) - np.testing.assert_allclose(manu_ema, final_ema, rtol=1e-05) - - -if __name__ == '__main__': - unittest.main() diff --git a/test/deprecated/legacy_test/test_ema_fleet_deprecated.py b/test/deprecated/legacy_test/test_ema_fleet_deprecated.py deleted file mode 100644 index 962efd73f873d7..00000000000000 --- a/test/deprecated/legacy_test/test_ema_fleet_deprecated.py +++ /dev/null @@ -1,115 +0,0 @@ -# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os -import unittest - -import numpy as np - -import paddle -from paddle import static, utils - -paddle.enable_static() - - -def gen_data(): - return np.random.random(size=(10, 5)).astype('float32') - - -class TestFleetStaticEMA(unittest.TestCase): - def setUp(self): - self._places = [paddle.CPUPlace()] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not paddle.device.is_compiled_with_cuda() - ): - self._places.append(paddle.CPUPlace()) - if paddle.device.is_compiled_with_cuda(): - self._places.append(paddle.CUDAPlace(0)) - self._ema_decay = 0.999 - self._param_name = "fc.weight" - self._train_program = static.Program() - self._startup_prog = static.Program() - - strategy = paddle.distributed.fleet.DistributedStrategy() - strategy.without_graph_optimization = True - paddle.distributed.fleet.init(is_collective=True, strategy=strategy) - - with ( - static.program_guard(self._train_program, self._startup_prog), - utils.unique_name.guard(), - ): - data = static.data(name='x', shape=[-1, 5], dtype='float32') - hidden = static.nn.fc(x=data, size=10, weight_attr=self._param_name) - cost = paddle.mean(hidden) - - self._test_program = static.default_main_program().clone( - for_test=True - ) - - optimizer = paddle.optimizer.Adam(learning_rate=0.001) - optimizer = paddle.distributed.fleet.distributed_optimizer( - optimizer, strategy - ) - optimizer.minimize(cost) - - self._ema = static.ExponentialMovingAverage(self._ema_decay) - self._ema.update() - - def train(self, place, restore): - exe = static.Executor(place) - exe.run(self._startup_prog) - - params = [] - for pass_id in range(2): - for batch_id in range(3): - exe.run(program=self._train_program, feed={'x': gen_data()}) - tmp_param = np.array( - static.global_scope() - .find_var(self._param_name) - .get_tensor() - ) - params.append(tmp_param) - - with self._ema.apply(exe, restore): - final_ema = np.array( - static.global_scope() - .find_var(self._param_name) - .get_tensor() - ) - exe.run(program=self._test_program, feed={'x': gen_data()}) - if not restore: - self._ema.restore(exe) - - return params, final_ema - - def test_check_ema(self): - for place in self._places: - for restore in (True, False): - params, final_ema = self.train(place, restore) - manu_ema = np.zeros_like(final_ema) - if len(params) > 0: - for param in params: - manu_ema = ( - self._ema_decay * manu_ema - + (1 - self._ema_decay) * param - ) - manu_ema = manu_ema / (1.0 - self._ema_decay ** len(params)) - np.testing.assert_allclose(manu_ema, final_ema, rtol=1e-05) - - -if __name__ == '__main__': - paddle.enable_static() - unittest.main() diff --git a/test/deprecated/legacy_test/test_get_inputs_outputs_in_block_deprecated.py b/test/deprecated/legacy_test/test_get_inputs_outputs_in_block_deprecated.py deleted file mode 100644 index 0d4b743c48ca7f..00000000000000 --- a/test/deprecated/legacy_test/test_get_inputs_outputs_in_block_deprecated.py +++ /dev/null @@ -1,81 +0,0 @@ -# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest - -import numpy as np - -import paddle - -paddle.enable_static() - - -class TestGetInputsOutputsInBlock(unittest.TestCase): - def test_ordered(self): - # Program variable names may be different when test order is different - # This helper makes the test ordered. - self._test_while_loop() - self._test_cond() - - def _test_while_loop(self): - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - with paddle.static.program_guard(main_program, startup_program): - i = paddle.assign(np.array([1])) - ten = paddle.assign(np.array([10])) - - def while_cond(i): - # use ten in parent block without passing it - return i < ten - - def while_body(i): - # variable created in sub block - one = paddle.assign(np.array([1])) - i = i + one - return [i] - - i = paddle.static.nn.while_loop(while_cond, while_body, [i]) - - sub_block = main_program.block(1) - ( - inner_inputs, - inner_outputs, - ) = paddle.utils.get_inputs_outputs_in_block(sub_block) - # 'assign_0.tmp_0', 'assign_1.tmp_0' are name of i and ten in program - self.assertTrue(inner_inputs == {'assign_0.tmp_0', 'assign_1.tmp_0'}) - # 'tmp_0', 'assign_0.tmp_0' are name of i < ten and i in program - self.assertTrue(inner_outputs == {'tmp_0', 'assign_0.tmp_0'}) - - def _test_cond(self): - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - with paddle.static.program_guard(main_program, startup_program): - a = paddle.zeros((1, 1)) - b = paddle.zeros((1, 1)) - c = a * b - out = paddle.static.nn.cond(a < b, lambda: a + c, lambda: b * b) - - sub_block = main_program.block(1) - ( - inner_inputs, - inner_outputs, - ) = paddle.utils.get_inputs_outputs_in_block(sub_block) - # 'fill_constant_1.tmp_0', 'tmp_3' are names of a, c - self.assertTrue(inner_inputs == {'fill_constant_1.tmp_0', 'tmp_0'}) - # '_generated_var_1', is name of a + c - self.assertTrue(inner_outputs == {'_generated_var_0'}) - - -if __name__ == "__main__": - unittest.main() diff --git a/test/deprecated/legacy_test/test_layers_deprecated.py b/test/deprecated/legacy_test/test_layers_deprecated.py deleted file mode 100644 index eff81097bb2532..00000000000000 --- a/test/deprecated/legacy_test/test_layers_deprecated.py +++ /dev/null @@ -1,1466 +0,0 @@ -# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import contextlib -import inspect -import sys -import unittest - -sys.path.append("../../legacy_test") -import nets -import numpy as np -from decorator_helper import prog_scope -from test_imperative_base import new_program_scope - -import paddle -from paddle import base -from paddle.base import core, dygraph -from paddle.base.framework import program_guard -from paddle.incubate.layers.nn import ( - batch_fc, - partial_concat, - partial_sum, - rank_attention, - shuffle_batch, -) -from paddle.tensor import random - -paddle.enable_static() - - -class LayerTest(unittest.TestCase): - @classmethod - def setUpClass(cls): - cls.seed = 111 - - @classmethod - def tearDownClass(cls): - pass - - def _get_place(self, force_to_use_cpu=False): - # this option for ops that only have cpu kernel - if force_to_use_cpu: - return core.CPUPlace() - else: - if core.is_compiled_with_cuda(): - return core.CUDAPlace(0) - return core.CPUPlace() - - @contextlib.contextmanager - def static_graph(self): - with new_program_scope(): - paddle.seed(self.seed) - paddle.framework.random._manual_program_seed(self.seed) - yield - - def get_static_graph_result( - self, feed, fetch_list, with_lod=False, force_to_use_cpu=False - ): - exe = base.Executor(self._get_place(force_to_use_cpu)) - exe.run(paddle.static.default_startup_program()) - return exe.run( - paddle.static.default_main_program(), - feed=feed, - fetch_list=fetch_list, - return_numpy=(not with_lod), - ) - - @contextlib.contextmanager - def dynamic_graph(self, force_to_use_cpu=False): - with base.dygraph.guard( - self._get_place(force_to_use_cpu=force_to_use_cpu) - ): - paddle.seed(self.seed) - paddle.framework.random._manual_program_seed(self.seed) - yield - - -class TestLayer(LayerTest): - def test_cvm(self): - inp = np.ones([10, 10], dtype='float32') - arr = [[0.6931472, -1.904654e-09, 1, 1, 1, 1, 1, 1, 1, 1]] * 10 - cvm1 = np.array(arr, dtype='float32') - cvm2 = np.ones([10, 8], dtype='float32') - show_clk = np.ones([10, 2], dtype='float32') - with self.static_graph(): - x = paddle.static.data( - name='data', - shape=[10, 10], - dtype='float32', - ) - u = paddle.static.data( - name='show_click', - shape=[10, 2], - dtype='float32', - ) - no_cvm = paddle.static.nn.continuous_value_model(x, u, True) - static_ret1 = self.get_static_graph_result( - feed={'data': inp, 'show_click': show_clk}, - fetch_list=[no_cvm], - )[0] - with self.static_graph(): - x = paddle.static.data( - name='data', - shape=[10, 10], - dtype='float32', - ) - u = paddle.static.data( - name='show_click', - shape=[10, 2], - dtype='float32', - ) - cvm = paddle.static.nn.continuous_value_model(x, u, False) - static_ret2 = self.get_static_graph_result( - feed={'data': inp, 'show_click': show_clk}, fetch_list=[cvm] - )[0] - np.testing.assert_allclose(static_ret1, cvm1, rtol=1e-5, atol=1e-06) - np.testing.assert_allclose(static_ret2, cvm2, rtol=1e-5, atol=1e-06) - - def test_conv2d_transpose(self): - inp_np = np.arange(0, 24).reshape([2, 3, 2, 2]).astype('float32') - with self.static_graph(): - img = paddle.static.data( - name='pixel', shape=[-1, 3, 2, 2], dtype='float32' - ) - out = paddle.static.nn.conv2d_transpose( - input=img, - num_filters=10, - filter_size=27, - act='sigmoid', - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - static_rlt = self.get_static_graph_result( - feed={'pixel': inp_np}, fetch_list=[out] - )[0] - with self.static_graph(): - img = paddle.static.data( - name='pixel', shape=[-1, 3, 2, 2], dtype='float32' - ) - conv2d_transpose = paddle.nn.Conv2DTranspose( - 3, - 10, - 27, - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - out = conv2d_transpose(img) - out = paddle.nn.functional.sigmoid(out) - static_rlt2 = self.get_static_graph_result( - feed={'pixel': inp_np}, fetch_list=[out] - )[0] - with self.dynamic_graph(): - conv2d_transpose = paddle.nn.Conv2DTranspose( - 3, - 10, - 27, - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - dy_rlt = conv2d_transpose(paddle.to_tensor(inp_np)) - dy_rlt = paddle.nn.functional.sigmoid(dy_rlt) - dy_rlt_value = dy_rlt.numpy() - np.testing.assert_allclose(static_rlt2, static_rlt, rtol=1e-05) - np.testing.assert_allclose(dy_rlt_value, static_rlt2, rtol=1e-05) - - with self.dynamic_graph(): - images = np.ones([2, 3, 5, 5], dtype='float32') - custom_weight = np.random.randn(3, 3, 2, 2).astype("float32") - weight_attr = base.ParamAttr( - initializer=paddle.nn.initializer.Assign(custom_weight) - ) - conv2d1 = paddle.nn.Conv2DTranspose(3, 3, [2, 2]) - conv2d2 = paddle.nn.Conv2DTranspose( - 3, - 3, - [2, 2], - weight_attr=weight_attr, - ) - dy_ret1 = conv2d1(paddle.to_tensor(images)) - dy_ret2 = conv2d2(paddle.to_tensor(images)) - self.assertFalse(np.array_equal(dy_ret1.numpy(), dy_ret2.numpy())) - - conv2d1_weight_np = conv2d1.weight.numpy() - conv2d1_bias = conv2d1.bias - self.assertFalse( - np.array_equal(conv2d1_weight_np, conv2d2.weight.numpy()) - ) - conv2d2.weight.set_value(conv2d1_weight_np) - np.testing.assert_array_equal( - conv2d1_weight_np, conv2d2.weight.numpy() - ) - conv2d2.bias.set_value(conv2d1_bias) - dy_ret1 = conv2d1(paddle.to_tensor(images)) - dy_ret2 = conv2d2(paddle.to_tensor(images)) - np.testing.assert_array_equal(dy_ret1.numpy(), dy_ret2.numpy()) - - conv2d2.weight = conv2d1.weight - conv2d2.bias = conv2d1.bias - np.testing.assert_array_equal( - conv2d1.weight.numpy(), conv2d2.weight.numpy() - ) - np.testing.assert_array_equal( - conv2d1.bias.numpy(), conv2d2.bias.numpy() - ) - - with self.static_graph(): - # the input of Conv2DTranspose must be Variable. - def test_Variable(): - images = np.ones([2, 3, 5, 5], dtype='float32') - conv2d = paddle.nn.Conv2DTranspose(3, 3, [2, 2]) - conv2d_ret1 = conv2d(images) - - self.assertRaises(TypeError, test_Variable) - - # the input dtype of Conv2DTranspose must be float16 or float32 or float64 - # float16 only can be set on GPU place - def test_type(): - images = paddle.static.data( - name='pixel', shape=[-1, 3, 5, 5], dtype='int32' - ) - conv2d = paddle.nn.Conv2DTranspose(3, 3, [2, 2]) - conv2d_ret2 = conv2d(images) - - self.assertRaises(TypeError, test_type) - - def test_bilinear_tensor_product(self): - def _test_static_specific(inp_np_x, inp_np_y): - with self.static_graph(): - data_x = paddle.static.data( - name='x', shape=[1, 3], dtype="float32" - ) - data_y = paddle.static.data( - name='y', shape=[1, 3], dtype="float32" - ) - out = paddle.static.nn.common.bilinear_tensor_product( - data_x, - data_y, - 6, - bias_attr=paddle.nn.initializer.Constant(value=1), - act='sigmoid', - ) - - static_rlt = self.get_static_graph_result( - feed={'x': inp_np_x, 'y': inp_np_y}, fetch_list=[out] - )[0] - - return static_rlt - - def _test_static(inp_np_x, inp_np_y): - with self.static_graph(): - data_x = paddle.static.data( - name='x', shape=[1, 3], dtype="float32" - ) - data_y = paddle.static.data( - name='y', shape=[1, 3], dtype="float32" - ) - btp = paddle.nn.Bilinear( - 3, - 3, - 6, - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - out = btp(data_x, data_y) - out = paddle.nn.functional.sigmoid(out) - static_rlt2 = self.get_static_graph_result( - feed={'x': inp_np_x, 'y': inp_np_y}, fetch_list=[out] - )[0] - - return static_rlt2 - - def _test_dygraph_1(inp_np_x, inp_np_y): - with self.dynamic_graph(): - btp = paddle.nn.Bilinear( - 3, - 3, - 6, - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - dy_rlt = btp( - paddle.to_tensor(inp_np_x), - paddle.to_tensor(inp_np_y), - ) - dy_rlt = paddle.nn.functional.sigmoid(dy_rlt) - dy_rlt_value = dy_rlt.numpy() - - with self.dynamic_graph(): - btp2 = paddle.nn.Bilinear(3, 3, 6) - dy_rlt2 = btp2( - paddle.to_tensor(inp_np_x), - paddle.to_tensor(inp_np_y), - ) - dy_rlt2 = paddle.nn.functional.sigmoid(dy_rlt2) - dy_rlt2_value = dy_rlt2.numpy() - - with self.static_graph(): - data_x2 = paddle.static.data( - name='x', shape=[1, 3], dtype="float32" - ) - data_y2 = paddle.static.data( - name='y', shape=[1, 3], dtype="float32" - ) - out2 = paddle.static.nn.common.bilinear_tensor_product( - data_x2, data_y2, 6, act='sigmoid' - ) - - static_rlt3 = self.get_static_graph_result( - feed={'x': inp_np_x, 'y': inp_np_y}, fetch_list=[out2] - )[0] - - return dy_rlt_value, dy_rlt2_value, static_rlt3 - - def _test_dygraph_2(inp_np_x, inp_np_y): - with self.dynamic_graph(): - custom_weight = np.random.randn(6, 3, 3).astype("float32") - weight_attr = base.ParamAttr( - initializer=paddle.nn.initializer.Assign(custom_weight) - ) - btp1 = paddle.nn.Bilinear(3, 3, 6) - btp2 = paddle.nn.Bilinear(3, 3, 6, weight_attr=weight_attr) - dy_rlt1 = btp1( - paddle.to_tensor(inp_np_x), - paddle.to_tensor(inp_np_y), - ) - dy_rlt1 = paddle.nn.functional.sigmoid(dy_rlt1) - dy_rlt2 = btp2( - paddle.to_tensor(inp_np_x), - paddle.to_tensor(inp_np_y), - ) - dy_rlt2 = paddle.nn.functional.sigmoid(dy_rlt2) - self.assertFalse( - np.array_equal(dy_rlt1.numpy(), dy_rlt2.numpy()) - ) - btp2.weight.set_value(btp1.weight.numpy()) - btp2.bias.set_value(btp1.bias) - dy_rlt1 = btp1( - paddle.to_tensor(inp_np_x), - paddle.to_tensor(inp_np_y), - ) - dy_rlt2 = btp2( - paddle.to_tensor(inp_np_x), - paddle.to_tensor(inp_np_y), - ) - np.testing.assert_array_equal(dy_rlt1.numpy(), dy_rlt2.numpy()) - - btp2.weight = btp1.weight - btp2.bias = btp1.bias - np.testing.assert_array_equal( - btp1.weight.numpy(), btp2.weight.numpy() - ) - np.testing.assert_array_equal( - btp1.bias.numpy(), btp2.bias.numpy() - ) - - inp_np_x = np.array([[1, 2, 3]]).astype('float32') - inp_np_y = np.array([[4, 5, 6]]).astype('float32') - - static_rlt = _test_static_specific(inp_np_x, inp_np_y) - static_rlt2 = _test_static(inp_np_x, inp_np_y) - dy_rlt_value, dy_rlt2_value, static_rlt3 = _test_dygraph_1( - inp_np_x, inp_np_y - ) - np.testing.assert_array_equal(dy_rlt2_value, static_rlt3) - np.testing.assert_array_equal(static_rlt2, static_rlt) - np.testing.assert_array_equal(dy_rlt_value, static_rlt) - - with paddle.pir_utils.IrGuard(): - static_pir_result = _test_static(inp_np_x, inp_np_y) - np.testing.assert_array_equal(static_pir_result, static_rlt) - - def test_embedding(self): - inp_word = np.array([[[1]]]).astype('int64') - dict_size = 20 - with self.static_graph(): - data_t = paddle.static.data( - name='word', shape=[-1, 1], dtype='int64' - ) - data_t.desc.set_need_check_feed(False) - emb = paddle.static.nn.embedding( - input=data_t.squeeze(-2), - size=[dict_size, 32], - param_attr='emb.w', - is_sparse=False, - ) - static_rlt = self.get_static_graph_result( - feed={'word': inp_word}, fetch_list=[emb] - )[0] - with self.static_graph(): - data_t = paddle.static.data( - name='word', shape=[-1, 1], dtype='int64' - ) - data_t.desc.set_need_check_feed(False) - emb2 = paddle.nn.Embedding( - dict_size, 32, weight_attr='emb.w', sparse=False - ) - emb_rlt = emb2(data_t) - static_rlt2 = self.get_static_graph_result( - feed={'word': inp_word}, fetch_list=[emb_rlt] - )[0] - with self.dynamic_graph(): - emb2 = paddle.nn.Embedding( - dict_size, 32, weight_attr='emb.w', sparse=False - ) - dy_rlt = emb2(paddle.to_tensor(inp_word)) - dy_rlt_value = dy_rlt.numpy() - - np.testing.assert_allclose(static_rlt2[0], static_rlt) - np.testing.assert_allclose(dy_rlt_value[0], static_rlt) - - with self.dynamic_graph(): - custom_weight = np.random.randn(dict_size, 32).astype("float32") - weight_attr = base.ParamAttr( - initializer=paddle.nn.initializer.Assign(custom_weight) - ) - emb1 = paddle.nn.Embedding(dict_size, 32, sparse=False) - emb2 = paddle.nn.Embedding( - dict_size, 32, weight_attr=weight_attr, sparse=False - ) - rep1 = emb1(paddle.to_tensor(inp_word)) - rep2 = emb2(paddle.to_tensor(inp_word)) - self.assertFalse(np.array_equal(emb1.weight.numpy(), custom_weight)) - np.testing.assert_array_equal(emb2.weight.numpy(), custom_weight) - self.assertFalse(np.array_equal(rep1.numpy(), rep2.numpy())) - emb2.weight.set_value(emb1.weight.numpy()) - rep2 = emb2(paddle.to_tensor(inp_word)) - np.testing.assert_array_equal(rep1.numpy(), rep2.numpy()) - - emb2.weight = emb1.weight - np.testing.assert_array_equal( - emb1.weight.numpy(), emb2.weight.numpy() - ) - - def test_conv3d(self): - with self.static_graph(): - images = paddle.static.data( - name='pixel', shape=[-1, 3, 6, 6, 6], dtype='float32' - ) - ret = paddle.static.nn.conv3d( - input=images, num_filters=3, filter_size=2 - ) - static_ret = self.get_static_graph_result( - feed={'pixel': np.ones([2, 3, 6, 6, 6], dtype='float32')}, - fetch_list=[ret], - )[0] - - with self.static_graph(): - images = paddle.static.data( - name='pixel', shape=[-1, 3, 6, 6, 6], dtype='float32' - ) - conv3d = paddle.nn.Conv3D( - in_channels=3, out_channels=3, kernel_size=2 - ) - ret = conv3d(images) - static_ret2 = self.get_static_graph_result( - feed={'pixel': np.ones([2, 3, 6, 6, 6], dtype='float32')}, - fetch_list=[ret], - )[0] - - with self.dynamic_graph(): - images = np.ones([2, 3, 6, 6, 6], dtype='float32') - conv3d = paddle.nn.Conv3D( - in_channels=3, out_channels=3, kernel_size=2 - ) - dy_ret = conv3d(paddle.to_tensor(images)) - dy_rlt_value = dy_ret.numpy() - - np.testing.assert_allclose(static_ret, dy_rlt_value, rtol=1e-05) - np.testing.assert_allclose(static_ret, static_ret2, rtol=1e-05) - - with self.dynamic_graph(): - images = np.ones([2, 3, 6, 6, 6], dtype='float32') - custom_weight = np.random.randn(3, 3, 2, 2, 2).astype("float32") - weight_attr = base.ParamAttr( - initializer=paddle.nn.initializer.Assign(custom_weight) - ) - conv3d1 = paddle.nn.Conv3D( - in_channels=3, out_channels=3, kernel_size=2 - ) - conv3d2 = paddle.nn.Conv3D( - in_channels=3, - out_channels=3, - kernel_size=2, - weight_attr=weight_attr, - ) - dy_ret1 = conv3d1(paddle.to_tensor(images)) - dy_ret2 = conv3d2(paddle.to_tensor(images)) - self.assertFalse(np.array_equal(dy_ret1.numpy(), dy_ret2.numpy())) - - conv3d1_weight_np = conv3d1.weight.numpy() - conv3d1_bias = conv3d1.bias - self.assertFalse( - np.array_equal(conv3d1_weight_np, conv3d2.weight.numpy()) - ) - conv3d2.weight.set_value(conv3d1_weight_np) - np.testing.assert_array_equal( - conv3d1_weight_np, conv3d2.weight.numpy() - ) - conv3d1.bias.set_value(conv3d1_bias) - dy_ret1 = conv3d1(paddle.to_tensor(images)) - dy_ret2 = conv3d2(paddle.to_tensor(images)) - np.testing.assert_array_equal(dy_ret1.numpy(), dy_ret2.numpy()) - - conv3d2.weight = conv3d1.weight - conv3d2.bias = conv3d1.bias - np.testing.assert_array_equal( - conv3d1.weight.numpy(), conv3d2.weight.numpy() - ) - np.testing.assert_array_equal( - conv3d1.bias.numpy(), conv3d2.bias.numpy() - ) - - def test_group_norm(self): - if core.is_compiled_with_cuda(): - place = core.CUDAPlace(0) - else: - place = core.CPUPlace() - - shape = (2, 4, 3, 3) - - def _test_static_specific(input): - with self.static_graph(): - X = paddle.static.data(name='X', shape=shape, dtype='float32') - ret = paddle.static.nn.group_norm( - input=X, - groups=2, - param_attr=paddle.nn.initializer.Uniform( - low=-0.5, high=0.5 - ), - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - static_ret = self.get_static_graph_result( - feed={ - 'X': base.create_lod_tensor( - data=input, recursive_seq_lens=[[1, 1]], place=place - ) - }, - fetch_list=[ret], - with_lod=True, - )[0] - - return static_ret - - def _test_static(input): - with self.static_graph(): - X = paddle.static.data(name='X', shape=shape, dtype='float32') - groupNorm = paddle.nn.GroupNorm( - num_channels=shape[1], - num_groups=2, - weight_attr=paddle.nn.initializer.Uniform( - low=-0.5, high=0.5 - ), - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - ret = groupNorm(X) - static_ret2 = self.get_static_graph_result( - feed={ - 'X': base.create_lod_tensor( - data=input, recursive_seq_lens=[[1, 1]], place=place - ) - }, - fetch_list=[ret, groupNorm.weight], - with_lod=True, - )[0] - - return static_ret2 - - def _test_dygraph(input): - with self.dynamic_graph(): - groupNorm = paddle.nn.GroupNorm( - num_channels=shape[1], - num_groups=2, - weight_attr=paddle.nn.initializer.Uniform( - low=-0.5, high=0.5 - ), - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - dy_ret = groupNorm(paddle.to_tensor(input)) - dy_rlt_value = dy_ret.numpy() - return dy_rlt_value - - input = np.random.random(shape).astype('float32') - static_ret = _test_static_specific(input) - static_ret2 = _test_static(input) - dy_rlt_value = _test_dygraph(input) - np.testing.assert_allclose(static_ret, dy_rlt_value, rtol=1e-05) - np.testing.assert_allclose(static_ret, static_ret2, rtol=1e-05) - - with paddle.pir_utils.IrGuard(): - static_ret_pir = _test_static(input) - - np.testing.assert_allclose(static_ret2, static_ret_pir, rtol=1e-05) - - def test_instance_norm(self): - if core.is_compiled_with_cuda(): - place = core.CUDAPlace(0) - else: - place = core.CPUPlace() - - shape = (2, 4, 3, 3) - - def _test_static_specific(input): - with self.static_graph(): - X = paddle.static.data(name='X', shape=shape, dtype='float32') - ret = paddle.static.nn.instance_norm(input=X) - static_ret = self.get_static_graph_result( - feed={'X': input}, fetch_list=[ret] - )[0] - return static_ret - - def _test_static(input): - with self.static_graph(): - X = paddle.static.data(name='X', shape=shape, dtype='float32') - instanceNorm = paddle.nn.InstanceNorm2D(num_features=shape[1]) - ret = instanceNorm(X) - static_ret2 = self.get_static_graph_result( - feed={'X': input}, fetch_list=[ret] - )[0] - return static_ret2 - - def _test_dygraph_1(input): - with self.dynamic_graph(): - instanceNorm = paddle.nn.InstanceNorm2D(num_features=shape[1]) - dy_ret = instanceNorm(paddle.to_tensor(input)) - dy_rlt_value = dy_ret.numpy() - - return dy_rlt_value - - def _test_dygraph_2(input): - with self.dynamic_graph(): - instanceNorm = paddle.nn.InstanceNorm2D(num_features=shape[1]) - dy_ret = instanceNorm(paddle.to_tensor(input)) - dy_rlt_value2 = dy_ret.numpy() - return dy_rlt_value2 - - input = np.random.random(shape).astype('float32') - static_ret = _test_static_specific(input) - static_ret2 = _test_static(input) - dy_rlt_value = _test_dygraph_1(input) - dy_rlt_value2 = _test_dygraph_2(input) - - np.testing.assert_allclose(static_ret, dy_rlt_value, rtol=1e-05) - np.testing.assert_allclose(static_ret, dy_rlt_value2, rtol=1e-05) - np.testing.assert_allclose(static_ret, static_ret2, rtol=1e-05) - - with paddle.pir_utils.IrGuard(): - static_ret_pir = _test_static(input) - - np.testing.assert_allclose(static_ret2, static_ret_pir, rtol=1e-05) - - def _test_errors(): - with self.static_graph(): - # the input of InstanceNorm must be Variable. - def test_Variable(): - instanceNorm = paddle.nn.InstanceNorm2D( - num_features=shape[1] - ) - ret1 = instanceNorm(input) - - self.assertRaises(TypeError, test_Variable) - - # the input dtype of InstanceNorm must be float32 or float64 - def test_type(): - input = np.random.random(shape).astype('int32') - instanceNorm = paddle.nn.InstanceNorm2D( - num_features=shape[1] - ) - ret2 = instanceNorm(input) - - self.assertRaises(TypeError, test_type) - - _test_errors() - with paddle.pir_utils.IrGuard(): - _test_errors() - - def test_spectral_norm(self): - if core.is_compiled_with_cuda(): - place = core.CUDAPlace(0) - else: - place = core.CPUPlace() - - shape = (2, 4, 3, 3) - - input = np.random.random(shape).astype('float32') - - with self.static_graph(): - Weight = paddle.static.data( - name='Weight', shape=shape, dtype='float32' - ) - ret = paddle.static.nn.spectral_norm( - weight=Weight, dim=1, power_iters=2 - ) - static_ret = self.get_static_graph_result( - feed={ - 'Weight': base.create_lod_tensor( - data=input, recursive_seq_lens=[[1, 1]], place=place - ), - }, - fetch_list=[ret], - with_lod=True, - )[0] - - with self.static_graph(): - Weight = paddle.static.data( - name='Weight', shape=shape, dtype='float32' - ) - spectralNorm = paddle.nn.SpectralNorm(shape, dim=1, power_iters=2) - ret = spectralNorm(Weight) - static_ret2 = self.get_static_graph_result( - feed={ - 'Weight': base.create_lod_tensor( - data=input, recursive_seq_lens=[[1, 1]], place=place - ) - }, - fetch_list=[ret], - with_lod=True, - )[0] - - with self.dynamic_graph(): - spectralNorm = paddle.nn.SpectralNorm(shape, dim=1, power_iters=2) - dy_ret = spectralNorm(paddle.to_tensor(input)) - dy_rlt_value = dy_ret.numpy() - - np.testing.assert_allclose(static_ret, dy_rlt_value, rtol=1e-05) - np.testing.assert_allclose(static_ret, static_ret2, rtol=1e-05) - - def test_conv3d_transpose(self): - input_array = ( - np.arange(0, 48).reshape([2, 3, 2, 2, 2]).astype('float32') - ) - - with self.static_graph(): - img = paddle.static.data( - name='pixel', shape=[-1, 3, 2, 2, 2], dtype='float32' - ) - out = paddle.static.nn.conv3d_transpose( - input=img, num_filters=12, filter_size=12, use_cudnn=True - ) - static_rlt = self.get_static_graph_result( - feed={'pixel': input_array}, fetch_list=[out] - )[0] - with self.static_graph(): - img = paddle.static.data( - name='pixel', shape=[-1, 3, 2, 2, 2], dtype='float32' - ) - conv3d_transpose = paddle.nn.Conv3DTranspose( - in_channels=3, out_channels=12, kernel_size=12 - ) - out = conv3d_transpose(img) - static_rlt2 = self.get_static_graph_result( - feed={'pixel': input_array}, fetch_list=[out] - )[0] - with self.dynamic_graph(): - conv3d_transpose = paddle.nn.Conv3DTranspose( - in_channels=3, out_channels=12, kernel_size=12 - ) - dy_rlt = conv3d_transpose(paddle.to_tensor(input_array)) - dy_rlt_value = dy_rlt.numpy() - np.testing.assert_allclose(static_rlt2, static_rlt, rtol=1e-05) - np.testing.assert_allclose(dy_rlt_value, static_rlt, rtol=1e-05) - - with self.dynamic_graph(): - images = np.ones([2, 3, 6, 6, 6], dtype='float32') - custom_weight = np.random.randn(3, 3, 2, 2, 2).astype("float32") - weight_attr = base.ParamAttr( - initializer=paddle.nn.initializer.Assign(custom_weight) - ) - conv3d1 = paddle.nn.Conv3DTranspose( - in_channels=3, - out_channels=3, - kernel_size=2, - bias_attr='conv3d1_b', - ) - conv3d2 = paddle.nn.Conv3DTranspose( - in_channels=3, - out_channels=3, - kernel_size=2, - weight_attr=weight_attr, - bias_attr='conv3d2_b', - ) - dy_ret1 = conv3d1(paddle.to_tensor(images)) - dy_ret2 = conv3d2(paddle.to_tensor(images)) - self.assertFalse(np.array_equal(dy_ret1.numpy(), dy_ret2.numpy())) - - conv3d1_weight_np = conv3d1.weight.numpy() - conv3d1_bias = conv3d1.bias - self.assertFalse( - np.array_equal(conv3d1_weight_np, conv3d2.weight.numpy()) - ) - conv3d2.weight.set_value(conv3d1_weight_np) - np.testing.assert_array_equal( - conv3d1_weight_np, conv3d2.weight.numpy() - ) - conv3d1.bias.set_value(conv3d1_bias) - dy_ret1 = conv3d1(paddle.to_tensor(images)) - dy_ret2 = conv3d2(paddle.to_tensor(images)) - np.testing.assert_array_equal(dy_ret1.numpy(), dy_ret2.numpy()) - - conv3d2.weight = conv3d1.weight - conv3d2.bias = conv3d1.bias - np.testing.assert_array_equal( - conv3d1.weight.numpy(), conv3d2.weight.numpy() - ) - np.testing.assert_array_equal( - conv3d1.bias.numpy(), conv3d2.bias.numpy() - ) - - def test_while_loop(self): - with self.static_graph(): - i = paddle.tensor.fill_constant(shape=[1], dtype='int64', value=0) - ten = paddle.tensor.fill_constant( - shape=[1], dtype='int64', value=10 - ) - - def cond(i): - return paddle.less_than(i, ten) - - def body(i): - return i + 1 - - out = paddle.static.nn.while_loop(cond, body, [i]) - static_ret = self.get_static_graph_result(feed={}, fetch_list=out) - - with self.dynamic_graph(): - i = paddle.tensor.fill_constant(shape=[1], dtype='int64', value=0) - ten = paddle.tensor.fill_constant( - shape=[1], dtype='int64', value=10 - ) - - def cond1(i): - return paddle.less_than(i, ten) - - def body1(i): - return i + 1 - - dy_ret = paddle.static.nn.while_loop(cond1, body1, [i]) - with self.assertRaises(ValueError): - j = paddle.tensor.fill_constant( - shape=[1], dtype='int64', value=0 - ) - - def body2(i): - return i + 1, i + 2 - - paddle.static.nn.while_loop(cond1, body2, [j]) - - np.testing.assert_array_equal(static_ret[0], dy_ret[0].numpy()) - - def test_cond(self): - def less_than_branch(a, b): - return paddle.add(a, b) - - def greater_equal_branch(a, b): - return paddle.subtract(a, b) - - with self.static_graph(): - a = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.1 - ) - b = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.23 - ) - out = paddle.static.nn.cond( - a >= b, - lambda: greater_equal_branch(a, b), - lambda: less_than_branch(a, b), - ) - place = ( - base.CUDAPlace(0) - if core.is_compiled_with_cuda() - else base.CPUPlace() - ) - exe = base.Executor(place) - ret = exe.run(fetch_list=[out]) - static_res = ret[0] - - with self.dynamic_graph(): - a = paddle.to_tensor(np.array([0.1]).astype('float32')) - b = paddle.to_tensor(np.array([0.23]).astype('float32')) - out = paddle.static.nn.cond( - a < b, - lambda: less_than_branch(a, b), - lambda: greater_equal_branch(a, b), - ) - out2 = paddle.static.nn.cond( - a >= b, - lambda: greater_equal_branch(a, b), - lambda: less_than_branch(a, b), - ) - dynamic_res = out.numpy() - dynamic_res2 = out2.numpy() - np.testing.assert_array_equal(dynamic_res, dynamic_res2) - with self.assertRaises(TypeError): - paddle.static.nn.cond(a < b, 'str', 'str') - with self.assertRaises(TypeError): - paddle.static.nn.cond(a >= b, 'str', 'str') - - np.testing.assert_array_equal(static_res, dynamic_res) - - def test_case(self): - def fn_1(): - return paddle.tensor.fill_constant( - shape=[1, 2], dtype='int32', value=1 - ) - - def fn_2(): - return paddle.tensor.fill_constant( - shape=[2, 2], dtype='int32', value=2 - ) - - def fn_3(): - return paddle.tensor.fill_constant( - shape=[3, 2], dtype='int32', value=3 - ) - - with self.static_graph(): - x = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.3 - ) - y = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.1 - ) - z = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.2 - ) - - pred_1 = paddle.less_than(z, x) # true: 0.2 < 0.3 - pred_2 = paddle.less_than(x, y) # false: 0.3 < 0.1 - pred_3 = paddle.equal(x, y) # false: 0.3 == 0.1 - - out_1 = paddle.static.nn.case( - pred_fn_pairs=[(pred_1, fn_1), (pred_2, fn_2)], default=fn_3 - ) - out_2 = paddle.static.nn.case( - pred_fn_pairs=[(pred_2, fn_2), (pred_3, fn_3)] - ) - - place = ( - base.CUDAPlace(0) - if core.is_compiled_with_cuda() - else base.CPUPlace() - ) - exe = base.Executor(place) - static_res1, static_res2 = exe.run(fetch_list=[out_1, out_2]) - - with self.dynamic_graph(): - x = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.3 - ) - y = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.1 - ) - z = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.2 - ) - - pred_1 = paddle.less_than(z, x) # true: 0.2 < 0.3 - pred_2 = paddle.less_than(x, y) # false: 0.3 < 0.1 - pred_3 = paddle.equal(x, y) # false: 0.3 == 0.1 - - out_1 = paddle.static.nn.case( - pred_fn_pairs=[(pred_1, fn_1), (pred_2, fn_2)], default=fn_3 - ) - out_2 = paddle.static.nn.case( - pred_fn_pairs=[(pred_2, fn_2), (pred_3, fn_3)] - ) - dynamic_res1 = out_1.numpy() - dynamic_res2 = out_2.numpy() - - np.testing.assert_array_equal(static_res1, dynamic_res1) - np.testing.assert_array_equal(static_res2, dynamic_res2) - - def test_switch_case(self): - def fn_1(): - return paddle.tensor.fill_constant( - shape=[1, 2], dtype='int32', value=1 - ) - - def fn_2(): - return paddle.tensor.fill_constant( - shape=[2, 2], dtype='int32', value=2 - ) - - def fn_3(): - return paddle.tensor.fill_constant( - shape=[3, 2], dtype='int32', value=3 - ) - - with self.static_graph(): - index_1 = paddle.tensor.fill_constant( - shape=[1], dtype='int32', value=1 - ) - index_2 = paddle.tensor.fill_constant( - shape=[1], dtype='int32', value=2 - ) - - out_1 = paddle.static.nn.switch_case( - branch_index=index_1, - branch_fns={1: fn_1, 2: fn_2}, - default=fn_3, - ) - out_2 = paddle.static.nn.switch_case( - branch_index=index_2, - branch_fns=[(1, fn_1), (2, fn_2)], - default=fn_3, - ) - out_3 = paddle.static.nn.switch_case( - branch_index=index_2, - branch_fns=[(0, fn_1), (4, fn_2), (7, fn_3)], - ) - - place = ( - base.CUDAPlace(0) - if core.is_compiled_with_cuda() - else base.CPUPlace() - ) - exe = base.Executor(place) - static_res1, static_res2, static_res3 = exe.run( - fetch_list=[out_1, out_2, out_3] - ) - - with self.dynamic_graph(): - index_1 = paddle.tensor.fill_constant( - shape=[1], dtype='int32', value=1 - ) - index_2 = paddle.tensor.fill_constant( - shape=[1], dtype='int32', value=2 - ) - - out_1 = paddle.static.nn.switch_case( - branch_index=index_1, - branch_fns={1: fn_1, 2: fn_2}, - default=fn_3, - ) - out_2 = paddle.static.nn.switch_case( - branch_index=index_2, - branch_fns=[(1, fn_1), (2, fn_2)], - default=fn_3, - ) - out_3 = paddle.static.nn.switch_case( - branch_index=index_2, - branch_fns=[(0, fn_1), (4, fn_2), (7, fn_3)], - ) - - dynamic_res1 = out_1.numpy() - dynamic_res2 = out_2.numpy() - dynamic_res3 = out_3.numpy() - - np.testing.assert_array_equal(static_res1, dynamic_res1) - np.testing.assert_array_equal(static_res2, dynamic_res2) - np.testing.assert_array_equal(static_res3, dynamic_res3) - - -class TestBook(LayerTest): - def setUp(self): - self.only_static_set = set({"make_word_embedding"}) - self.not_compare_static_dygraph_set = set( - { - "make_gaussian_random", - "make_kldiv_loss", - "make_uniform_random_batch_size_like", - } - ) - self.all_close_compare = set({"make_spectral_norm"}) - - def test_all_layers(self): - attrs = (getattr(self, name) for name in dir(self)) - methods = filter(inspect.ismethod, attrs) - for method in methods: - if not method.__name__.startswith('make_'): - continue - self._low_data_bound = 0 - self._high_data_bound = 2 - self._batch_size = 2 - self._feed_dict = {} - self._force_to_use_cpu = False - with self.static_graph(): - static_var = method() - if isinstance(static_var, tuple): - static_var = static_var[0] - - if static_var is not None: - fetch_list = [static_var.name] - static_result = self.get_static_graph_result( - feed=self._feed_dict, - fetch_list=fetch_list, - force_to_use_cpu=self._force_to_use_cpu, - ) - - else: - continue - if method.__name__ in self.only_static_set: - continue - - with self.dynamic_graph(self._force_to_use_cpu): - dy_result = method() - if isinstance(dy_result, tuple): - dy_result = dy_result[0] - dy_result_value = dy_result.numpy() - - if method.__name__ in self.all_close_compare: - np.testing.assert_allclose( - static_result[0], - dy_result_value, - rtol=1e-05, - atol=0, - err_msg=f'Result of function [{method.__name__}] compare failed', - ) - continue - - if method.__name__ not in self.not_compare_static_dygraph_set: - np.testing.assert_array_equal( - static_result[0], - dy_result_value, - err_msg=f'Result of function [{method.__name__}] not equal', - ) - - def _get_np_data(self, shape, dtype, append_batch_size=True): - np.random.seed(self.seed) - if append_batch_size: - shape = [self._batch_size, *shape] - if dtype == 'float32': - return np.random.random(shape).astype(dtype) - elif dtype == 'float64': - return np.random.random(shape).astype(dtype) - elif dtype == 'int32': - return np.random.randint( - self._low_data_bound, self._high_data_bound, shape - ).astype(dtype) - elif dtype == 'int64': - return np.random.randint( - self._low_data_bound, self._high_data_bound, shape - ).astype(dtype) - - def _get_data( - self, name, shape, dtype, set_feed_dict=True, append_batch_size=True - ): - if dygraph.base.enabled(): - return paddle.to_tensor( - self._get_np_data(shape, dtype, append_batch_size), - ) - else: - if set_feed_dict: - self._feed_dict[name] = self._get_np_data( - shape, dtype, append_batch_size - ) - if append_batch_size: - shape = [-1, *shape] - data = paddle.static.data( - name=name, - shape=shape, - dtype=dtype, - ) - data.desc.set_need_check_feed(False) - return data - - def make_conv2d_transpose(self): - with program_guard( - base.default_main_program(), base.default_startup_program() - ): - img = self._get_data(name='pixel', shape=[3, 2, 2], dtype='float32') - return paddle.static.nn.conv2d_transpose( - input=img, num_filters=10, output_size=28 - ) - - def make_word_embedding(self): - with program_guard( - base.default_main_program(), base.default_startup_program() - ): - dict_size = 10000 - embed_size = 32 - first_word = self._get_data(name='firstw', shape=[1], dtype='int64') - second_word = self._get_data( - name='secondw', shape=[1], dtype='int64' - ) - third_word = self._get_data(name='thirdw', shape=[1], dtype='int64') - forth_word = self._get_data(name='forthw', shape=[1], dtype='int64') - next_word = self._get_data(name='nextw', shape=[1], dtype='int64') - - embed_first = paddle.static.nn.embedding( - input=first_word, - size=[dict_size, embed_size], - dtype='float32', - param_attr='shared_w', - ) - embed_second = paddle.static.nn.embedding( - input=second_word, - size=[dict_size, embed_size], - dtype='float32', - param_attr='shared_w', - ) - - embed_third = paddle.static.nn.embedding( - input=third_word, - size=[dict_size, embed_size], - dtype='float32', - param_attr='shared_w', - ) - embed_forth = paddle.static.nn.embedding( - input=forth_word, - size=[dict_size, embed_size], - dtype='float32', - param_attr='shared_w', - ) - - concat_embed = paddle.concat( - [embed_first, embed_second, embed_third, embed_forth], - axis=1, - ) - - hidden1 = paddle.static.nn.fc( - x=concat_embed, size=256, activation='sigmoid' - ) - predict_word = paddle.static.nn.fc( - x=hidden1, size=dict_size, activation='softmax' - ) - cost = paddle.nn.functional.cross_entropy( - input=predict_word, - label=next_word, - reduction='none', - use_softmax=False, - ) - avg_cost = paddle.mean(cost) - return avg_cost - - @prog_scope() - def make_nce(self): - window_size = 5 - words = [] - for i in range(window_size): - words.append( - self._get_data(name=f'word_{i}', shape=[1], dtype='int64') - ) - - dict_size = 10000 - label_word = int(window_size // 2) + 1 - - embs = [] - for i in range(window_size): - if i == label_word: - continue - - emb = paddle.static.nn.embedding( - input=words[i], - size=[dict_size, 32], - param_attr='emb.w', - is_sparse=True, - ) - - embs.append(emb) - - embs = paddle.concat(embs, axis=1) - loss = paddle.static.nn.nce( - input=embs, - label=words[label_word], - num_total_classes=dict_size, - param_attr='nce.w', - bias_attr='nce.b', - ) - avg_loss = paddle.mean(loss) - return avg_loss - - def make_bilinear_tensor_product_layer(self): - with program_guard( - base.default_main_program(), base.default_startup_program() - ): - data = self._get_data(name='data', shape=[4], dtype="float32") - - theta = self._get_data(name="theta", shape=[5], dtype="float32") - out = paddle.static.nn.common.bilinear_tensor_product( - data, theta, 6 - ) - return out - - def make_batch_norm(self): - with program_guard( - base.default_main_program(), base.default_startup_program() - ): - data = self._get_data( - name='data', shape=[32, 128, 128], dtype="float32" - ) - out = paddle.static.nn.batch_norm(data) - return out - - def make_batch_norm_momentum_variable(self): - with program_guard( - base.default_main_program(), base.default_startup_program() - ): - data = self._get_data( - name='data', shape=[32, 128, 128], dtype="float32" - ) - momentum = self._get_data( - name='momentum', - shape=[1], - dtype='float32', - append_batch_size=False, - ) - out = paddle.static.nn.batch_norm(data, momentum=momentum) - return out - - def make_spectral_norm(self): - with program_guard( - base.default_main_program(), base.default_startup_program() - ): - weight = self._get_data( - name='weight', - shape=[2, 3, 32, 32], - dtype="float32", - append_batch_size=False, - ) - out = paddle.static.nn.spectral_norm(weight, dim=1, power_iters=1) - return out - - def make_recognize_digits_conv(self): - with base.program_guard( - base.default_main_program(), base.default_startup_program() - ): - images = self._get_data( - name='pixel', shape=[1, 28, 28], dtype='float32' - ) - label = self._get_data(name='label', shape=[1], dtype='int64') - conv_pool_1 = nets.simple_img_conv_pool( - input=images, - filter_size=5, - num_filters=2, - pool_size=2, - pool_stride=2, - act="relu", - ) - conv_pool_2 = nets.simple_img_conv_pool( - input=conv_pool_1, - filter_size=5, - num_filters=4, - pool_size=2, - pool_stride=2, - act="relu", - ) - - conv_pool_2_new = paddle.reshape( - conv_pool_2, - [ - conv_pool_2.shape[0], - conv_pool_2.shape[1] - * conv_pool_2.shape[2] - * conv_pool_2.shape[3], - ], - ) - predict = paddle.nn.Linear( - conv_pool_2.shape[1] - * conv_pool_2.shape[2] - * conv_pool_2.shape[3], - 10, - )(conv_pool_2_new) - predict = paddle.nn.functional.softmax(predict) - cost = paddle.nn.functional.cross_entropy( - input=predict, label=label, reduction='none', use_softmax=False - ) - avg_cost = paddle.mean(cost) - return avg_cost - - def make_uniform_random_batch_size_like(self): - with base.program_guard( - base.default_main_program(), base.default_startup_program() - ): - input = self._get_data( - name="input", shape=[13, 11], dtype='float32' - ) - out = random.uniform_random_batch_size_like(input, [-1, 11]) - return out - - def test_row_conv(self): - # TODO(minqiyang): dygraph do not support lod now - with self.static_graph(): - x = paddle.static.data(name='x', shape=[-1, 16], dtype='float32') - out = paddle.static.nn.row_conv(input=x, future_context_size=2) - return out - - def test_simple_conv2d(self): - # TODO(minqiyang): dygraph do not support layers with param now - with self.static_graph(): - images = paddle.static.data( - name='pixel', shape=[-1, 3, 48, 48], dtype='float32' - ) - return paddle.static.nn.conv2d( - input=images, num_filters=3, filter_size=[4, 4] - ) - - def test_shuffle_batch(self): - # TODO(minqiyang): dygraph do not support lod now - with self.static_graph(): - x = paddle.static.data(name='X', shape=[-1, 4, 50], dtype='float32') - out1 = shuffle_batch(x) - paddle.seed(1000) - out2 = shuffle_batch(x) - self.assertIsNotNone(out1) - self.assertIsNotNone(out2) - return out1 - - def test_rank_attention(self): - with self.static_graph(): - input = paddle.static.data( - name="input", shape=[None, 2], dtype="float32" - ) - rank_offset = paddle.static.data( - name="rank_offset", shape=[None, 7], dtype="int32" - ) - out = rank_attention( - input=input, - rank_offset=rank_offset, - rank_param_shape=[18, 3], - rank_param_attr=base.ParamAttr( - learning_rate=1.0, - name="ubm_rank_param.w_0", - initializer=paddle.nn.initializer.XavierNormal(), - ), - max_rank=3, - ) - return out - - def test_partial_sum(self): - with self.static_graph(): - x = paddle.static.data(name="x", shape=[None, 3], dtype="float32") - y = paddle.static.data(name="y", shape=[None, 3], dtype="float32") - sum = partial_sum([x, y], start_index=0, length=2) - return sum - - def test_partial_concat(self): - with self.static_graph(): - x = paddle.static.data(name="x", shape=[None, 3], dtype="float32") - y = paddle.static.data(name="y", shape=[None, 3], dtype="float32") - concat1 = partial_concat([x, y], start_index=0, length=2) - concat2 = partial_concat(x, start_index=0, length=-1) - return concat1, concat2 - - def test_batch_fc(self): - with self.static_graph(): - input = paddle.static.data( - name="input", shape=[16, 2, 3], dtype="float32" - ) - out = batch_fc( - input=input, - param_size=[16, 3, 10], - param_attr=base.ParamAttr( - learning_rate=1.0, - name="w_0", - initializer=paddle.nn.initializer.XavierNormal(), - ), - bias_size=[16, 10], - bias_attr=base.ParamAttr( - learning_rate=1.0, - name="b_0", - initializer=paddle.nn.initializer.XavierNormal(), - ), - act="relu", - ) - return out - - -if __name__ == '__main__': - unittest.main() diff --git a/test/deprecated/legacy_test/test_lbfgs_deprecated.py b/test/deprecated/legacy_test/test_lbfgs_deprecated.py deleted file mode 100644 index 24e6e7e11d8134..00000000000000 --- a/test/deprecated/legacy_test/test_lbfgs_deprecated.py +++ /dev/null @@ -1,169 +0,0 @@ -# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest - -import numpy as np - -import paddle -import paddle.nn.functional as F -from paddle.incubate.optimizer.functional.lbfgs import minimize_lbfgs - -np.random.seed(123) - - -def test_static_graph(func, x0, line_search_fn='strong_wolfe', dtype='float32'): - dimension = x0.shape[0] - paddle.enable_static() - main = paddle.static.Program() - startup = paddle.static.Program() - with paddle.static.program_guard(main, startup): - X = paddle.static.data(name='x', shape=[dimension], dtype=dtype) - Y = minimize_lbfgs(func, X, line_search_fn=line_search_fn, dtype=dtype) - - exe = paddle.static.Executor() - exe.run(startup) - return exe.run(main, feed={'x': x0}, fetch_list=[Y]) - - -def test_static_graph_H0(func, x0, H0, dtype='float32'): - paddle.enable_static() - main = paddle.static.Program() - startup = paddle.static.Program() - with paddle.static.program_guard(main, startup): - X = paddle.static.data(name='x', shape=[x0.shape[0]], dtype=dtype) - H = paddle.static.data( - name='h', shape=[H0.shape[0], H0.shape[1]], dtype=dtype - ) - Y = minimize_lbfgs( - func, X, initial_inverse_hessian_estimate=H, dtype=dtype - ) - - exe = paddle.static.Executor() - exe.run(startup) - return exe.run(main, feed={'x': x0, 'h': H0}, fetch_list=[Y]) - - -def test_dynamic_graph( - func, x0, H0=None, line_search_fn='strong_wolfe', dtype='float32' -): - paddle.disable_static() - x0 = paddle.to_tensor(x0) - if H0 is not None: - H0 = paddle.to_tensor(H0) - return minimize_lbfgs( - func, - x0, - initial_inverse_hessian_estimate=H0, - line_search_fn=line_search_fn, - dtype=dtype, - ) - - -class TestLbfgs(unittest.TestCase): - def test_quadratic_nd(self): - for dimension in [1, 10]: - minimum = np.random.random(size=[dimension]).astype('float32') - scale = np.exp(np.random.random(size=[dimension]).astype('float32')) - - def func(x): - minimum_ = paddle.assign(minimum) - scale_ = paddle.assign(scale) - return paddle.sum( - paddle.multiply(scale_, (F.square_error_cost(x, minimum_))) - ) - - x0 = np.random.random(size=[dimension]).astype('float32') - results = test_static_graph(func, x0) - np.testing.assert_allclose(minimum, results[2], rtol=1e-05) - - results = test_dynamic_graph(func, x0) - np.testing.assert_allclose(minimum, results[2].numpy(), rtol=1e-05) - - def test_inf_minima(self): - extreme_point = np.array([-1, 2]).astype('float32') - - def func(x): - # df = 3(x - 1.01)(x - 0.99) - # f = x^3 - 3x^2 + 3*1.01*0.99x - return ( - x * x * x / 3.0 - - (extreme_point[0] + extreme_point[1]) * x * x / 2 - + extreme_point[0] * extreme_point[1] * x - ) - - x0 = np.array([-1.7]).astype('float32') - results = test_static_graph(func, x0) - self.assertFalse(results[0][0]) - - def test_multi_minima(self): - def func(x): - # df = 12(x + 1.1)(x - 0.2)(x - 0.8) - # f = 3*x^4+0.4*x^3-5.46*x^2+2.112*x - # minimum = -1.1 or 0.8. - # All these minima may be reached from appropriate starting points. - return 3 * x**4 + 0.4 * x**3 - 5.64 * x**2 + 2.112 * x - - x0 = np.array([0.82], dtype='float64') - - results = test_static_graph(func, x0, dtype='float64') - np.testing.assert_allclose(0.8, results[2], rtol=1e-05) - - def test_rosenbrock(self): - # The Rosenbrock function is a standard optimization test case. - a = np.random.random(size=[1]).astype('float32') - minimum = [a.item(), (a**2).item()] - b = np.random.random(size=[1]).astype('float32') - - def func(position): - # f(x, y) = (a - x)^2 + b (y - x^2)^2 - # minimum = (a, a^2) - x, y = position[0], position[1] - c = (a - x) ** 2 + b * (y - x**2) ** 2 - # the return can't be np array[1], or in jacobin will cause flat error - return c[0] - - x0 = np.random.random(size=[2]).astype('float32') - - results = test_dynamic_graph(func, x0) - np.testing.assert_allclose(minimum, results[2], rtol=1e-05) - - def test_exception(self): - def func(x): - return paddle.dot(x, x) - - x0 = np.random.random(size=[2]).astype('float32') - H0 = np.array([[2.0, 0.0], [0.0, 0.9]]).astype('float32') - - # test dtype is not float32 or float64 - x1 = np.random.random(size=[2]).astype('int32') - self.assertRaises( - ValueError, test_static_graph, func, x1, dtype='int32' - ) - - # test initial_inverse_hessian_estimate is good - results = test_static_graph_H0(func, x0, H0, dtype='float32') - np.testing.assert_allclose([0.0, 0.0], results[2], rtol=1e-05) - self.assertTrue(results[0][0]) - - # test initial_inverse_hessian_estimate is bad and float64 - x2 = np.random.random(size=[2]).astype('float64') - H1 = np.array([[1.0, 2.0], [3.0, 1.0]]).astype('float64') - self.assertRaises( - ValueError, test_static_graph_H0, func, x2, H0=H1, dtype='float64' - ) - - -if __name__ == '__main__': - unittest.main() diff --git a/test/deprecated/legacy_test/test_learning_rate_scheduler_deprecated.py b/test/deprecated/legacy_test/test_learning_rate_scheduler_deprecated.py deleted file mode 100644 index 6e6f1fe01a34f8..00000000000000 --- a/test/deprecated/legacy_test/test_learning_rate_scheduler_deprecated.py +++ /dev/null @@ -1,620 +0,0 @@ -# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import copy -import math -import os -import unittest - -import numpy as np - -import paddle -from paddle import base -from paddle.base import core, framework - - -def exponential_decay( - learning_rate, global_step, decay_steps, decay_rate, staircase=False -): - exponent = global_step / decay_steps - if staircase: - exponent = math.floor(exponent) - return learning_rate * decay_rate**exponent - - -def natural_exp_decay( - learning_rate, global_step, decay_steps, decay_rate, staircase=False -): - exponent = float(global_step) / float(decay_steps) - if staircase: - exponent = math.floor(exponent) - return learning_rate * math.exp(-1 * decay_rate * exponent) - - -def inverse_time_decay( - learning_rate, global_step, decay_steps, decay_rate, staircase=False -): - temp = float(global_step) / float(decay_steps) - if staircase: - temp = math.floor(temp) - return learning_rate / (1 + decay_rate * temp) - - -def polynomial_decay( - learning_rate, - global_step, - decay_steps, - end_learning_rate=0.0001, - power=1.0, - cycle=False, -): - if cycle: - div = math.ceil(global_step / float(decay_steps)) - if div == 0: - div = 1 - decay_steps = decay_steps * div - else: - global_step = min(global_step, decay_steps) - return (learning_rate - end_learning_rate) * ( - (1 - float(global_step) / float(decay_steps)) ** power - ) + end_learning_rate - - -def piecewise_decay(global_step, boundaries, values): - assert len(boundaries) + 1 == len(values) - for i in range(len(boundaries)): - if global_step < boundaries[i]: - return values[i] - return values[len(values) - 1] - - -def cosine_decay(global_step, learning_rate, step_each_epoch, epochs): - cur_epoch = math.floor(global_step / step_each_epoch) - decayed_lr = ( - learning_rate * 0.5 * (math.cos(cur_epoch * math.pi / epochs) + 1) - ) - return decayed_lr - - -def noam_decay(global_step, d_model, warmup_steps, learning_rate=1.0): - a = math.pow(global_step, -0.5) - b = math.pow(warmup_steps, -1.5) * global_step - decayed_lr = learning_rate * math.pow(d_model, -0.5) * min(a, b) - - return decayed_lr - - -def linear_lr_warmup(global_step, warmup_steps, start_lr, end_lr): - linear_step = end_lr - start_lr - decayed_lr = start_lr + linear_step * (global_step / warmup_steps) - return decayed_lr - - -def multi_step_decay(global_step, learning_rate, milestones, decay_rate=0.1): - for i in range(len(milestones)): - if global_step < milestones[i]: - return learning_rate * math.pow(decay_rate, i) - - return learning_rate * math.pow(decay_rate, len(milestones)) - - -def step_decay(global_step, learning_rate, step_size, decay_rate=0.1): - return learning_rate * math.pow(decay_rate, global_step // step_size) - - -def lambda_decay(global_step, learning_rate, lr_lambda): - return learning_rate * lr_lambda(global_step) - - -class TestLearningRateDecayDygraph(unittest.TestCase): - def test_LR_state_dict(self): - with base.dygraph.guard(): - x = np.random.uniform(-1, 1, [3, 10]).astype("float32") - linear = paddle.nn.Linear(10, 10) - input = paddle.to_tensor(x) - - Exponential_scheduler = paddle.optimizer.lr.ExponentialDecay( - learning_rate=0.1, - gamma=0.5, - ) - Step_scheduler = paddle.optimizer.lr.StepDecay(0.5, step_size=3) - Reducelr_scheduler = paddle.optimizer.lr.ReduceOnPlateau( - learning_rate=1.0, factor=0.5, patience=5, cooldown=3 - ) - - adam1 = paddle.optimizer.Adam( - learning_rate=Exponential_scheduler, - parameters=linear.parameters(), - ) - adam2 = paddle.optimizer.Adam( - learning_rate=Step_scheduler, parameters=linear.parameters() - ) - adam3 = paddle.optimizer.Adam( - learning_rate=Reducelr_scheduler, - parameters=linear.parameters(), - ) - print(adam3.state_dict()) - - for epoch in range(10): - out = linear(input) - loss = paddle.mean(out) - loss.backward() - adam1.minimize(loss) - adam2.minimize(loss) - adam3.minimize(loss) - linear.clear_gradients() - - Step_scheduler.get_lr() - Reducelr_scheduler.step(loss) - - paddle.save(linear.state_dict(), "save_path.pdparams") - - Exponential_scheduler_test = paddle.optimizer.lr.ExponentialDecay( - learning_rate=0.1, - gamma=0.5, - ) - Step_scheduler_test = paddle.optimizer.lr.StepDecay( - 0.5, step_size=3 - ) - Reducelr_scheduler_test = paddle.optimizer.lr.ReduceOnPlateau( - learning_rate=1.0, factor=0.5, patience=5, cooldown=3 - ) - - paddle.save(adam1.state_dict(), "save_path.pdopt") - opt_state = paddle.load("save_path.pdopt") - adam_test = paddle.optimizer.Adam( - learning_rate=Exponential_scheduler_test, - parameters=linear.parameters(), - ) - adam_test.set_state_dict(opt_state) - self.assertEqual( - adam_test._learning_rate.last_epoch, - adam1._learning_rate.last_epoch, - "last_epoch is different before and after set_state_dict", - ) - - paddle.save(adam2.state_dict(), "save_path.pdopt") - opt_state = paddle.load("save_path.pdopt") - adam_test = paddle.optimizer.Adam( - learning_rate=Step_scheduler_test, - parameters=linear.parameters(), - ) - adam_test.set_state_dict(opt_state) - self.assertEqual( - adam_test._learning_rate.last_epoch, - adam2._learning_rate.last_epoch, - "epoch_num is different before and after set_state_dict", - ) - self.assertEqual( - adam_test._learning_rate(), - adam2._learning_rate(), - "current learning rate is different before and after set_state_dict", - ) - - paddle.save(adam3.state_dict(), "save_path.pdopt") - opt_state = paddle.load("save_path.pdopt") - adam_test = paddle.optimizer.Adam( - learning_rate=Reducelr_scheduler_test, - parameters=linear.parameters(), - ) - adam_test.set_state_dict(opt_state) - self.assertEqual( - adam_test._learning_rate.best, - adam3._learning_rate.best, - "best_loss is different before and after set_state_dict", - ) - self.assertEqual( - adam_test._learning_rate.cooldown_counter, - adam3._learning_rate.cooldown_counter, - "cooldown_counter is different before and after set_state_dict", - ) - self.assertEqual( - adam_test._learning_rate.num_bad_epochs, - adam3._learning_rate.num_bad_epochs, - "num_bad_epochs is different before and after set_state_dict", - ) - self.assertEqual( - adam_test._learning_rate.last_epoch, - adam3._learning_rate.last_epoch, - "epoch is different before and after set_state_dict", - ) - self.assertEqual( - adam_test._learning_rate(), - adam3._learning_rate(), - "current learning rate is different before and after set_state_dict", - ) - - def test_NoamDecay(self): - with base.dygraph.guard(): - d_model = 0.01 - warmup_steps = 200 - learning_rate = 2.0 - lr = paddle.optimizer.lr.noam_decay( - d_model, warmup_steps, learning_rate - ) - for step in range(5): - step += 1 - right_result = noam_decay( - step, d_model, warmup_steps, learning_rate - ) - lr.step() - base_result = lr() - - self.assertAlmostEqual( - right_result, - base_result, - msg=f'Failed lr scheduler in step {step}, Python result is {right_result}, Fluid result is {base_result}', - ) - - def test_LinearLrWarmup(self): - with base.dygraph.guard(): - lr = paddle.optimizer.lr.PolynomialDecay( - learning_rate=1.0, - decay_steps=10, - end_lr=0.0, - power=1.0, - ) - lr.step() - lr = paddle.optimizer.lr.LinearWarmup( - learning_rate=lr, warmup_steps=2, start_lr=0.0, end_lr=1.0 - ) - lr.step() - right_result = [0.5, 0.9, 0.8, 0.7, 0.6] - for i in range(5): - if i == 1: - lr.step() - t = lr() - lr.step() - np.testing.assert_allclose(t, right_result[i], rtol=1e-05) - - with self.assertRaises(TypeError): - lr = paddle.optimizer.lr.linear_lr_warmup( - learning_rate="fake_lr", - warmup_steps=2, - start_lr=0.0, - end_lr=1.0, - ) - - def test_MultiStepDecay(self): - with base.dygraph.guard(): - learning_rate = 0.5 - milestones = [2, 4, 8] - decay_rate = 0.2 - linear = paddle.nn.Linear(10, 10) - - scheduler = paddle.optimizer.lr.MultiStepDecay( - learning_rate, milestones, decay_rate - ) - - adam = paddle.optimizer.Adam( - learning_rate=scheduler, parameters=linear.parameters() - ) - for epoch in range(10): - right_result = multi_step_decay( - epoch, learning_rate, milestones, decay_rate - ) - base_result = adam.get_lr() - adam.step() - scheduler.step() - self.assertAlmostEqual( - right_result, - base_result, - msg=f'Failed lr scheduler in epoch {epoch}, Python result is {right_result}, Fluid result is {base_result}', - ) - - with self.assertRaises(ValueError): - lr = paddle.optimizer.lr.MultiStepDecay( - learning_rate, [30, 50, 20], 0.1 - ) - - with self.assertRaises(ValueError): - lr = paddle.optimizer.lr.MultiStepDecay( - learning_rate, [20, 30, 50], 1 - ) - - with self.assertRaises(TypeError): - lr = paddle.optimizer.lr.MultiStepDecay("test", [20, 30, 50]) - - with self.assertRaises(ValueError): - lr = paddle.optimizer.lr.MultiStepDecay(-1, [20, 30, 50]) - - def test_StepDecay(self): - with base.dygraph.guard(): - learning_rate = 0.5 - step_size = 3 - decay_rate = 0.2 - scheduler = paddle.optimizer.lr.StepDecay( - learning_rate, step_size, decay_rate - ) - for epoch in range(10): - right_result = step_decay( - epoch, learning_rate, step_size, decay_rate - ) - base_result = scheduler() - scheduler.get_lr() - scheduler.step() - self.assertAlmostEqual( - right_result, - base_result, - msg=f'Failed lr scheduler in epoch {epoch}, Python result is {right_result}, Fluid result is {base_result}', - ) - - with self.assertRaises(TypeError): - lr = paddle.optimizer.lr.StepDecay(learning_rate, "test", 0.1) - - with self.assertRaises(ValueError): - lr = paddle.optimizer.lr.StepDecay(learning_rate, 20, 2) - - def test_LambdaDecay(self): - with base.dygraph.guard(): - learning_rate = 0.5 - lr_lambda = lambda x: 0.95**x - scheduler = paddle.optimizer.lr.LambdaDecay( - learning_rate, lr_lambda - ) - - linear = paddle.nn.Linear(10, 10) - adam = paddle.optimizer.Adam( - scheduler, parameters=linear.parameters() - ) - - for epoch in range(30): - right_result = lambda_decay(epoch, learning_rate, lr_lambda) - base_result = scheduler() - scheduler.get_lr() - scheduler.step() - self.assertAlmostEqual( - right_result, - base_result, - msg=f'Failed lr scheduler in epoch {epoch}, Python result is {right_result}, Fluid result is {base_result}', - ) - - with self.assertRaises(TypeError): - lr = paddle.optimizer.lr.LambdaDecay(learning_rate, "test") - - -class TestLearningRateDecay(unittest.TestCase): - def check_decay(self, python_decay_fn, base_decay_fn, kwargs): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for place in places: - self.check_decay_with_place( - place, python_decay_fn, base_decay_fn, kwargs - ) - - def check_decay_with_place( - self, place, python_decay_fn, base_decay_fn, kwargs - ): - main_prog = base.Program() - startup_prog = base.Program() - - with base.program_guard(main_prog, startup_prog): - decayed_lr = base_decay_fn(**kwargs) - - place = base.CPUPlace() - exe = base.Executor(place) - - exe.run(startup_prog) - - for step in range(10): - # Step of NoamDecay starts from 1. - if python_decay_fn.__name__ == 'noam_decay': - step += 1 - (lr_val,) = exe.run(main_prog, feed={}, fetch_list=[decayed_lr]) - python_decayed_lr = python_decay_fn( - global_step=float(step), **kwargs - ) - self.assertAlmostEqual( - python_decayed_lr, - lr_val[0], - places=6, - msg=f'Failed lr scheduler is {python_decay_fn.__name__}, step {step}, Python result is {python_decayed_lr}, Fluid result is {lr_val[0]}', - ) - - def test_decay(self): - common_kwargs_true = { - "learning_rate": 1.0, - "decay_steps": 5, - "decay_rate": 0.5, - "staircase": True, - } - common_kwargs_false = copy.deepcopy(common_kwargs_true) - common_kwargs_false["staircase"] = False - - decay_fns = [ - ( - exponential_decay, - paddle.optimizer.lr.exponential_decay, - common_kwargs_true, - ), - ( - exponential_decay, - paddle.optimizer.lr.exponential_decay, - common_kwargs_false, - ), - ( - natural_exp_decay, - paddle.optimizer.lr.natural_exp_decay, - common_kwargs_true, - ), - ( - natural_exp_decay, - paddle.optimizer.lr.natural_exp_decay, - common_kwargs_false, - ), - ( - inverse_time_decay, - paddle.optimizer.lr.inverse_time_decay, - common_kwargs_true, - ), - ( - inverse_time_decay, - paddle.optimizer.lr.inverse_time_decay, - common_kwargs_false, - ), - ( - polynomial_decay, - paddle.optimizer.lr.polynomial_decay, - {"learning_rate": 1.0, "decay_steps": 5, "cycle": True}, - ), - ( - polynomial_decay, - paddle.optimizer.lr.polynomial_decay, - {"learning_rate": 1.0, "decay_steps": 5, "cycle": False}, - ), - ( - piecewise_decay, - paddle.optimizer.lr.piecewise_decay, - {"boundaries": [3, 6, 9], "values": [0.1, 0.2, 0.3, 0.4]}, - ), - ( - cosine_decay, - paddle.optimizer.lr.cosine_decay, - {"learning_rate": 0.1, "step_each_epoch": 100, "epochs": 120}, - ), - ( - noam_decay, - paddle.optimizer.lr.noam_decay, - {"d_model": 0.01, "warmup_steps": 200, "learning_rate": 2.0}, - ), - ] - - for py_decay_fn, base_decay_fn, kwargs in decay_fns: - print( - "class=" - + self.__class__.__name__ - + " decay_fn=" - + py_decay_fn.__name__ - + " kwargs=" - + str(kwargs) - ) - main_program = framework.Program() - startup_program = framework.Program() - with framework.program_guard(main_program, startup_program): - self.check_decay(py_decay_fn, base_decay_fn, kwargs) - - -class TestLinearWamrupLearningRateDecay(unittest.TestCase): - def check_decay_with_place( - self, place, python_decay_fn, base_decay_fn, kwargs - ): - main_prog = base.Program() - startup_prog = base.Program() - - warmup_steps = 10 - start_lr = 0.1 / 3.0 - end_lr = 0.1 - - with base.program_guard(main_prog, startup_prog): - decayed_lr = paddle.optimizer.lr.linear_lr_warmup( - base_decay_fn(**kwargs), warmup_steps, start_lr, end_lr - ) - - place = base.CPUPlace() - exe = base.Executor(place) - exe.run(startup_prog) - - for step in range(20): - # Step of NoamDecay starts from 1. - if base_decay_fn.__name__ == 'noam_decay': - step += 1 - (lr_val,) = exe.run(main_prog, feed={}, fetch_list=[decayed_lr]) - if step < warmup_steps: - python_decayed_lr = linear_lr_warmup( - float(step), warmup_steps, start_lr, end_lr - ) - else: - python_decayed_lr = python_decay_fn( - global_step=float(step), **kwargs - ) - self.assertAlmostEqual( - python_decayed_lr, - lr_val[0], - msg=f'Test {python_decay_fn.__name__} Failed, step {step}, Python result is {python_decayed_lr}, Fluid result is {lr_val[0]}', - ) - - -class TestLinearWamrupLearningRateDecayWithScalarInput(unittest.TestCase): - def run_scalar_lr(self, place, lr, start_lr, end_lr): - main_prog = base.Program() - startup_prog = base.Program() - - warmup_steps = 10 - - with base.program_guard(main_prog, startup_prog): - decayed_lr = paddle.optimizer.lr.linear_lr_warmup( - lr, warmup_steps, start_lr, end_lr - ) - - exe = base.Executor(place) - exe.run(startup_prog) - - for step in range(20): - (lr_val,) = exe.run(main_prog, feed={}, fetch_list=[decayed_lr]) - if step < warmup_steps: - expected_lr = linear_lr_warmup( - float(step), warmup_steps, start_lr, end_lr - ) - else: - expected_lr = lr - self.assertAlmostEqual( - expected_lr, - lr_val[0], - places=6, - msg=f'Test failed, step {step}, expected {expected_lr}, but got {lr_val[0]}', - ) - - def test_scalar_lr(self): - def run_places(lr, start_lr, end_lr): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(base.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: - self.run_scalar_lr(p, lr, start_lr, end_lr) - - # float - lr = 0.2 - start_lr = 0.1 / 3.0 - end_lr = 0.2 - run_places(lr, start_lr, end_lr) - - # int end_lr - lr = 2.0 - start_lr = 0.1 / 3.0 - end_lr = 1 - run_places(lr, start_lr, end_lr) - - # int - lr = 1 - start_lr = 0 - end_lr = 1 - run_places(lr, start_lr, end_lr) - - -if __name__ == '__main__': - paddle.enable_static() - unittest.main() diff --git a/test/deprecated/legacy_test/test_math_op_patch_deprecated.py b/test/deprecated/legacy_test/test_math_op_patch_deprecated.py deleted file mode 100644 index 0f3b8e4ff306cd..00000000000000 --- a/test/deprecated/legacy_test/test_math_op_patch_deprecated.py +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest - -import numpy as np -from decorator_helper import prog_scope - -import paddle -from paddle import base -from paddle.framework import in_pir_mode - - -class TestMathOpPatches(unittest.TestCase): - @classmethod - def setUp(self): - np.random.seed(1024) - paddle.enable_static() - - @prog_scope() - def test_equal_and_cond(self): - a = paddle.static.data(name="a", shape=[-1, 1], dtype='float32') - b = paddle.static.data(name="b", shape=[-1, 1], dtype='float32') - if not in_pir_mode(): - a.desc.set_need_check_feed(False) - b.desc.set_need_check_feed(False) - one = paddle.ones(shape=[1], dtype='int32') - zero = paddle.zeros(shape=[1], dtype='int32') - cond = one == zero - c = paddle.static.nn.cond(cond, lambda: a + b, lambda: a - b) - - place = base.CPUPlace() - exe = base.Executor(place) - a_np = np.array([3, 4, 10, 14, 9, 18]).astype('float32') - b_np = np.array([3, 4, 11, 15, 8, 18]).astype('float32') - - (c_np,) = exe.run( - paddle.static.default_main_program(), - feed={"a": a_np, "b": b_np}, - fetch_list=[c], - ) - - np.testing.assert_array_equal(c_np, a_np - b_np) - - -if __name__ == '__main__': - unittest.main() diff --git a/test/deprecated/legacy_test/test_optimizer_in_control_flow_deprecated.py b/test/deprecated/legacy_test/test_optimizer_in_control_flow_deprecated.py deleted file mode 100644 index 997a7e1a88df3b..00000000000000 --- a/test/deprecated/legacy_test/test_optimizer_in_control_flow_deprecated.py +++ /dev/null @@ -1,250 +0,0 @@ -# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest - -import numpy as np - -import paddle -from paddle import base -from paddle.base import core -from paddle.base.framework import Program, program_guard - -BATCH_SIZE = 1 -INPUT_SIZE = 784 -CLASS_NUM = 10 -FC_SIZE = 40 -EPOCH_NUM = 5 -LR = 0.001 -SEED = 2020 - -paddle.enable_static() - - -def static( - train_data, loss_in_switch=True, use_cuda=False, use_parallel_exe=False -): - startup_program = Program() - main_program = Program() - paddle.seed(SEED) - - with program_guard(main_program, startup_program): - - def double_fc_net(image): - hidden = paddle.static.nn.fc( - image, - size=FC_SIZE, - activation='relu', - weight_attr=base.ParamAttr( - initializer=paddle.nn.initializer.Constant(value=0.99) - ), - bias_attr=base.ParamAttr( - initializer=paddle.nn.initializer.Constant(value=0.5) - ), - name="hidden", - ) - - prediction = paddle.static.nn.fc( - hidden, - size=CLASS_NUM, - activation='softmax', - weight_attr=base.ParamAttr( - initializer=paddle.nn.initializer.Constant(value=1.2) - ), - bias_attr=base.ParamAttr( - initializer=paddle.nn.initializer.Constant(value=0.8) - ), - name="prediction", - ) - return hidden, prediction - - def fn_1(opt, avg_loss=None, pred=None, label=None): - if avg_loss is None: - loss = paddle.nn.functional.cross_entropy( - input=pred, label=label, reduction='none', use_softmax=False - ) - avg_loss = paddle.mean(loss, name='mean_cross_entropy_loss') - opt.minimize(avg_loss) - return avg_loss - - def fn_2(opt, avg_loss=None, pred=None, label=None): - if avg_loss is None: - loss = paddle.nn.functional.softmax_with_cross_entropy( - logits=pred, label=label - ) - avg_loss = paddle.mean(loss, name='mean_softmax_loss') - opt.minimize(avg_loss) - return avg_loss - - image = paddle.static.data('image', [BATCH_SIZE, INPUT_SIZE], 'float32') - label = paddle.static.data('label', [BATCH_SIZE, 1], 'int64') - hidden, prediction = double_fc_net(image) - - adam = paddle.optimizer.Adam(learning_rate=LR) - sgd = paddle.optimizer.SGD(learning_rate=LR) - - id = paddle.static.data('id', [1], 'int32') - two = paddle.tensor.fill_constant([1], 'int32', 2) - mod_two = paddle.remainder(id, two) == 0 - - if loss_in_switch: - avg_loss = paddle.static.nn.case( - [(mod_two, lambda: fn_1(adam, None, prediction, label))], - lambda: fn_2(sgd, None, prediction, label), - ) - else: - loss_1 = paddle.nn.functional.cross_entropy( - input=prediction, - label=label, - reduction='none', - use_softmax=False, - ) - avg_loss_1 = paddle.mean(loss_1) - loss_2 = paddle.nn.functional.softmax_with_cross_entropy( - logits=prediction, label=label - ) - avg_loss_2 = paddle.mean(loss_2) - avg_loss = paddle.static.nn.case( - [(mod_two, lambda: fn_1(adam, avg_loss_1))], - lambda: fn_2(sgd, avg_loss_2), - ) - - place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() - exe = base.Executor(place) - exe.run(startup_program) - - for epoch in range(EPOCH_NUM): - feed_image, feed_label = train_data[epoch] - fetch_list = [hidden, prediction, avg_loss] - feed = { - 'image': feed_image, - 'label': feed_label, - 'id': np.array([epoch]).astype('int32'), - } - out = exe.run(main_program, feed=feed, fetch_list=fetch_list) - out_hidden, out_pred, loss = out - - return out_hidden, out_pred, loss - - -class DygraphLayer(paddle.nn.Layer): - def __init__(self): - super().__init__() - self.fc_1 = paddle.nn.Linear( - INPUT_SIZE, - FC_SIZE, - weight_attr=paddle.ParamAttr( - initializer=paddle.nn.initializer.Constant(value=0.99) - ), - bias_attr=paddle.ParamAttr( - initializer=paddle.nn.initializer.Constant(value=0.5) - ), - ) - self.act_1 = paddle.nn.ReLU() - self.fc_2 = paddle.nn.Linear( - FC_SIZE, - CLASS_NUM, - weight_attr=paddle.ParamAttr( - initializer=paddle.nn.initializer.Constant(value=1.2) - ), - bias_attr=paddle.ParamAttr( - initializer=paddle.nn.initializer.Constant(value=0.8) - ), - ) - - self.act_2 = paddle.nn.Softmax() - - def forward(self, inputs): - hidden = self.fc_1(inputs) - prediction = self.fc_2(hidden) - return self.act_1(hidden), self.act_2(prediction) - - -def dynamic(train_data, use_cuda=False, use_parallel_exe=False): - place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() - with base.dygraph.guard(place): - paddle.seed(SEED) - dy_layer = DygraphLayer() - adam = paddle.optimizer.Adam( - learning_rate=LR, parameters=dy_layer.parameters() - ) - sgd = paddle.optimizer.SGD( - learning_rate=LR, parameters=dy_layer.parameters() - ) - - for epoch in range(EPOCH_NUM): - image_data, label = train_data[epoch] - var_input = paddle.to_tensor(image_data) - var_label = paddle.to_tensor(label) - hidden, prediction = dy_layer(var_input) - - if epoch % 2 == 0: - cross_entropy_loss = paddle.nn.functional.cross_entropy( - prediction, var_label, reduction='none', use_softmax=False - ) - loss = paddle.mean(cross_entropy_loss) - loss.backward() - adam.minimize(loss) - else: - softmax_loss = paddle.nn.functional.softmax_with_cross_entropy( - prediction, var_label - ) - loss = paddle.mean(softmax_loss) - loss.backward() - sgd.minimize(loss) - - dy_layer.clear_gradients() - return hidden.numpy(), prediction.numpy(), loss.numpy() - - -class TestMultiTask(unittest.TestCase): - ''' - Compare results of static graph and dynamic graph. - Todo(liym27): add parallel GPU train. - ''' - - def random_input( - self, - seed, - image_shape=[BATCH_SIZE, INPUT_SIZE], - label_shape=[BATCH_SIZE, 1], - ): - np.random.seed(seed) - image_np = np.random.random(size=image_shape).astype('float32') - np.random.seed(seed) - label_np = np.random.randint( - low=0, high=CLASS_NUM - 1, size=label_shape - ).astype('int64') - return image_np, label_np - - def init_train_data(self): - self.train_data = [] - for epoch in range(EPOCH_NUM): - self.train_data.append(self.random_input(epoch)) - - def test_optimizer_in_switch(self): - self.init_train_data() - use_cuda = core.is_compiled_with_cuda() - hidden_2, pre_2, loss_2 = dynamic(self.train_data, use_cuda) - for loss_in_switch in [True, False]: - hidden_1, pre_1, loss_1 = static( - self.train_data, loss_in_switch, use_cuda - ) - np.testing.assert_allclose(hidden_1, hidden_2, rtol=1e-05) - np.testing.assert_allclose(pre_1, pre_2, rtol=1e-05) - np.testing.assert_allclose(loss_1, loss_2, rtol=1e-05) - - -if __name__ == '__main__': - unittest.main() diff --git a/test/deprecated/legacy_test/test_program_code_deprecated.py b/test/deprecated/legacy_test/test_program_code_deprecated.py deleted file mode 100644 index 86979038a0a28d..00000000000000 --- a/test/deprecated/legacy_test/test_program_code_deprecated.py +++ /dev/null @@ -1,74 +0,0 @@ -# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest - -import paddle -from paddle import base - - -class TestProgramToReadableCode(unittest.TestCase): - def setUp(self): - self.program = base.Program() - self.block = self.program.current_block() - self.var = self.block.create_var( - name="X", shape=[-1, 23, 48], dtype='float32' - ) - self.param = self.block.create_parameter( - name="W", shape=[23, 48], dtype='float32', trainable=True - ) - self.op = self.block.append_op( - type="abs", inputs={"X": [self.var]}, outputs={"Out": [self.var]} - ) - # add control flow op and sub block - self.append_cond_op(self.program) - - def append_cond_op(self, program): - def true_func(): - return paddle.tensor.fill_constant( - shape=[2, 3], dtype='int32', value=2 - ) - - def false_func(): - return paddle.tensor.fill_constant( - shape=[3, 2], dtype='int32', value=-1 - ) - - with base.program_guard(program): - x = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.1 - ) - y = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.23 - ) - pred = paddle.less_than(y, x) - out = paddle.static.nn.cond(pred, true_func, false_func) - - def test_program_code(self): - self.var._to_readable_code() - self.param._to_readable_code() - self.op._to_readable_code() - self.block._to_readable_code() - self.program._to_readable_code() - - def test_program_print(self): - print(self.var) - print(self.param) - print(self.op) - print(self.block) - print(self.program) - - -if __name__ == "__main__": - unittest.main() diff --git a/test/deprecated/legacy_test/test_program_prune_backward_deprecated.py b/test/deprecated/legacy_test/test_program_prune_backward_deprecated.py deleted file mode 100755 index b7fc83d4dee0c2..00000000000000 --- a/test/deprecated/legacy_test/test_program_prune_backward_deprecated.py +++ /dev/null @@ -1,592 +0,0 @@ -# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import contextlib -import os -import unittest - -import numpy as np -import seresnext_net -import transformer_model -from feed_data_reader import FeedDataReader -from simple_nets import fc_with_batchnorm, init_data, simple_fc_net - -import paddle -from paddle import base -from paddle.base import core -from paddle.dataset import wmt16 - -paddle.enable_static() - -DeviceType = core.DeviceType - - -class ModelHyperParams: - # Dictionary size for source and target language. This model directly uses - # paddle.dataset.wmt16 in which , and token has - # already been added, but the token is not added. Transformer requires - # sequences in a mini-batch are padded to have the same length. A token is - # added into the original dictionary in paddle.dataset.wmt16. - - # size of source word dictionary. - src_vocab_size = 10000 - # index for token in source language. - src_pad_idx = src_vocab_size - - # size of target word dictionary - trg_vocab_size = 10000 - # index for token in target language. - trg_pad_idx = trg_vocab_size - - # position value corresponding to the token. - pos_pad_idx = 0 - - # max length of sequences. It should plus 1 to include position - # padding token for position encoding. - max_length = 50 - - # the dimension for word embeddings, which is also the last dimension of - # the input and output of multi-head attention, position-wise feed-forward - # networks, encoder and decoder. - - d_model = 512 - # size of the hidden layer in position-wise feed-forward networks. - d_inner_hid = 1024 - # the dimension that keys are projected to for dot-product attention. - d_key = 64 - # the dimension that values are projected to for dot-product attention. - d_value = 64 - # number of head used in multi-head attention. - n_head = 8 - # number of sub-layers to be stacked in the encoder and decoder. - # NOTE(zcd): the origin number of layer is 6, to make this unit test faster, - # we should reduce the layer number to 4. - n_layer = 4 - # dropout rate used by all dropout layers. - dropout = 0.1 - - -def prepare_batch_input(insts, src_pad_idx, trg_pad_idx, n_head): - """ - Pad the instances to the max sequence length in batch, and generate the - corresponding position data and attention bias. Then, convert the numpy - data to tensors and return a dict mapping names to tensors. - """ - - def __pad_batch_data( - insts, - pad_idx, - is_target=False, - return_pos=True, - return_attn_bias=True, - return_max_len=True, - ): - """ - Pad the instances to the max sequence length in batch, and generate the - corresponding position data and attention bias. - """ - return_list = [] - max_len = max(len(inst) for inst in insts) - inst_data = np.array( - [inst + [pad_idx] * (max_len - len(inst)) for inst in insts] - ) - return_list += [inst_data.astype("int64").reshape([-1, 1])] - if return_pos: - inst_pos = np.array( - [ - [ - pos_i + 1 if w_i != pad_idx else 0 - for pos_i, w_i in enumerate(inst) - ] - for inst in inst_data - ] - ) - - return_list += [inst_pos.astype("int64").reshape([-1, 1])] - if return_attn_bias: - if is_target: - # This is used to avoid attention on paddings and subsequent - # words. - slf_attn_bias_data = np.ones( - (inst_data.shape[0], max_len, max_len) - ) - slf_attn_bias_data = np.triu(slf_attn_bias_data, 1).reshape( - [-1, 1, max_len, max_len] - ) - slf_attn_bias_data = np.tile( - slf_attn_bias_data, [1, n_head, 1, 1] - ) * [-1e9] - else: - # This is used to avoid attention on paddings. - slf_attn_bias_data = np.array( - [ - [0] * len(inst) + [-1e9] * (max_len - len(inst)) - for inst in insts - ] - ) - slf_attn_bias_data = np.tile( - slf_attn_bias_data.reshape([-1, 1, 1, max_len]), - [1, n_head, max_len, 1], - ) - return_list += [slf_attn_bias_data.astype("float32")] - if return_max_len: - return_list += [max_len] - return return_list if len(return_list) > 1 else return_list[0] - - src_word, src_pos, src_slf_attn_bias, src_max_len = __pad_batch_data( - [inst[0] for inst in insts], src_pad_idx, is_target=False - ) - trg_word, trg_pos, trg_slf_attn_bias, trg_max_len = __pad_batch_data( - [inst[1] for inst in insts], trg_pad_idx, is_target=True - ) - trg_src_attn_bias = np.tile( - src_slf_attn_bias[:, :, ::src_max_len, :], [1, 1, trg_max_len, 1] - ).astype("float32") - lbl_word = __pad_batch_data( - [inst[2] for inst in insts], trg_pad_idx, False, False, False, False - ) - lbl_weight = (lbl_word != trg_pad_idx).astype("float32").reshape([-1, 1]) - - return [ - src_word, - src_pos, - trg_word, - trg_pos, - src_slf_attn_bias, - trg_slf_attn_bias, - trg_src_attn_bias, - lbl_word, - lbl_weight, - ] - - -feed_data_reader = None - - -def transformer(use_feed): - assert not use_feed, "transformer doesn't support feed yet" - return transformer_model.transformer( - ModelHyperParams.src_vocab_size + 1, - ModelHyperParams.trg_vocab_size + 1, - ModelHyperParams.max_length + 1, - ModelHyperParams.n_layer, - ModelHyperParams.n_head, - ModelHyperParams.d_key, - ModelHyperParams.d_value, - ModelHyperParams.d_model, - ModelHyperParams.d_inner_hid, - ModelHyperParams.dropout, - ModelHyperParams.src_pad_idx, - ModelHyperParams.trg_pad_idx, - ModelHyperParams.pos_pad_idx, - ) - - -def get_feed_data_reader(): - global feed_data_reader - if feed_data_reader is not None: - return feed_data_reader - - reader = paddle.batch( - wmt16.train( - ModelHyperParams.src_vocab_size, ModelHyperParams.trg_vocab_size - ), - batch_size=transformer_model.batch_size, - ) - all_batch_tensors = [] - for batch in reader(): - tensors = [] - for tensor in prepare_batch_input( - batch, - ModelHyperParams.src_pad_idx, - ModelHyperParams.trg_pad_idx, - ModelHyperParams.n_head, - ): - tensors.append(np.array(tensor)) - all_batch_tensors.append(tensors) - - def __reader__(): - yield from all_batch_tensors - - feed_data_reader = FeedDataReader( - feed_list=transformer_model.build_inputs( - ModelHyperParams.max_length + 1, ModelHyperParams.n_head - ), - reader=__reader__, - ) - - return feed_data_reader - - -def simple_fc_net_with_accuracy(use_feed): - img = paddle.static.data(name='image', shape=[-1, 784], dtype='float32') - label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64') - - hidden = img - for _ in range(4): - hidden = paddle.static.nn.fc( - hidden, - size=200, - activation='relu', - bias_attr=base.ParamAttr( - initializer=paddle.nn.initializer.Constant(value=1.0) - ), - ) - prediction = paddle.static.nn.fc(hidden, size=10, activation='softmax') - loss = paddle.nn.functional.cross_entropy( - input=prediction, label=label, reduction='none', use_softmax=False - ) - loss = paddle.mean(loss) - accuracy_out = paddle.static.accuracy(input=prediction, label=label, k=5) - return loss - - -def cond_net(use_feed=None): - x = paddle.static.data(name="x", shape=[-1, 4], dtype='float32') - label = paddle.static.data('label', shape=[-1, 1], dtype='int64') - prediction = paddle.static.nn.fc(x, size=1, activation=None) - - def loss1(pred, label): - x = paddle.static.data(name="x", shape=[-1, 4], dtype='float32') - loss = paddle.nn.functional.cross_entropy( - input=pred, label=label, reduction='none', use_softmax=False - ) - avg_loss = paddle.mean(loss, name='mean_cross_entropy_loss') - return avg_loss - - def loss2(pred, label): - loss = paddle.nn.functional.softmax_with_cross_entropy( - logits=pred, label=label - ) - avg_loss = paddle.mean(loss, name='mean_softmax_loss') - return avg_loss - - two = paddle.tensor.fill_constant([1], 'int32', 2) - pred = two == 0 - avg_loss = paddle.static.nn.case( - [(pred, lambda: loss1(prediction, label))], - lambda: loss2(prediction, label), - ) - return avg_loss - - -def pylayer_net(use_feed=None): - x = paddle.static.data(name="x", shape=[-1, 4], dtype='float32') - label = paddle.static.data('label', shape=[-1, 1], dtype='int64') - - def forward_fn(x): - y = 3 * x - return y - - def backward_fn(dy): - grad = paddle.exp(dy) - return grad - - y = paddle.static.nn.static_pylayer(forward_fn, [x], backward_fn) - hidden = paddle.static.nn.fc(x=[y], size=4, activation="softmax") - loss = paddle.nn.functional.cross_entropy( - input=hidden, label=label, reduction='none', use_softmax=False - ) - loss = paddle.mean(loss, name='mean_softmax_loss') - return loss - - -def optimization_in_cond_net(with_optimize=False): - x = paddle.static.data(name="x", shape=[-1, 4], dtype='float32') - label = paddle.static.data('label', shape=[-1, 1], dtype='int64') - prediction = paddle.static.nn.fc(x, size=1, activation=None) - - def loss1(opt, pred, label, with_optimize): - x = paddle.static.data(name="x", shape=[-1, 4], dtype='float32') - loss = paddle.nn.functional.cross_entropy( - input=pred, label=label, reduction='none', use_softmax=False - ) - avg_loss = paddle.mean(loss, name='mean_cross_entropy_loss') - if with_optimize: - opt.minimize(avg_loss) - return avg_loss - - def loss2(opt, pred, label, with_optimize): - loss = paddle.nn.functional.softmax_with_cross_entropy( - logits=pred, label=label - ) - avg_loss = paddle.mean(loss, name='mean_softmax_loss') - if with_optimize: - opt.minimize(avg_loss) - return avg_loss - - sgd = paddle.optimizer.SGD(learning_rate=0.1) - two = paddle.tensor.fill_constant([1], 'int32', 2) - pred = two == 0 - avg_loss = paddle.static.nn.case( - [(pred, lambda: loss1(sgd, prediction, label, with_optimize))], - lambda: loss2(sgd, prediction, label, with_optimize), - ) - return avg_loss - - -def optimization_in_pylayer_net(with_optimize=False): - x = paddle.static.data(name="x", shape=[-1, 4], dtype='float32') - label = paddle.static.data('label', shape=[-1, 1], dtype='int64') - - def forward_fn(x): - y = 3 * x - return y - - def backward_fn(dy): - grad = paddle.exp(dy) - return grad - - y = paddle.static.nn.static_pylayer(forward_fn, [x], backward_fn) - hidden = 3 * y - loss = paddle.nn.functional.softmax_with_cross_entropy( - logits=hidden, label=label - ) - loss = paddle.mean(loss, name='mean_softmax_loss') - sgd = paddle.optimizer.SGD(learning_rate=0.1) - if with_optimize: - sgd.minimize(loss) - - return loss - - -class TestProgramPruneBackward(unittest.TestCase): - def program_compare(self, program_a, program_b): - assert isinstance( - program_a, base.framework.Program - ), "The first argument should be base.framework.Program." - assert isinstance( - program_b, base.framework.Program - ), "The second argument should be base.framework Program." - - self.assertEqual(len(program_a.blocks), len(program_b.blocks)) - for idx in range(len(program_a.blocks)): - block_a = program_a.blocks[idx] - block_b = program_b.blocks[idx] - self.assertEqual(len(block_a.ops), len(block_b.ops)) - self.assertEqual(len(block_a.vars), len(block_b.vars)) - for op_idx in range(len(block_a.ops)): - self.assertEqual( - block_a.ops[op_idx].type, block_b.ops[op_idx].type - ) - for var_key in list(block_a.vars.keys()): - self.assertTrue(block_b.has_var(var_key)) - - def check_prune_correctness(self, method, feed_dict, optimizer): - loss = method(use_feed=False) - - main_program = base.default_main_program() - test_prog_orig = main_program.clone(for_test=True) - optimizer().minimize(loss) - test_prog_prune = main_program.clone(for_test=True) - - self.program_compare(test_prog_orig, test_prog_prune) - - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - ): - places.append(core.CPUPlace()) - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - - for place in places: - exe = base.Executor(place) - exe.run(base.default_startup_program()) - - (loss_data_prune,) = exe.run( - test_prog_prune, feed=feed_dict, fetch_list=[loss] - ) - (loss_data_orig,) = exe.run( - test_prog_orig, feed=feed_dict, fetch_list=[loss] - ) - self.assertEqual(loss_data_orig, loss_data_prune) - - def test_simple_fc_net(self): - def optimizer(): - optimizer = paddle.optimizer.SGD( - learning_rate=0.001, - weight_decay=paddle.regularizer.L2Decay(1e-4), - ) - return optimizer - - with self.program_scope_guard(): - img, label = init_data() - self.check_prune_correctness( - method=simple_fc_net, - feed_dict={"image": img, "label": label}, - optimizer=optimizer, - ) - - def test_simple_fc_net_with_accuracy(self): - def optimizer(): - optimizer = paddle.optimizer.SGD( - learning_rate=0.001, - weight_decay=paddle.regularizer.L2Decay(1e-4), - ) - return optimizer - - with self.program_scope_guard(): - img, label = init_data() - self.check_prune_correctness( - method=simple_fc_net_with_accuracy, - feed_dict={"image": img, "label": label}, - optimizer=optimizer, - ) - - def test_batchnorm_fc(self): - def optimizer(): - optimizer = paddle.optimizer.SGD( - learning_rate=0.001, - weight_decay=paddle.regularizer.L2Decay(1e-4), - ) - return optimizer - - with self.program_scope_guard(): - img, label = init_data() - self.check_prune_correctness( - method=fc_with_batchnorm, - feed_dict={"image": img, "label": label}, - optimizer=optimizer, - ) - - def test_seresnet(self): - with self.program_scope_guard(): - self.check_prune_correctness( - method=seresnext_net.model, - feed_dict=seresnext_net.feed_dict(use_device=DeviceType.CPU), - optimizer=seresnext_net.optimizer, - ) - - def test_transformer(self): - def optimizer(): - optimizer = paddle.optimizer.Adam( - learning_rate=0.001, - weight_decay=paddle.regularizer.L2Decay(1e-4), - ) - return optimizer - - with self.program_scope_guard(): - # the program argument is used to distinguish Program and CompiledProgram - feed_dict = get_feed_data_reader().get_next( - base.Executor(core.CPUPlace()), base.default_main_program() - ) - self.check_prune_correctness( - method=transformer, feed_dict=feed_dict, optimizer=optimizer - ) - - def test_cond(self): - def optimizer(): - optimizer = paddle.optimizer.SGD(learning_rate=0.01) - return optimizer - - with self.program_scope_guard(): - x_in = np.random.random(size=(10, 4)).astype('float32') - label_in = np.random.randint(1, size=(10, 1)).astype('int64') - feed_dict = {'x': x_in, 'label': label_in} - self.check_prune_correctness( - method=cond_net, feed_dict=feed_dict, optimizer=optimizer - ) - - def test_pylayer(self): - def optimizer(): - optimizer = paddle.optimizer.SGD(learning_rate=0.01) - return optimizer - - with self.program_scope_guard(): - x_in = np.random.random(size=(10, 4)).astype('float32') - label_in = np.random.randint(1, size=(10, 1)).astype('int64') - feed_dict = {'x': x_in, 'label': label_in} - self.check_prune_correctness( - method=pylayer_net, feed_dict=feed_dict, optimizer=optimizer - ) - - def test_optimization_in_cond(self): - x_in = np.random.random(size=(10, 4)).astype('float32') - label_in = np.random.randint(1, size=(10, 1)).astype('int64') - feed_dict = {'x': x_in, 'label': label_in} - with self.program_scope_guard(): - loss = optimization_in_cond_net(False) - main_program = base.default_main_program() - test_prog_orig = main_program.clone(for_test=True) - place = core.CPUPlace() - exe = base.Executor(place) - exe.run(base.default_startup_program()) - (loss_data_orig,) = exe.run( - test_prog_orig, feed=feed_dict, fetch_list=[loss] - ) - - with self.program_scope_guard(): - loss = optimization_in_cond_net(True) - main_program = base.default_main_program() - test_prog_prune = main_program.clone(for_test=True) - - place = core.CPUPlace() - exe = base.Executor(place) - exe.run(base.default_startup_program()) - (loss_data_prune,) = exe.run( - test_prog_prune, feed=feed_dict, fetch_list=[loss] - ) - - self.program_compare(test_prog_orig, test_prog_prune) - self.assertEqual(loss_data_orig, loss_data_prune) - - def test_optimization_in_pylayer(self): - x_in = np.random.random(size=(10, 4)).astype('float32') - label_in = np.random.randint(1, size=(10, 1)).astype('int64') - feed_dict = {'x': x_in, 'label': label_in} - with self.program_scope_guard(): - loss = optimization_in_pylayer_net(False) - main_program = base.default_main_program() - test_prog_orig = main_program.clone(for_test=True) - place = core.CPUPlace() - exe = base.Executor(place) - exe.run(base.default_startup_program()) - (loss_data_orig,) = exe.run( - test_prog_orig, feed=feed_dict, fetch_list=[loss] - ) - - with self.program_scope_guard(): - loss = optimization_in_pylayer_net(True) - main_program = base.default_main_program() - test_prog_prune = main_program.clone(for_test=True) - - place = core.CPUPlace() - exe = base.Executor(place) - exe.run(base.default_startup_program()) - (loss_data_prune,) = exe.run( - test_prog_prune, feed=feed_dict, fetch_list=[loss] - ) - - self.program_compare(test_prog_orig, test_prog_prune) - self.assertEqual(loss_data_orig, loss_data_prune) - - @contextlib.contextmanager - def program_scope_guard(self): - prog = base.Program() - startup_prog = base.Program() - scope = base.core.Scope() - with ( - base.scope_guard(scope), - base.program_guard(prog, startup_prog), - base.unique_name.guard(), - ): - yield - - -if __name__ == '__main__': - paddle.enable_static() - unittest.main() diff --git a/test/deprecated/legacy_test/test_static_pylayer_deprecated.py b/test/deprecated/legacy_test/test_static_pylayer_deprecated.py deleted file mode 100644 index e29f5762aca6ef..00000000000000 --- a/test/deprecated/legacy_test/test_static_pylayer_deprecated.py +++ /dev/null @@ -1,751 +0,0 @@ -# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import functools -import sys -import unittest - -sys.path.append(".") -import numpy as np -from test_prune_deprecated import ( - TestExecutorRunAutoPrune, - TestPruneBase, -) - -import paddle -from paddle import base -from paddle.base import core -from paddle.base.backward import append_backward - -np.random.seed(123) - - -class TestStaticPyLayerInputOutput(unittest.TestCase): - def setUp(self): - paddle.enable_static() - - def test_return_single_var(self): - """ - pseudocode: - - y = 3 * x - """ - - def forward_fn(x): - return 3 * x - - main_program = paddle.static.Program() - start_program = paddle.static.Program() - with paddle.static.program_guard(main_program, start_program): - data = paddle.static.data(name="X", shape=[1], dtype="float32") - out = paddle.static.nn.static_pylayer(forward_fn, [data]) - - place = ( - base.CUDAPlace(0) - if core.is_compiled_with_cuda() - else base.CPUPlace() - ) - exe = base.Executor(place) - x = np.array([2.0], dtype=np.float32) - (ret,) = exe.run(main_program, feed={"X": x}, fetch_list=[out]) - np.testing.assert_allclose( - np.asarray(ret), np.array([6.0], np.float32), rtol=1e-05 - ) - - # NOTE: Users should not be able to return none when actually using it. - - def test_return_0d_tensor(self): - """ - pseudocode: - - y = 3 * x - """ - - def forward_fn(x): - return 3 * x - - main_program = paddle.static.Program() - start_program = paddle.static.Program() - with paddle.static.program_guard(main_program, start_program): - data = paddle.full(shape=[], dtype='float32', fill_value=2.0) - out = paddle.static.nn.static_pylayer(forward_fn, [data]) - - place = ( - base.CUDAPlace(0) - if core.is_compiled_with_cuda() - else base.CPUPlace() - ) - exe = base.Executor(place) - (ret,) = exe.run(main_program, fetch_list=[out]) - np.testing.assert_allclose( - np.asarray(ret), np.array(6.0, np.float32), rtol=1e-05 - ) - self.assertEqual(ret.shape, ()) - - def test_0d_tensor_backward(self): - ''' - pseudocode: - - y = 3 * x - dx = -5 * dy - ''' - - def forward_fn(x): - return 3 * x - - def backward_fn(dy): - return -5 * dy - - main_program = paddle.static.Program() - start_program = paddle.static.Program() - with paddle.static.program_guard(main_program, start_program): - data = paddle.full(shape=[], dtype='float32', fill_value=-2.0) - data.stop_gradient = False - out = paddle.static.nn.static_pylayer( - forward_fn, [data], backward_fn - ) - grad_list = append_backward(out, [data]) - - place = ( - base.CUDAPlace(0) - if core.is_compiled_with_cuda() - else base.CPUPlace() - ) - exe = base.Executor(place) - - if paddle.framework.in_pir_mode(): - for p, g in grad_list: - if p.is_same(data): - data_grad = g - ret, x_grad = exe.run( - main_program, - fetch_list=[out, data_grad], - ) - else: - ret, x_grad = exe.run( - main_program, - fetch_list=[out.name, data.grad_name], - ) - - np.testing.assert_allclose(np.asarray(ret), np.array(-6.0), rtol=1e-05) - self.assertEqual(ret.shape, ()) - - np.testing.assert_allclose( - np.asarray(x_grad), np.array(-5.0), rtol=1e-05 - ) - self.assertEqual(x_grad.shape, ()) - - def test_return_var_type(self): - def forward_fn(a, b): - return 3 * a, -2 * b - - main_program = paddle.static.Program() - start_program = paddle.static.Program() - with paddle.static.program_guard(main_program, start_program): - data_1 = paddle.full(shape=[2, 4], dtype='float32', fill_value=-2.0) - data_2 = paddle.full(shape=[4, 5], dtype='float32', fill_value=10.0) - out_1, out_2 = paddle.static.nn.static_pylayer( - forward_fn, [data_1, data_2] - ) - - place = ( - base.CUDAPlace(0) - if core.is_compiled_with_cuda() - else base.CPUPlace() - ) - exe = base.Executor(place) - ret_1, ret_2 = exe.run(main_program, fetch_list=[out_1, out_2]) - np.testing.assert_allclose( - np.asarray(ret_1), - np.full((2, 4), -6.0, dtype=np.float32), - rtol=1e-05, - ) - - np.testing.assert_allclose( - np.asarray(ret_2), - np.full((4, 5), -20.0, dtype=np.float32), - rtol=1e-05, - ) - - def test_return_forward_none(self): - input_shape = (1, 3) - - def forward_fn(x): - y = 3 * x - - main_program = paddle.static.Program() - start_program = paddle.static.Program() - with paddle.static.program_guard(main_program, start_program): - data = paddle.full( - shape=input_shape, dtype='float32', fill_value=-2.0 - ) - out = paddle.static.nn.static_pylayer(forward_fn, [data]) - - place = ( - base.CUDAPlace(0) - if core.is_compiled_with_cuda() - else base.CPUPlace() - ) - exe = base.Executor(place) - exe.run(main_program) - self.assertIsNone(out) - - def test_wrong_structure_exception(self): - """ - test not all ``stop_gradient`` of inputs is True when ``backward_fn`` is None, and - wrong number of inputs and outputs returned by ``forward_fn`` and ``backward_fn`` - """ - - def forward_fn(a, b): - return 3 * a, -b, paddle.mean(b) - - def backward_fn(daout, dbout): - return 3 * daout, -dbout - - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - with paddle.static.program_guard(main_program, startup_program): - data_1 = paddle.static.data( - name="data_1", shape=[2, 4], dtype="float32" - ) - data_2 = paddle.static.data( - name="data_2", shape=[6], dtype="float32" - ) - data_2.stop_gradient = False - with self.assertRaises(ValueError) as e: - out = paddle.static.nn.static_pylayer( - forward_fn, [data_1, data_2], backward_fn=None - ) - self.assertTrue( - "``stop_gradient`` attr of all inputs to ``forward_fn`` are expected to be True, when ``backward_fn == None``" - in str(e.exception) - ) - - with self.assertRaises(TypeError) as e: - out = paddle.static.nn.static_pylayer( - forward_fn, [data_1, data_2], backward_fn=backward_fn - ) - append_backward(out, [data_1, data_2]) - - -class TestControlFlowNestedStaticPyLayer(unittest.TestCase): - def setUp(self): - paddle.enable_static() - - def test_cond_inside_static_pylayer(self): - """ - forward propagation: - _ _ _ _ _ _ _ _ - ---> a ---> | | -----> out_a ------ - | | StaticPyLayer | | - i ---------> |_ _ _ _ _ _ _ _| -----> out_i ---> out ---> loss - - - pseudocode: - def forward_fn(i, a): - if i < 5: - return i, a + a - else: - return i, a - a - - def backward_fn(diout, daout): - daout_scaled = daout * 3.0 - if diout < 5: - return daout_scaled, -1 * daout - else: - return daout_scaled, daout * daout - """ - - def forward_fn(i, a): - return i, paddle.static.nn.cond( - i < 5.0, lambda: paddle.add(a, a), lambda: paddle.subtract(a, a) - ) - - def backward_fn(diout, daout): - daout_scale = daout * 3.0 - return daout_scale, paddle.static.nn.cond( - diout < 5.0, - lambda: -1 * daout, - lambda: daout * daout, - ) - - main_program = paddle.static.Program() - start_program = paddle.static.Program() - with paddle.static.program_guard(main_program, start_program): - i = paddle.static.data(name="i", shape=[1], dtype="float32") - i.stop_gradient = False - a = 2.0 * i - out_i, out_a = paddle.static.nn.static_pylayer( - forward_fn, [i, a], backward_fn - ) - out = out_i + out_a - loss = paddle.exp(out) - grad_list = append_backward(loss, [i, a, out_i, out_a, out]) - - place = ( - base.CUDAPlace(0) - if core.is_compiled_with_cuda() - else base.CPUPlace() - ) - exe = base.Executor(place) - for feed_i in range(0, 10): - expected_a = 2.0 * feed_i - if feed_i < 5: - expected_out_i = feed_i - expected_out_a = expected_a + expected_a - expected_out = expected_out_a + expected_out_i - expected_out_grad = np.exp(expected_out) - else: - expected_out_i = feed_i - expected_out_a = expected_a - expected_a - expected_out = expected_out_a + expected_out_i - expected_out_grad = np.exp(expected_out) - - if expected_out_grad < 5: - expected_a_grad = -1 * expected_out_grad - expected_i_grad = 3 * expected_out_grad + 2 * expected_a_grad - else: - expected_a_grad = expected_out_grad * expected_out_grad - expected_i_grad = 3 * expected_out_grad + 2 * expected_a_grad - - if paddle.framework.in_pir_mode(): - out_grad = None - out_i_grad = None - out_a_grad = None - a_grad = None - i_grad = None - - for p, g in grad_list: - if p.is_same(out_i): - out_i_grad = g - elif p.is_same(out_a): - out_a_grad = g - elif p.is_same(a): - a_grad = g - elif p.is_same(i): - i_grad = g - elif p.is_same(out): - out_grad = g - - ret = exe.run( - main_program, - feed={'i': np.full((1), feed_i, dtype=np.float32)}, - fetch_list=[ - out, - out_grad, - out_i_grad, - out_a_grad, - a_grad, - i_grad, - ], - ) - else: - ret = exe.run( - main_program, - feed={'i': np.full((1), feed_i, dtype=np.float32)}, - fetch_list=[ - out.name, - out.grad_name, - out_i.grad_name, - out_a.grad_name, - a.grad_name, - i.grad_name, - ], - ) - - np.testing.assert_allclose( - np.asarray(ret[0]), expected_out, rtol=1e-05 - ) - np.testing.assert_allclose( - np.asarray(ret[1]), expected_out_grad, rtol=1e-05 - ) - np.testing.assert_allclose( - np.asarray(ret[2]), expected_out_grad, rtol=1e-05 - ) - np.testing.assert_allclose( - np.asarray(ret[3]), expected_out_grad, rtol=1e-05 - ) - np.testing.assert_allclose( - np.asarray(ret[4]), expected_a_grad, rtol=1e-05 - ) - np.testing.assert_allclose( - np.asarray(ret[5]), expected_i_grad, rtol=1e-05 - ) - - -class TestStaticPyLayerBackward(unittest.TestCase): - def setUp(self): - paddle.enable_static() - - def test_identity_backward(self): - def forward_fn(x): - return x - - def backward_fn(dy): - return dy - - main_program = paddle.static.Program() - start_program = paddle.static.Program() - input_shape = (2, 4) - with paddle.static.program_guard(main_program, start_program): - data = paddle.static.data( - name="X", shape=input_shape, dtype="float32" - ) - data.stop_gradient = False - out = paddle.static.nn.static_pylayer( - forward_fn, [data], backward_fn - ) - loss = paddle.mean(out) - grad_list = append_backward(loss, [data]) - - place = ( - paddle.CUDAPlace(0) - if core.is_compiled_with_cuda() - else paddle.CPUPlace() - ) - exe = base.Executor(place) - randn_x = np.random.random(size=input_shape).astype(np.float32) - - if paddle.framework.in_pir_mode(): - for p, g in grad_list: - if p.is_same(data): - data_grad = g - ret, x_grad = exe.run( - main_program, - feed={ - 'X': randn_x, - }, - fetch_list=[out, data_grad], - ) - else: - ret, x_grad = exe.run( - main_program, - feed={ - 'X': randn_x, - }, - fetch_list=[out.name, data.grad_name], - ) - - np.testing.assert_allclose( - np.asarray(ret), - randn_x, - rtol=1e-05, - ) - - np.testing.assert_allclose( - np.asarray(x_grad), - np.full( - input_shape, - 1.0 / functools.reduce(lambda x, y: x * y, input_shape), - dtype=np.float32, - ), - rtol=1e-05, - ) - - def test_static_pylayer_backward(self): - ''' - pseudocode: - - y = 3 * x - dx = tanh(dy) - ''' - - def forward_fn(x): - return 3 * x - - def backward_fn(dy): - return paddle.tanh(dy) - - main_program = paddle.static.Program() - start_program = paddle.static.Program() - input_shape = (3, 4) - with paddle.static.program_guard(main_program, start_program): - data = paddle.full( - shape=input_shape, dtype='float32', fill_value=-2.0 - ) - data.stop_gradient = False - out = paddle.static.nn.static_pylayer( - forward_fn, [data], backward_fn - ) - loss = paddle.mean(out) - grad_list = append_backward(loss, [data]) - - place = ( - paddle.CUDAPlace(0) - if core.is_compiled_with_cuda() - else paddle.CPUPlace() - ) - exe = base.Executor(place) - - if paddle.framework.in_pir_mode(): - for p, g in grad_list: - if p.is_same(data): - data_grad = g - ret, x_grad = exe.run( - main_program, - fetch_list=[out, data_grad], - ) - else: - ret, x_grad = exe.run( - main_program, - fetch_list=[out.name, data.grad_name], - ) - - np.testing.assert_allclose( - np.asarray(ret), - np.full(input_shape, -6.0, dtype=np.float32), - rtol=1e-05, - ) - - np.testing.assert_allclose( - np.asarray(x_grad), - np.full( - input_shape, - np.tanh( - 1.0 / functools.reduce(lambda x, y: x * y, input_shape) - ), - dtype=np.float32, - ), - rtol=1e-05, - ) - - -class TestStaticPyLayerPrune(TestPruneBase): - def setUp(self): - paddle.enable_static() - - def net(self): - def forward_fn(x): - y = 3 * x - return y - - def backward_fn(dy): - grad = paddle.exp(dy) - return grad - - x = paddle.static.data(name='x', shape=[-1, 2], dtype='float32') - x.desc.set_need_check_feed(False) - hidden = paddle.static.nn.fc(x=[x], size=4, activation="softmax") - y = paddle.static.nn.static_pylayer(forward_fn, [hidden], backward_fn) - loss = paddle.mean(y) - return x, hidden, y, loss - - def net_with_weight(self): - def forward_fn(x): - y = 3 * x - return y - - def backward_fn(dy): - grad = paddle.exp(dy) - return grad - - x = paddle.static.data(name='x', shape=[-1, 2], dtype='float32') - x.desc.set_need_check_feed(False) - label = paddle.static.data(name="label", shape=[-1, 1], dtype="int64") - label.desc.set_need_check_feed(False) - w_param_attrs = base.ParamAttr( - name="fc_weight", - learning_rate=0.5, - initializer=paddle.nn.initializer.Constant(1.0), - trainable=True, - ) - - y = paddle.static.nn.static_pylayer(forward_fn, [x], backward_fn) - hidden = paddle.static.nn.fc( - x=[y], size=4, activation="softmax", weight_attr=w_param_attrs - ) - loss1 = paddle.nn.functional.cross_entropy( - input=hidden, label=label, reduction='none', use_softmax=False - ) - loss1 = paddle.mean(x=loss1) - loss2 = paddle.nn.functional.cross_entropy( - input=hidden, label=label, reduction='none', use_softmax=False - ) - loss2 = paddle.mean(x=loss2) - loss1.persistable = True - loss2.persistable = True - - return x, hidden, label, loss1, loss2, w_param_attrs - - def test_prune_with_input(self): - ops_before_pruned = [ - "mul", - "elementwise_add", - "softmax", - "pylayer", - "reduce_mean", - ] - - ops_after_pruned = ["pylayer", "reduce_mean"] - - (x, hidden, y, loss), program = self.run_net(self.net) - - self.check_prune_with_input( - program, [hidden.name], [loss], ops_before_pruned, ops_after_pruned - ) - - def test_prune(self): - ops_before_pruned = [ - "mul", - "elementwise_add", - "softmax", - "pylayer", - "reduce_mean", - ] - - ops_after_pruned = [ - "mul", - "elementwise_add", - "softmax", - "pylayer", - "reduce_mean", - ] - - (x, hidden, y, loss), program = self.run_net(self.net) - - self.check_prune(program, [loss], ops_before_pruned, ops_after_pruned) - - def test_prune_target_not_list(self): - ops_before_pruned = [ - "mul", - "elementwise_add", - "softmax", - "pylayer", - "reduce_mean", - ] - - ops_after_pruned = [ - "mul", - "elementwise_add", - "softmax", - "pylayer", - "reduce_mean", - ] - - (x, hidden, y, loss), program = self.run_net(self.net) - self.check_prune_target_not_list( - program, loss, ops_before_pruned, ops_after_pruned - ) - - def test_prune_target_none(self): - ops_before_pruned = [ - "mul", - "elementwise_add", - "softmax", - "pylayer", - "reduce_mean", - ] - - (x, hidden, y, loss), program = self.run_net(self.net) - self.check_prune_target_none(program, ops_before_pruned) - - -def net_with_weight1(): - def forward_fn(x): - y = 3 * x - return y - - def backward_fn(dy): - grad = paddle.exp(dy) - return grad - - x = paddle.static.data(name='x', shape=[-1, 2], dtype='float32') - x.desc.set_need_check_feed(False) - label = paddle.static.data(name="label", shape=[-1, 1], dtype="int64") - label.desc.set_need_check_feed(False) - w_param_attrs = base.ParamAttr( - name="fc_weight", - learning_rate=0.5, - initializer=paddle.nn.initializer.Constant(1.0), - trainable=True, - ) - - y = paddle.static.nn.static_pylayer(forward_fn, [x], backward_fn) - hidden = paddle.static.nn.fc( - x=[y], size=4, activation="softmax", weight_attr=w_param_attrs - ) - loss1 = paddle.nn.functional.cross_entropy( - input=hidden, label=label, reduction='none', use_softmax=False - ) - loss1 = paddle.mean(x=loss1) - loss2 = paddle.nn.functional.cross_entropy( - input=hidden, label=label, reduction='none', use_softmax=False - ) - loss2 = paddle.mean(x=loss2) - loss1.persistable = True - loss2.persistable = True - - return x, hidden, label, loss1, loss2, w_param_attrs - - -def net_with_weight2(): - def forward_fn(x): - y = 3 * x - return y - - def backward_fn(dy): - grad = paddle.exp(dy) - return grad - - x1 = paddle.static.data(name='x1', shape=[-1, 2], dtype='float32') - x1.desc.set_need_check_feed(False) - x2 = paddle.static.data(name='x2', shape=[-1, 2], dtype='float32') - x2.desc.set_need_check_feed(False) - label = paddle.static.data(name="label", shape=[-1, 1], dtype="int64") - label.desc.set_need_check_feed(False) - w1_param_attrs = base.ParamAttr( - name="fc_weight1", - learning_rate=0.5, - initializer=paddle.nn.initializer.Constant(1.0), - trainable=True, - ) - w2_param_attrs = base.ParamAttr( - name="fc_weight2", - learning_rate=0.5, - initializer=paddle.nn.initializer.Constant(1.0), - trainable=True, - ) - - y1 = paddle.static.nn.static_pylayer(forward_fn, [x1], backward_fn) - hidden1 = paddle.static.nn.fc( - x=[y1], size=4, activation="softmax", weight_attr=w1_param_attrs - ) - y2 = paddle.static.nn.static_pylayer(forward_fn, [x2], backward_fn) - hidden2 = paddle.static.nn.fc( - x=[y2], size=4, activation="softmax", weight_attr=w2_param_attrs - ) - - loss1 = paddle.nn.functional.cross_entropy( - input=hidden1, label=label, reduction='none', use_softmax=False - ) - loss1 = paddle.mean(x=loss1) - loss2 = paddle.nn.functional.cross_entropy( - input=hidden2, label=label, reduction='none', use_softmax=False - ) - loss2 = paddle.mean(x=loss2) - loss1.persistable = True - loss2.persistable = True - - return x1, x2, y1, y2, label, loss1, loss2, w1_param_attrs, w2_param_attrs - - -class TestStaticPyLayerExecutorAutoPrune(TestExecutorRunAutoPrune): - def setUp(self): - paddle.enable_static() - self.net1 = net_with_weight1 - self.net2 = net_with_weight2 - - -if __name__ == '__main__': - unittest.main() diff --git a/test/deprecated/legacy_test/test_switch_deprecated.py b/test/deprecated/legacy_test/test_switch_deprecated.py deleted file mode 100644 index d8b2e2fd061ad9..00000000000000 --- a/test/deprecated/legacy_test/test_switch_deprecated.py +++ /dev/null @@ -1,108 +0,0 @@ -# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest - -import paddle -from paddle.base import core, framework -from paddle.base.executor import Executor -from paddle.base.framework import default_startup_program - -paddle.enable_static() - - -class TestSwitch(unittest.TestCase): - def check_switch(self, value): - x = paddle.tensor.fill_constant(shape=[1], dtype='float32', value=value) - zero_var = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.0 - ) - one_var = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=1.0 - ) - two_var = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=2.0 - ) - three_var = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=3.0 - ) - - result = paddle.static.create_global_var( - shape=[1], value=-1.0, dtype='float32', persistable=True - ) - - res = paddle.static.nn.case( - pred_fn_pairs=[ - (paddle.less_than(x, zero_var), lambda: zero_var), - (paddle.less_than(x, one_var), lambda: one_var), - (paddle.less_than(x, two_var), lambda: two_var), - ], - default=lambda: three_var, - ) - paddle.assign(res, result) - - cpu = core.CPUPlace() - exe = Executor(cpu) - exe.run(default_startup_program()) - - out = exe.run(feed={}, fetch_list=[result])[0][0] - return out - - def test_switch(self): - test_data = {(-0.1, 0), (0.1, 1), (1.1, 2), (2.1, 3)} - for x, expected_result in test_data: - main_program = framework.Program() - startup_program = framework.Program() - with framework.program_guard(main_program, startup_program): - result = self.check_switch(x) - self.assertEqual(result, expected_result) - - -class TestSwitchCaseError(unittest.TestCase): - def test_error(self): - main_program = framework.Program() - startup_program = framework.Program() - with framework.program_guard(main_program, startup_program): - cond = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.0 - ) - zero_var = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.0 - ) - - result = paddle.static.create_global_var( - shape=[1], value=-1.0, dtype='float32', persistable=True - ) - - # 1. The type of 'condition' in case must be Variable. - def test_condition_type(): - res = paddle.static.nn.case( - [(1, lambda: zero_var)], default=lambda: result - ) - paddle.assign(res, result) - - self.assertRaises(TypeError, test_condition_type) - - # 2. The dtype of 'condition' in case must be 'bool'. - def test_condition_dtype(): - res = paddle.static.nn.case( - [cond, lambda: zero_var], default=lambda: result - ) - paddle.assign(res, result) - - self.assertRaises(TypeError, test_condition_dtype) - - -if __name__ == '__main__': - unittest.main() diff --git a/test/ir/pir/test_special_op_translator.py b/test/ir/pir/test_special_op_translator.py index 4f77b551b724b2..5690094d898b2c 100644 --- a/test/ir/pir/test_special_op_translator.py +++ b/test/ir/pir/test_special_op_translator.py @@ -66,35 +66,6 @@ def cond_with_inplace(): l = pir.translate_to_pir(legacy_program.main_program.desc) assert l is not None - def test_nested_op(self): - with paddle.pir_utils.OldIrGuard(): - - def cond_with_inplace(): - x = paddle.ones(shape=[2, 1, 2, 3], dtype="float32") - y = paddle.ones(shape=[2, 1, 2, 3], dtype="float32") - z = paddle.ones(shape=[2, 1, 2, 3], dtype="float32") - running_mean = paddle.to_tensor([0], dtype="float32") - running_variance = paddle.to_tensor([1], dtype="float32") - weight = paddle.to_tensor([2], dtype="float32") - bias = paddle.to_tensor([1], dtype="float32") - if y > z: - z = paddle.nn.functional.batch_norm( - z, running_mean, running_variance, weight, bias - ) - else: - y = paddle.nn.functional.batch_norm( - x, running_mean, running_variance, weight, bias - ) - - legacy_program = paddle.jit.to_static( - cond_with_inplace, - input_spec=[], - full_graph=True, - ) - - l = pir.translate_to_pir(legacy_program.main_program.desc) - assert l is not None - class TestElementwiseOpTranscriber(unittest.TestCase): def test_elementwise_without_y_grad(self): diff --git a/test/legacy_test/test_index_select_op.py b/test/legacy_test/test_index_select_op.py index 76efcc52245c4e..b5be024abeda88 100644 --- a/test/legacy_test/test_index_select_op.py +++ b/test/legacy_test/test_index_select_op.py @@ -315,5 +315,99 @@ def test_dygraph_api(self): np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) +def get_places(): + places = [] + if base.is_compiled_with_cuda(): + places.append(paddle.CUDAPlace(0)) + places.append(paddle.CPUPlace()) + return places + + +class TestIndexSelectAPI_Compatibility(unittest.TestCase): + def setUp(self): + np.random.seed(123) + self.places = get_places() + self.shape = [10, 20] + self.index_shape = [5] + self.axis = 1 + self.dtype = 'float32' + self.init_data() + + def init_data(self): + self.np_input = np.random.rand(*self.shape).astype(self.dtype) + self.np_index = np.random.randint( + 0, self.shape[self.axis], self.index_shape + ).astype('int64') + + def test_dygraph_Compatibility(self): + paddle.disable_static() + x = paddle.to_tensor(self.np_input) + index = paddle.to_tensor(self.np_index) + paddle_dygraph_out = [] + # Position args (args) + out1 = paddle.index_select(x, index, self.axis) + paddle_dygraph_out.append(out1) + # Key words args (kwargs) for paddle + out2 = paddle.index_select(x=x, index=index, axis=self.axis) + paddle_dygraph_out.append(out2) + # Key words args for torch + out3 = paddle.index_select(input=x, index=index, dim=self.axis) + paddle_dygraph_out.append(out3) + # Combined args and kwargs + out4 = paddle.index_select(x, index, dim=self.axis) + paddle_dygraph_out.append(out4) + # Tensor method args + out5 = x.index_select(index, self.axis) + paddle_dygraph_out.append(out5) + # Tensor method kwargs + out6 = x.index_select(index=index, dim=self.axis) + paddle_dygraph_out.append(out6) + # Test out + ref_out_shape = list(self.np_input.shape) + ref_out_shape[self.axis] = len(self.np_index) + out7 = paddle.empty(ref_out_shape, dtype=x.dtype) + paddle.index_select(input=x, index=index, dim=self.axis, out=out7) + paddle_dygraph_out.append(out7) + # Numpy reference out + ref_out = np.take(self.np_input, self.np_index, axis=self.axis) + # Check + for out in paddle_dygraph_out: + np.testing.assert_allclose(ref_out, out.numpy(), rtol=1e-05) + paddle.enable_static() + + def test_static_Compatibility(self): + paddle.enable_static() + main = paddle.static.Program() + startup = paddle.static.Program() + with base.program_guard(main, startup): + x = paddle.static.data(name="x", shape=self.shape, dtype=self.dtype) + index = paddle.static.data( + name="index", shape=self.index_shape, dtype='int64' + ) + # Position args (args) + out1 = paddle.index_select(x, index, self.axis) + # Key words args (kwargs) for paddle + out2 = paddle.index_select(x=x, index=index, axis=self.axis) + # Key words args for torch + out3 = paddle.index_select(input=x, index=index, dim=self.axis) + # Combined args and kwargs + out4 = paddle.index_select(x, index, dim=self.axis) + # Tensor method args + out5 = x.index_select(index, self.axis) + # Tensor method kwargs + out6 = x.index_select(index=index, dim=self.axis) + # Do not support out in static + ref_out = np.take(self.np_input, self.np_index, axis=self.axis) + for place in self.places: + exe = base.Executor(place) + fetches = exe.run( + main, + feed={"x": self.np_input, "index": self.np_index}, + fetch_list=[out1, out2, out3, out4, out5, out6], + ) + for out in fetches: + np.testing.assert_allclose(out, ref_out, rtol=1e-05) + + if __name__ == '__main__': unittest.main() diff --git a/test/legacy_test/test_logical_op.py b/test/legacy_test/test_logical_op.py index c605f29af0f33b..e4cdc3cf841d72 100755 --- a/test/legacy_test/test_logical_op.py +++ b/test/legacy_test/test_logical_op.py @@ -18,6 +18,7 @@ from op_test import convert_float_to_uint16 import paddle +from paddle import base from paddle.framework import in_dynamic_mode SUPPORTED_DTYPES = [ @@ -301,6 +302,151 @@ def test_type_error(self): test_type_error(self, True, type_map) +def get_places(): + places = [] + if base.is_compiled_with_cuda(): + places.append(paddle.CUDAPlace(0)) + places.append(paddle.CPUPlace()) + return places + + +class TestLogicalOpsAPI_Compatibility(unittest.TestCase): + def setUp(self): + np.random.seed(123) + paddle.enable_static() + self.places = get_places() + self.shape = [10, 20] + self.dtype = 'bool' + + def test_dygraph_api_compatibility(self): + paddle.disable_static() + for op_info in TEST_META_OP_DATA: + op_str = op_info['op_str'] + is_binary = op_info['binary_op'] + with self.subTest(op=op_str): + np_input = np.random.choice([True, False], size=self.shape) + x = paddle.to_tensor(np_input) + paddle_op = getattr(paddle, op_str) + ref_op = getattr(np, op_str) + + paddle_dygraph_out = [] + + if is_binary: + np_other = np.random.choice([True, False], size=self.shape) + y = paddle.to_tensor(np_other) + # Position args (args) + paddle_dygraph_out.append(paddle_op(x, y)) + # Key words args (kwargs) for paddle + paddle_dygraph_out.append(paddle_op(x=x, y=y)) + # Key words args for torch + paddle_dygraph_out.append(paddle_op(input=x, other=y)) + # Combined args and kwargs + paddle_dygraph_out.append(paddle_op(x, other=y)) + # Tensor method args + paddle_dygraph_out.append(x.__getattribute__(op_str)(y)) + # Tensor method kwargs + paddle_dygraph_out.append( + x.__getattribute__(op_str)(other=y) + ) + + # Test out + out_tensor = paddle.empty(self.shape, dtype=self.dtype) + paddle_op(x, y, out=out_tensor) + paddle_dygraph_out.append(out_tensor) + + # Numpy reference out + ref_out = ref_op(np_input, np_other) + else: # Unary op (logical_not) + # Position args (args) + paddle_dygraph_out.append(paddle_op(x)) + # Key words args (kwargs) for paddle + paddle_dygraph_out.append(paddle_op(x=x)) + # Key words args for torch + paddle_dygraph_out.append(paddle_op(input=x)) + # Tensor method args + paddle_dygraph_out.append(x.__getattribute__(op_str)()) + + # Test out + out_tensor = paddle.empty(self.shape, dtype=self.dtype) + paddle_op(x, out=out_tensor) + paddle_dygraph_out.append(out_tensor) + + # Numpy reference out + ref_out = ref_op(np_input) + + # Check + for out in paddle_dygraph_out: + np.testing.assert_equal(ref_out, out.numpy()) + + paddle.enable_static() + + def test_static_api_compatibility(self): + for op_info in TEST_META_OP_DATA: + op_str = op_info['op_str'] + is_binary = op_info['binary_op'] + + with self.subTest(op=op_str): + np_input = np.random.choice([True, False], size=self.shape) + ref_op = getattr(np, op_str) + + main = paddle.static.Program() + startup = paddle.static.Program() + with base.program_guard(main, startup): + x = paddle.static.data( + name="x", shape=self.shape, dtype=self.dtype + ) + paddle_op = getattr(paddle, op_str) + + fetch_list = [] + feed_dict = {"x": np_input} + + if is_binary: + np_other = np.random.choice( + [True, False], size=self.shape + ) + y = paddle.static.data( + name="y", shape=self.shape, dtype=self.dtype + ) + feed_dict["y"] = np_other + + # Position args (args) + fetch_list.append(paddle_op(x, y)) + # Key words args (kwargs) for paddle + fetch_list.append(paddle_op(x=x, y=y)) + # Key words args for torch + fetch_list.append(paddle_op(input=x, other=y)) + # Combined args and kwargs + fetch_list.append(paddle_op(x, other=y)) + # Tensor method args + fetch_list.append(x.__getattribute__(op_str)(y)) + # Tensor method kwargs + fetch_list.append(x.__getattribute__(op_str)(other=y)) + + # Numpy reference out + ref_out = ref_op(np_input, np_other) + else: # Unary op + # Position args (args) + fetch_list.append(paddle_op(x)) + # Key words args (kwargs) for paddle + fetch_list.append(paddle_op(x=x)) + # Key words args for torch + fetch_list.append(paddle_op(input=x)) + # Tensor method args + fetch_list.append(x.__getattribute__(op_str)()) + + # Numpy reference out + ref_out = ref_op(np_input) + + for place in self.places: + exe = base.Executor(place) + outs = exe.run( + main, feed=feed_dict, fetch_list=fetch_list + ) + # Check + for out in outs: + np.testing.assert_equal(ref_out, out) + + if __name__ == '__main__': paddle.enable_static() unittest.main() diff --git a/test/legacy_test/test_while_loop_op.py b/test/legacy_test/test_while_loop_op.py index 6299321d4709b7..78a263cdbd23fa 100644 --- a/test/legacy_test/test_while_loop_op.py +++ b/test/legacy_test/test_while_loop_op.py @@ -540,56 +540,6 @@ def internal_body(i, x, mem_array): np.testing.assert_allclose(res[5], [0.0] * 10, rtol=1e-05) -class TestApiWhileLoopWithSwitchCase(unittest.TestCase): - @compare_legacy_with_pt - def test_with_switch_case(self): - def cond(i): - return paddle.less_than(i, ten) - - def body(i): - def fn_add_three(): - data_add_three = paddle.add(x=i, y=three) - return data_add_three - - def fn_square(): - data_mul_data = paddle.multiply(x=i, y=i) - return data_mul_data - - def fn_add_one(): - data_add_one = paddle.add(x=i, y=one) - return data_add_one - - return paddle.static.nn.switch_case( - branch_index=i, - branch_fns={2: fn_add_three, 5: fn_square}, - default=fn_add_one, - ) - - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - with paddle.static.program_guard(main_program, startup_program): - i = paddle.tensor.fill_constant(shape=[1], dtype='int64', value=1) - ten = paddle.tensor.fill_constant( - shape=[1], dtype='int64', value=10 - ) - three = paddle.tensor.fill_constant( - shape=[1], dtype='int64', value=3 - ) - one = paddle.tensor.fill_constant(shape=[1], dtype='int64', value=1) - out = paddle.static.nn.while_loop(cond, body, [i]) - - place = ( - base.CUDAPlace(0) - if core.is_compiled_with_cuda() - else base.CPUPlace() - ) - exe = base.Executor(place) - res = exe.run(main_program, fetch_list=out) - - data = np.asarray([25]).astype('int64') - np.testing.assert_allclose(np.asarray(res[0]), data, rtol=1e-05) - - class TestApiWhileLoop_Error(unittest.TestCase): @compare_legacy_with_pt def test_error1(self): diff --git a/tools/parallel_UT_rule.py b/tools/parallel_UT_rule.py index cf76c82a31b598..277b7f67f35b71 100755 --- a/tools/parallel_UT_rule.py +++ b/tools/parallel_UT_rule.py @@ -424,7 +424,6 @@ 'op_version_registry_test', 'test_cudnn_placement_pass', 'cipher_utils_test', - 'test_program_code_deprecated', 'test_save_model_without_var', 'program_utils_test', 'test_fleet_distributed_strategy', @@ -1098,7 +1097,6 @@ 'test_normal', 'test_tensor_scalar_type_promotion_static', 'test_trt_group_norm_op', - 'test_learning_rate_scheduler_deprecated', 'test_numel_op', 'test_adaptive_max_pool3d', 'test_sequential', @@ -1176,7 +1174,6 @@ 'test_memory_reuse_exclude_feed_var', 'test_polygon_box_transform', 'math_function_gpu_test', - 'test_program_prune_backward_deprecated', 'test_ema_fleet', 'test_normalize', 'test_correlation', @@ -1626,7 +1623,6 @@ 'test_protobuf', 'test_progressbar', 'test_program_to_string', - 'test_program_code_deprecated', 'test_program', 'test_precision_recall_op', 'test_post_training_quantization_resnet50', @@ -2567,7 +2563,6 @@ 'test_label_smooth_op', 'test_logsumexp', 'test_log_softmax', - 'test_learning_rate_scheduler_deprecated', 'test_linspace', 'test_linear_interp_op', 'test_lamb_op', diff --git a/tools/static_mode_white_list.py b/tools/static_mode_white_list.py index 7b23b6cff60a90..dc9aa5e510e328 100755 --- a/tools/static_mode_white_list.py +++ b/tools/static_mode_white_list.py @@ -269,7 +269,6 @@ 'test_layer_norm_mkldnn_op', 'test_layer_norm_bf16_mkldnn_op', 'test_layer_norm_op_v2', - 'test_learning_rate_scheduler_deprecated', 'test_linear_interp_op', 'test_linear_interp_v2_op', 'test_linspace', @@ -356,8 +355,6 @@ 'test_prior_box_op', 'test_profiler', 'test_program', - 'test_program_code_deprecated', - 'test_program_prune_backward_deprecated', 'test_program_to_string', 'test_protobuf_descs', 'test_proximal_gd_op', diff --git a/tools/windows/run_unittests.sh b/tools/windows/run_unittests.sh index f0db3f2474b50e..27af49c4f7476f 100644 --- a/tools/windows/run_unittests.sh +++ b/tools/windows/run_unittests.sh @@ -24,7 +24,6 @@ disable_wingpu_test="^test_model$|\ ^test_generator_dataloader_deprecated$|\ ^test_parallel_dygraph_sync_batch_norm$|\ ^test_py_reader_using_executor$|\ -^test_program_prune_backward_deprecated$|\ ^test_decoupled_py_reader_data_check_deprecated$|\ ^test_fleet_base_single$|\ ^test_multiprocess_dataloader_iterable_dataset_dynamic$|\ @@ -430,7 +429,6 @@ disable_wincpu_test="^jit_kernel_test$|\ ^test_vision_models$|\ ^test_dygraph_multi_forward$|\ ^test_imperative_transformer_sorted_gradient$|\ -^test_program_prune_backward_deprecated$|\ ^test_imperative_resnet$|\ ^test_imperative_resnet_sorted_gradient$|\ ^test_imperative_se_resnext$|\ From af42b4bca5659769b9422e58a0d7bf63451fd2f3 Mon Sep 17 00:00:00 2001 From: cangtianhuang Date: Wed, 20 Aug 2025 21:03:14 +0800 Subject: [PATCH 05/27] add out for index_select --- python/paddle/_paddle_docs.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index 7b73acac36d334..a6ca4df6ba7f16 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -430,6 +430,9 @@ def all( axis (int, optional): The dimension in which we index. Default: if None, the ``axis`` is 0. name (str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + Keyword Args: + out (Tensor|optional): The output tensor. + Returns: Tensor, A Tensor with same data type as ``x``. From 61acc8ff04f3c0ff6a89abac1d626b374e8dadd4 Mon Sep 17 00:00:00 2001 From: cangtianhuang Date: Wed, 20 Aug 2025 21:15:44 +0800 Subject: [PATCH 06/27] add out in signature for index_select --- python/paddle/_paddle_docs.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index a6ca4df6ba7f16..eb2f42cc316ca5 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -458,8 +458,13 @@ def all( """, """ def index_select( - x: Tensor, index: Tensor, axis: int = 0, name: str | None = None -) -> Tensor + x: Tensor, + index: Tensor, + axis: int = 0, + name: str | None = None, + *, + out: Tensor | None = None, +) -> Tensor: """, ) From 9919331b91d7eb56fb9d96efd9a6188e19fe0440 Mon Sep 17 00:00:00 2001 From: cangtianhuang Date: Thu, 21 Aug 2025 10:37:25 +0800 Subject: [PATCH 07/27] fix --- python/paddle/_paddle_docs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index eb2f42cc316ca5..91dfaaa7cfbf1c 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -464,7 +464,7 @@ def index_select( name: str | None = None, *, out: Tensor | None = None, -) -> Tensor: +) -> Tensor """, ) From 39fbd8359b1f89a6dc364640ca9528e30905c743 Mon Sep 17 00:00:00 2001 From: cangtianhuang Date: Thu, 21 Aug 2025 15:24:20 +0800 Subject: [PATCH 08/27] fix tests --- test/legacy_test/test_cond.py | 8 ----- test/legacy_test/test_while_loop_op.py | 49 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/test/legacy_test/test_cond.py b/test/legacy_test/test_cond.py index 5a9b1fb51d9140..c009965bcc5b83 100644 --- a/test/legacy_test/test_cond.py +++ b/test/legacy_test/test_cond.py @@ -30,7 +30,6 @@ class TestCondInputOutput(unittest.TestCase): - @compare_legacy_with_pt def test_return_single_var(self): """ pseudocode: @@ -81,7 +80,6 @@ def false_func(): np.asarray(ret), np.full((3, 2), -1, np.int32), rtol=1e-05 ) - @compare_legacy_with_pt def test_return_0d_tensor(self): """ pseudocode: @@ -122,7 +120,6 @@ def false_func(): np.testing.assert_allclose(np.asarray(ret), np.array(2), rtol=1e-05) self.assertEqual(ret.shape, ()) - @compare_legacy_with_pt def test_0d_tensor_as_cond(self): """ pseudocode: @@ -233,7 +230,6 @@ def test_0d_tensor_dygraph(self): ) self.assertEqual(a.grad.shape, []) - @compare_legacy_with_pt def test_return_var_tuple(self): """ pseudocode: @@ -283,7 +279,6 @@ def false_func(): np.asarray(ret[1]), np.full((2, 3), True, bool), rtol=1e-05 ) - @compare_legacy_with_pt def test_pass_and_modify_var(self): """ pseudocode: @@ -374,7 +369,6 @@ def false_func(): self.assertIsNone(out2) self.assertIsNone(out3) - @compare_legacy_with_pt def test_wrong_structure_exception(self): """ test returning different number of tensors cannot merge into output @@ -821,7 +815,6 @@ def add_optimizer_helper(self, cond_func, use_cuda): fetch_list=[loss], ) - @compare_legacy_with_pt def test_cond_backward(self): paddle.enable_static() @@ -929,7 +922,6 @@ def func(): class TestCondWithDict(unittest.TestCase): - @compare_legacy_with_pt def test_input_with_dict(self): paddle.enable_static() main_program = framework.Program() diff --git a/test/legacy_test/test_while_loop_op.py b/test/legacy_test/test_while_loop_op.py index 78a263cdbd23fa..8a5cdf36bbd867 100644 --- a/test/legacy_test/test_while_loop_op.py +++ b/test/legacy_test/test_while_loop_op.py @@ -540,6 +540,55 @@ def internal_body(i, x, mem_array): np.testing.assert_allclose(res[5], [0.0] * 10, rtol=1e-05) +class TestApiWhileLoopWithSwitchCase(unittest.TestCase): + def test_with_switch_case(self): + def cond(i): + return paddle.less_than(i, ten) + + def body(i): + def fn_add_three(): + data_add_three = paddle.add(x=i, y=three) + return data_add_three + + def fn_square(): + data_mul_data = paddle.multiply(x=i, y=i) + return data_mul_data + + def fn_add_one(): + data_add_one = paddle.add(x=i, y=one) + return data_add_one + + return paddle.static.nn.switch_case( + branch_index=i, + branch_fns={2: fn_add_three, 5: fn_square}, + default=fn_add_one, + ) + + main_program = paddle.static.Program() + startup_program = paddle.static.Program() + with paddle.static.program_guard(main_program, startup_program): + i = paddle.tensor.fill_constant(shape=[1], dtype='int64', value=1) + ten = paddle.tensor.fill_constant( + shape=[1], dtype='int64', value=10 + ) + three = paddle.tensor.fill_constant( + shape=[1], dtype='int64', value=3 + ) + one = paddle.tensor.fill_constant(shape=[1], dtype='int64', value=1) + out = paddle.static.nn.while_loop(cond, body, [i]) + + place = ( + base.CUDAPlace(0) + if core.is_compiled_with_cuda() + else base.CPUPlace() + ) + exe = base.Executor(place) + res = exe.run(main_program, fetch_list=out) + + data = np.asarray([25]).astype('int64') + np.testing.assert_allclose(np.asarray(res[0]), data, rtol=1e-05) + + class TestApiWhileLoop_Error(unittest.TestCase): @compare_legacy_with_pt def test_error1(self): From 0576301692b4e397956d0e3df7bf31b70bc4fc18 Mon Sep 17 00:00:00 2001 From: cangtianhuang Date: Thu, 21 Aug 2025 22:05:48 +0800 Subject: [PATCH 09/27] fix docs --- python/paddle/_paddle_docs.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index 91dfaaa7cfbf1c..cd89ea4af54a4d 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -348,6 +348,8 @@ def amax( Keyword Args: out (Tensor|optional): The output tensor. + + Returns: Tensor: Results the ``logical and`` on the specified axis of input Tensor `x`, it's data type is bool. Examples: From 0701cd37cd643bac582ed44ed677aa4f67566233 Mon Sep 17 00:00:00 2001 From: zhengshengning Date: Fri, 22 Aug 2025 17:51:11 +0800 Subject: [PATCH 10/27] add numpy.dtype and str_dtype to Paddle DataType --- paddle/fluid/pybind/eager_utils.cc | 88 +++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index e679052bab5415..0491b31e688841 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -118,6 +118,79 @@ int TensorDtype2NumpyDtype(phi::DataType dtype) { } } +phi::DataType NumpyDtype2TensorDtype(const int& np_dtype) { + switch (np_dtype) { + case pybind11::detail::npy_api::NPY_BOOL_: + return phi::DataType::BOOL; + case pybind11::detail::npy_api::NPY_INT8_: + return phi::DataType::INT8; + case pybind11::detail::npy_api::NPY_UINT8_: + return phi::DataType::UINT8; + case pybind11::detail::npy_api::NPY_INT16_: + return phi::DataType::INT16; + case pybind11::detail::npy_api::NPY_INT32_: + return phi::DataType::INT32; + case pybind11::detail::npy_api::NPY_INT64_: + return phi::DataType::INT64; + case pybind11::detail::NPY_UINT16_: + return phi::DataType::BFLOAT16; + case pybind11::detail::NPY_FLOAT16_: + return phi::DataType::FLOAT16; + case pybind11::detail::npy_api::NPY_FLOAT_: + return phi::DataType::FLOAT32; + case pybind11::detail::npy_api::NPY_DOUBLE_: + return phi::DataType::FLOAT64; + case pybind11::detail::NPY_COMPLEX64: + return phi::DataType::COMPLEX64; + case pybind11::detail::NPY_COMPLEX128: + return phi::DataType::COMPLEX128; + case pybind11::detail::npy_api::NPY_UNICODE_: + return phi::DataType::PSTRING; + default: + PADDLE_THROW(common::errors::InvalidArgument( + "Unknown numpy dtype, the int value = %d.", np_dtype)); + return phi::DataType::UNDEFINED; + } +} + +phi::DataType StrDtype2TensorDtype(const std::string& np_dtype) { + if (np_dtype == "bool") { + return phi::DataType::BOOL; + } else if (np_dtype == "int8") { + return phi::DataType::INT8; + } else if (np_dtype == "uint8") { + return phi::DataType::UINT8; + } else if (np_dtype == "int16") { + return phi::DataType::INT16; + } else if (np_dtype == "int32") { + return phi::DataType::INT32; + } else if (np_dtype == "int64") { + return phi::DataType::INT64; + } else if (np_dtype == "bfloat16") { + return phi::DataType::BFLOAT16; + } else if (np_dtype == "float16") { + return phi::DataType::FLOAT16; + } else if (np_dtype == "float32") { + return phi::DataType::FLOAT32; + } else if (np_dtype == "float64") { + return phi::DataType::FLOAT64; + } else if (np_dtype == "complex64") { + return phi::DataType::COMPLEX64; + } else if (np_dtype == "complex128") { + return phi::DataType::COMPLEX128; + } else if (np_dtype == "float8_e4m3fn") { + return phi::DataType::FLOAT8_E4M3FN; + } else if (np_dtype == "float8_e5m2") { + return phi::DataType::FLOAT8_E5M2; + } else if (np_dtype == "unicode") { + return phi::DataType::PSTRING; + } else { + PADDLE_THROW(common::errors::InvalidArgument( + "Unknown numpy dtype, the value = %s.", np_dtype)); + return phi::DataType::UNDEFINED; + } +} + bool PyObject_CheckStr(PyObject* obj) { return PyUnicode_Check(obj); } bool PyObject_CheckIRValue(PyObject* obj) { @@ -2657,8 +2730,21 @@ paddle::DataType CastPyArg2DataType(PyObject* obj, if (PyObject_TypeCheck(obj, g_vartype_pytype)) { framework::proto::VarType::Type type = CastPyArg2ProtoType(obj, arg_pos); return phi::TransToPhiDataType(type); + } else if (PyObject_TypeCheck(obj, g_data_type_pytype)) { + return CastPyArg2DataTypeDirectly(obj, op_type, arg_pos); + } else if (PyObject_CheckStr(obj)) { + std::string type_str = CastPyArg2AttrString(obj, arg_pos); + return StrDtype2TensorDtype(type_str); + } else { + if (!pybind11::detail::npy_api::get().PyArrayDescr_Check_(obj)) { + pybind11::object dtype_obj = pybind11::module::import("numpy").attr( + "dtype")(pybind11::reinterpret_borrow(obj)); + obj = dtype_obj.ptr(); + } + int type_num = + reinterpret_cast(obj)->type_num; + return NumpyDtype2TensorDtype(type_num); } - return CastPyArg2DataTypeDirectly(obj, op_type, arg_pos); } paddle::DataType CastPyArg2DataType(PyObject* obj, const std::string& op_type, From 118757bfb99c8e340d66cf15f61416016dc6d879 Mon Sep 17 00:00:00 2001 From: zhengshengning Date: Fri, 22 Aug 2025 19:14:52 +0800 Subject: [PATCH 11/27] =?UTF-8?q?paddle.roll=E3=80=81paddle.flatten=20and?= =?UTF-8?q?=20paddle.Tensor.flatten=20sink=20into=20C++?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pir/dialect/op_generator/python_c_gen.py | 4 +- paddle/fluid/pybind/arg_pre_process.cc | 41 +++++- paddle/fluid/pybind/arg_pre_process.h | 15 +- paddle/phi/ops/yaml/ops.yaml | 7 + python/paddle/_paddle_docs.py | 60 ++++++++ python/paddle/tensor/manipulation.py | 124 ++-------------- .../test_flatten_contiguous_range_op.py | 23 +++ test/legacy_test/test_roll_op.py | 135 +++++++++++++++++- 8 files changed, 288 insertions(+), 121 deletions(-) diff --git a/paddle/fluid/pir/dialect/op_generator/python_c_gen.py b/paddle/fluid/pir/dialect/op_generator/python_c_gen.py index 7c60d327dc05f3..9d352d6f7d0d2d 100644 --- a/paddle/fluid/pir/dialect/op_generator/python_c_gen.py +++ b/paddle/fluid/pir/dialect/op_generator/python_c_gen.py @@ -230,9 +230,9 @@ PyObject *{name}_obj = PyTuple_GET_ITEM(args, {index});""" MUTABLE_ATTR_OBJ_FROM_ARGS_KWARGS_WITH_DEFAULT_VALUE_TEMPLATE = """ - PyObject *{name}_obj = GetItemFromArgsOrKWArgs(args, {index},kwargs,{keywords}, nargs, &remaining_kwargs,false);""" -MUTABLE_ATTR_OBJ_FROM_ARGS_KWARGS_TEMPLATE = """ PyObject *{name}_obj = GetItemFromArgsOrKWArgs(args, {index},kwargs,{keywords}, nargs, &remaining_kwargs);""" +MUTABLE_ATTR_OBJ_FROM_ARGS_KWARGS_TEMPLATE = """ + PyObject *{name}_obj = GetItemFromArgsOrKWArgs(args, {index},kwargs,{keywords}, nargs, &remaining_kwargs, false);""" MUTABLE_ATTR_CAST_TEMPLATE = """ {type} {name_} = {cast_func}({name}_obj, "{api_name}", {index});""" diff --git a/paddle/fluid/pybind/arg_pre_process.cc b/paddle/fluid/pybind/arg_pre_process.cc index 1dd1e8c70e3c07..dcf488646928b6 100644 --- a/paddle/fluid/pybind/arg_pre_process.cc +++ b/paddle/fluid/pybind/arg_pre_process.cc @@ -19,11 +19,50 @@ // processing of parameters originally done in the Python API #include "paddle/fluid/pybind/arg_pre_process.h" #include "paddle/fluid/eager/utils.h" +#include "paddle/fluid/pir/utils/general_functions.h" #include "paddle/fluid/pybind/eager_utils.h" #include "paddle/fluid/pybind/op_function_common.h" #include "paddle/phi/common/data_type.h" #include "paddle/phi/core/enforce.h" namespace paddle { -namespace pybind {} // namespace pybind +namespace pybind { +void RollPreProcess(Tensor* x, IntArray* shifts, IntVector* axis) { + int64_t len_origin_shape = x->dims().size(); + if (axis != NULL) { + int64_t axis_len = axis->size(); + for (int64_t i = 0; i < axis_len; i++) { + PADDLE_ENFORCE_EQ( + ((*axis)[i] < len_origin_shape && (*axis)[i] >= -len_origin_shape), + true, + common::errors::InvalidArgument("axis is out of range, it should be " + "in range [%d, %d), but received %ld", + -len_origin_shape, + len_origin_shape, + (*axis)[i])); + } + } else { + axis = new IntVector(); + } +} +void RollPreProcess(Value* x, Value* shifts, IntVector* axis) { + std::vector x_shape = pir::GetShapeFromValue(*x); + int64_t len_origin_shape = x_shape.size(); + if (axis != NULL) { + int64_t axis_len = axis->size(); + for (int64_t i = 0; i < axis_len; i++) { + PADDLE_ENFORCE_EQ( + ((*axis)[i] < len_origin_shape && (*axis)[i] >= -len_origin_shape), + true, + common::errors::InvalidArgument("axis is out of range, it should be " + "in range [%d, %d), but received %ld", + -len_origin_shape, + len_origin_shape, + (*axis)[i])); + } + } else { + axis = new IntVector(); + } +} +} // namespace pybind } // namespace paddle diff --git a/paddle/fluid/pybind/arg_pre_process.h b/paddle/fluid/pybind/arg_pre_process.h index 557b6d1c5f4739..5a77c81c7ca853 100644 --- a/paddle/fluid/pybind/arg_pre_process.h +++ b/paddle/fluid/pybind/arg_pre_process.h @@ -15,9 +15,22 @@ #pragma once #include +#include "paddle/fluid/ir_adaptor/translator/program_translator.h" +#include "paddle/phi/api/include/tensor.h" namespace paddle { -namespace pybind {} // namespace pybind +namespace pybind { +using Tensor = paddle::Tensor; +using Value = pir::Value; +using IntArray = paddle::experimental::IntArray; +using IntVector = std::vector; + +void FlattenPreProcess(Tensor* x, int* start_axis, int* stop_axis); +void FlattenPreProcess(Value* x, int* start_axis, int* stop_axis); + +void RollPreProcess(Tensor* x, IntArray* shifts, IntVector* axis); +void RollPreProcess(Value* x, Value* shifts, IntVector* axis); +} // namespace pybind } // namespace paddle diff --git a/paddle/phi/ops/yaml/ops.yaml b/paddle/phi/ops/yaml/ops.yaml index e124d501d2a3b0..dd9bdbc0a679bb 100644 --- a/paddle/phi/ops/yaml/ops.yaml +++ b/paddle/phi/ops/yaml/ops.yaml @@ -4615,6 +4615,13 @@ - op : roll args : (Tensor x, IntArray shifts={}, int64_t[] axis={}) + python_api: + name : [paddle.roll, paddle.Tensor.roll] + args_alias: + axis : [dims] + use_default_mapping : True + pre_process: + func : RollPreProcess(x, shifts, axis) output : Tensor(out) infer_meta : func : RollInferMeta diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index 02212c974e43e6..2b098fb93f9eda 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -504,6 +504,66 @@ def isnan( """, ) +add_doc_and_signature( + "roll", + """ + Roll the `x` tensor along the given axis(axes). With specific 'shifts', Elements that + roll beyond the last position are re-introduced at the first according to 'shifts'. + If a axis is not specified, + the tensor will be flattened before rolling and then restored to the original shape. + .. note:: + Alias Support: The parameter name ``input`` can be used as an alias for ``x``, and the parameter name ``dim`` can be used as an alias for ``axis``. + For example, ``roll(input=tensor_x, dim=1)`` is equivalent to ``roll(x=tensor_x, axis=1)``. + Args: + x (Tensor): The x tensor as input. + alias: ``input``. + shifts (int|list|tuple): The number of places by which the elements + of the `x` tensor are shifted. + axis (int|list|tuple, optional): axis(axes) along which to roll. Default: None + alias: ``dim``. + name(str|None, optional): The default value is None. Normally there is no need for user to set this property. + For more information, please refer to :ref:`api_guide_Name` . + The image below shows a 2D tensor `[[1,2,3],[4,5,6],[7,8,9]]` being transformed into tensors with + different shapes through the roll operation. + .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/roll.png + :width: 700 + :align: center + :alt: legend of roll API + Returns: + Tensor, A Tensor with same data type as `x`. + Examples: + .. code-block:: python + >>> # type: ignore + >>> import paddle + >>> x = paddle.to_tensor([[1.0, 2.0, 3.0], + ... [4.0, 5.0, 6.0], + ... [7.0, 8.0, 9.0]]) + >>> out_z1 = paddle.roll(x, shifts=1) + >>> print(out_z1.numpy()) + [[9. 1. 2.] + [3. 4. 5.] + [6. 7. 8.]] + >>> out_z2 = paddle.roll(x, shifts=1, axis=0) + >>> print(out_z2.numpy()) + [[7. 8. 9.] + [1. 2. 3.] + [4. 5. 6.]] + >>> out_z3 = paddle.roll(x, shifts=1, axis=1) + >>> print(out_z3.numpy()) + [[3. 1. 2.] + [6. 4. 5.] + [9. 7. 8.]] + """, + """ +def roll( + x: Tensor, + shifts: int | Sequence[int], + axis: int | Sequence[int] | None = None, + name: str | None = None, +) -> Tensor +""", +) + # liuyi add_doc_and_signature( "any", diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 4dda5de05faa1d..14e73b9b2ea3f7 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -23,6 +23,7 @@ import paddle from paddle import _C_ops +from paddle._C_ops import roll # noqa: F401 from paddle.tensor import fill_constant from paddle.utils.decorator_utils import ( ParamAliasDecorator, @@ -1871,6 +1872,9 @@ def rot90( return flip(transpose(x, axes_list), axes[1]) +@ParamAliasDecorator( + {"x": ["input"], "start_axis": ["start_dim"], "stop_axis": ["end_dim"]} +) def flatten( x: Tensor, start_axis: int = 0, stop_axis: int = -1, name: str | None = None ) -> Tensor: @@ -1909,11 +1913,18 @@ def flatten( We get: Out.shape = (3 * 100 * 100 * 4) + .. note:: + Alias Support: The parameter name ``input`` can be used as an alias for ``x``, the parameter name ``start_dim`` can be used as an alias for ``start_axis`` , and the parameter name ``end_dim`` can be used as an alias for ``stop_axis``. + For example, ``flatten(input=tensor_x, start_dim=0, end_dim=-1)`` is equivalent to ``flatten(x=tensor_x, start_axis=0, stop_axis=-1)``. + Args: x (Tensor): A tensor of number of dimensions >= axis. A tensor with data type float16, float32, float64, int8, int32, int64, uint8. + alias: ``input``. start_axis (int): the start axis to flatten + alias: ``start_dim``. stop_axis (int): the stop axis to flatten + alias: ``end_dim``. name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: @@ -2087,119 +2098,6 @@ def flatten_( return _C_ops.flatten_(x, start_axis, stop_axis) -def roll( - x: Tensor, - shifts: int | Sequence[int], - axis: int | Sequence[int] | None = None, - name: str | None = None, -) -> Tensor: - """ - Roll the `x` tensor along the given axis(axes). With specific 'shifts', Elements that - roll beyond the last position are re-introduced at the first according to 'shifts'. - If a axis is not specified, - the tensor will be flattened before rolling and then restored to the original shape. - - Args: - x (Tensor): The x tensor as input. - shifts (int|list|tuple): The number of places by which the elements - of the `x` tensor are shifted. - axis (int|list|tuple, optional): axis(axes) along which to roll. Default: None - name(str|None, optional): The default value is None. Normally there is no need for user to set this property. - For more information, please refer to :ref:`api_guide_Name` . - - The image below shows a 2D tensor `[[1,2,3],[4,5,6],[7,8,9]]` being transformed into tensors with - different shapes through the roll operation. - - .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/images/api_legend/roll.png - :width: 700 - :align: center - :alt: legend of roll API - - Returns: - Tensor, A Tensor with same data type as `x`. - - Examples: - .. code-block:: python - - >>> import paddle - - >>> x = paddle.to_tensor([[1.0, 2.0, 3.0], - ... [4.0, 5.0, 6.0], - ... [7.0, 8.0, 9.0]]) - >>> out_z1 = paddle.roll(x, shifts=1) - >>> print(out_z1.numpy()) - [[9. 1. 2.] - [3. 4. 5.] - [6. 7. 8.]] - >>> out_z2 = paddle.roll(x, shifts=1, axis=0) - >>> print(out_z2.numpy()) - [[7. 8. 9.] - [1. 2. 3.] - [4. 5. 6.]] - >>> out_z3 = paddle.roll(x, shifts=1, axis=1) - >>> print(out_z3.numpy()) - [[3. 1. 2.] - [6. 4. 5.] - [9. 7. 8.]] - """ - origin_shape = x.shape - if type(shifts) == int: - shifts = [shifts] - if type(axis) == int: - axis = [axis] - - len_origin_shape = len(origin_shape) - if axis is not None: - for i in range(len(axis)): - if axis[i] >= len_origin_shape or axis[i] < -len_origin_shape: - raise ValueError( - f"axis is out of range, it should be in range [{-len_origin_shape}, {len_origin_shape}), but received {axis}" - ) - else: - axis = [] - - if in_dynamic_or_pir_mode(): - return _C_ops.roll(x, shifts, axis) - else: - check_variable_and_dtype( - x, - 'dtype', - [ - 'bool', - 'float16', - 'float32', - 'uint16', - 'float64', - 'int32', - 'int64', - 'complex64', - 'complex128', - ], - 'roll', - ) - helper = LayerHelper("roll", **locals()) - check_type(axis, 'axis', (list, tuple), 'roll') - - out = helper.create_variable_for_type_inference(x.dtype) - - if isinstance(shifts, Variable): - helper.append_op( - type='roll', - inputs={'X': x, "ShiftsTensor": shifts}, - outputs={'Out': out}, - attrs={'axis': axis}, - ) - else: - check_type(shifts, 'shifts', (list, tuple), 'roll') - helper.append_op( - type='roll', - inputs={'X': x}, - outputs={'Out': out}, - attrs={'axis': axis, 'shifts': shifts}, - ) - return out - - def stack( x: Sequence[Tensor], axis: int = 0, name: str | None = None ) -> Tensor: diff --git a/test/legacy_test/test_flatten_contiguous_range_op.py b/test/legacy_test/test_flatten_contiguous_range_op.py index 4ae6368ac12339..4e0862fec49736 100644 --- a/test/legacy_test/test_flatten_contiguous_range_op.py +++ b/test/legacy_test/test_flatten_contiguous_range_op.py @@ -600,5 +600,28 @@ def test_static(self): np.testing.assert_equal(fetch_out, out_np) +class TestFlattenAPI_Compatible(unittest.TestCase): + def test_dygraph(self): + paddle.disable_static() + data = np.random.randn(2, 3, 5) + x = paddle.to_tensor(data) + out = paddle.flatten(input=x, start_dim=0, end_dim=-1) + out_np = data.flatten() + np.testing.assert_equal(out.numpy(), out_np) + + def test_static(self): + paddle.enable_static() + data = np.random.randn(2, 3, 5) + main_prog = paddle.static.Program() + with paddle.static.program_guard(main_prog, paddle.static.Program()): + x = paddle.static.data(name="x", shape=[2, 3, 5], dtype='float64') + out = paddle.flatten(input=x, start_dim=0, end_dim=-1) + + exe = paddle.static.Executor(place=paddle.CPUPlace()) + fetch_out = exe.run(main_prog, feed={"x": data}, fetch_list=[out])[0] + out_np = data.flatten() + np.testing.assert_equal(fetch_out, out_np) + + if __name__ == "__main__": unittest.main() diff --git a/test/legacy_test/test_roll_op.py b/test/legacy_test/test_roll_op.py index d625ddabcb602a..0f2dbc550122bf 100644 --- a/test/legacy_test/test_roll_op.py +++ b/test/legacy_test/test_roll_op.py @@ -55,7 +55,7 @@ def test_check_output(self): def test_check_grad_normal(self): self.check_grad( - ['X'], 'Out', check_prim=True, check_pir=True, check_prim_pir=True + ['X'], 'Out', check_prim=False, check_pir=True, check_prim_pir=True ) @@ -160,7 +160,7 @@ def test_check_output(self): def test_check_grad_normal(self): self.check_grad_with_place( - self.place, ['X'], 'Out', check_prim=True, check_pir=True + self.place, ['X'], 'Out', check_prim=False, check_pir=True ) @@ -187,7 +187,7 @@ def test_check_grad_normal(self): self.place, ['X'], 'Out', - check_prim=True, + check_prim=False, check_pir=True, check_prim_pir=True, ) @@ -216,7 +216,7 @@ def test_check_grad_normal(self): self.place, ['X'], 'Out', - check_prim=True, + check_prim=False, check_pir=True, check_prim_pir=True, ) @@ -562,5 +562,132 @@ def test_dygraph_api(self): np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) +class TestRollAPI_Compatibility(unittest.TestCase): + def input_data(self): + self.data_x = np.array( + [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]] + ) + + def test_roll_op_api_case1(self): + with static_guard(): + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): + x = paddle.static.data(name='x', shape=[-1, 3], dtype='float32') + data_x = np.array( + [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]] + ).astype('float32') + z = paddle.roll(input=x, shifts=1) + exe = paddle.static.Executor(paddle.CPUPlace()) + (res,) = exe.run( + paddle.static.default_main_program(), + feed={'x': data_x}, + fetch_list=[z], + return_numpy=False, + ) + expect_out = np.array( + [[9.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]] + ) + np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05) + + def test_roll_op_api_case2(self): + with static_guard(): + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): + x = paddle.static.data(name='x', shape=[-1, 3], dtype='float32') + data_x = np.array( + [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]] + ).astype('float32') + z = paddle.roll(x, 1, dims=0) + exe = paddle.static.Executor(paddle.CPUPlace()) + (res,) = exe.run( + paddle.static.default_main_program(), + feed={'x': data_x}, + fetch_list=[z], + return_numpy=False, + ) + expect_out = np.array( + [[7.0, 8.0, 9.0], [1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] + ) + np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05) + paddle.disable_static() + + def test_dygraph_api(self): + self.input_data() + # case 1: + with base.dygraph.guard(): + x = paddle.to_tensor(self.data_x) + z = paddle.roll(input=x, shifts=1) + np_z = z.numpy() + expect_out = np.array( + [[9.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]] + ) + np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) + + # case 2: + with base.dygraph.guard(): + x = paddle.to_tensor(self.data_x) + z = paddle.roll(input=x, shifts=1, dims=0) + np_z = z.numpy() + expect_out = np.array( + [[7.0, 8.0, 9.0], [1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] + ) + np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) + + def test_roll_op_false(self): + def test_axis_out_range(): + paddle.enable_static() + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): + x = paddle.static.data(name='x', shape=[-1, 3], dtype='float32') + data_x = np.array( + [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]] + ).astype('float32') + z = paddle.roll(input=x, shifts=1, dims=10) + exe = base.Executor(base.CPUPlace()) + (res,) = exe.run( + feed={'x': data_x}, + fetch_list=[z], + return_numpy=False, + ) + + self.assertRaises(ValueError, test_axis_out_range) + paddle.disable_static() + + def test_shifts_as_tensor_dygraph(self): + with base.dygraph.guard(): + x = paddle.arange(9).reshape([3, 3]) + shape = paddle.shape(x) + shifts = shape // 2 + axes = [0, 1] + out = paddle.roll(input=x, shifts=shifts, dims=axes).numpy() + expected_out = np.array([[8, 6, 7], [2, 0, 1], [5, 3, 4]]) + np.testing.assert_allclose(out, expected_out, rtol=1e-05) + + def test_shifts_as_tensor_static(self): + paddle.enable_static() + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): + x = paddle.arange(9).reshape([3, 3]).astype('float32') + shape = paddle.shape(x) + shifts = shape // 2 + axes = [0, 1] + out = paddle.roll(input=x, shifts=shifts, dims=axes) + expected_out = np.array([[8, 6, 7], [2, 0, 1], [5, 3, 4]]) + + exe = paddle.static.Executor(paddle.CPUPlace()) + [out_np] = exe.run(fetch_list=[out]) + np.testing.assert_allclose(out_np, expected_out, rtol=1e-05) + + if paddle.is_compiled_with_cuda(): + exe = base.Executor(base.CPUPlace()) + [out_np] = exe.run(fetch_list=[out]) + np.testing.assert_allclose(out_np, expected_out, rtol=1e-05) + paddle.disable_static() + + if __name__ == "__main__": unittest.main() From e680d08163e88ab9c1e41ab365d3ae93e960529e Mon Sep 17 00:00:00 2001 From: cangtianhuang Date: Sat, 23 Aug 2025 14:42:53 +0800 Subject: [PATCH 12/27] fix tests --- test/deprecated/auto_parallel/CMakeLists.txt | 2 - ...st_conditional_block_reshard_deprecated.py | 101 ------------------ 2 files changed, 103 deletions(-) delete mode 100644 test/deprecated/auto_parallel/test_conditional_block_reshard_deprecated.py diff --git a/test/deprecated/auto_parallel/CMakeLists.txt b/test/deprecated/auto_parallel/CMakeLists.txt index c9f7c76c945acf..a3570c556e0ef7 100644 --- a/test/deprecated/auto_parallel/CMakeLists.txt +++ b/test/deprecated/auto_parallel/CMakeLists.txt @@ -129,8 +129,6 @@ if(WITH_DISTRIBUTE AND WITH_GPU) test_dist_op_cost_deprecated) py_test_modules(test_cost_interface_deprecated MODULES test_cost_interface_deprecated) - py_test_modules(test_conditional_block_reshard_deprecated MODULES - test_conditional_block_reshard_deprecated) py_test_modules(test_base_cost_deprecated MODULES test_base_cost_deprecated) py_test_modules(test_auto_conditional_block_deprecated MODULES test_auto_conditional_block_deprecated) diff --git a/test/deprecated/auto_parallel/test_conditional_block_reshard_deprecated.py b/test/deprecated/auto_parallel/test_conditional_block_reshard_deprecated.py deleted file mode 100644 index 4a50138752621e..00000000000000 --- a/test/deprecated/auto_parallel/test_conditional_block_reshard_deprecated.py +++ /dev/null @@ -1,101 +0,0 @@ -# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest - -import paddle -import paddle.nn.functional as F -from paddle import nn -from paddle.distributed.fleet import auto -from paddle.static import InputSpec - - -class MLPLayer(nn.Layer): - def __init__( - self, hidden_size=64, intermediate_size=4 * 64, initializer_range=0.02 - ): - super().__init__() - self.norm = nn.LayerNorm(hidden_size, epsilon=1e-5) - self.linear0 = nn.Linear( - hidden_size, - intermediate_size, - paddle.ParamAttr( - initializer=nn.initializer.Normal( - mean=0.0, std=initializer_range - ) - ), - bias_attr=None, - ) - self.linear1 = nn.Linear( - intermediate_size, - hidden_size, - paddle.ParamAttr( - initializer=nn.initializer.Normal( - mean=0.0, std=initializer_range - ) - ), - bias_attr=None, - ) - - def forward(self, input): - out = self.norm(input) - - auto.shard_tensor( - self.linear0.weight, auto.ProcessMesh([0, 1], ["x"]), [None, "x"] - ) - out = self.linear0(out) - out = F.gelu(out, approximate=True) - - auto.shard_tensor( - self.linear1.weight, auto.ProcessMesh([0, 1], ["x"]), ["x", None] - ) - out = self.linear1(out) - - if paddle.mean(out) < 2: - out = self.norm(out) - out = self.linear0(out) - out = F.gelu(out, approximate=True) - out = self.linear1(out) - else: - out = self.norm(out) - out = self.linear0(out) - out = self.linear1(out) - - return out - - -def loss_fn(predict, label): - error_cost = paddle.nn.functional.square_error_cost(predict, label) - loss = paddle.mean(error_cost) - return loss - - -class TestSubblock(unittest.TestCase): - def test_subblock(self): - mlp = MLPLayer() - - strategy = auto.Strategy() - strategy.auto_mode = "semi" - - engine = auto.Engine(model=mlp, loss=loss_fn, strategy=strategy) - - input_spec = InputSpec([4, 64], 'float32', 'input') - label_spec = InputSpec([4, 1], 'float32', 'label') - engine.prepare( - inputs_spec=[input_spec], labels_spec=[label_spec], mode="predict" - ) - - -if __name__ == "__main__": - unittest.main() From 93fef563a78c32ecef5615a911a3465d61c78072 Mon Sep 17 00:00:00 2001 From: zhengshengning Date: Sun, 24 Aug 2025 17:57:22 +0800 Subject: [PATCH 13/27] PyObject can be a mixed type in static image mode --- .../pir/dialect/op_generator/python_c_gen.py | 21 +- paddle/fluid/pybind/arg_pre_process.cc | 9 +- paddle/fluid/pybind/arg_pre_process.h | 12 +- paddle/fluid/pybind/eager_utils.cc | 206 ++++++++++++ paddle/fluid/pybind/eager_utils.h | 6 + paddle/phi/ops/yaml/ops.yaml | 6 + python/paddle/_paddle_docs.py | 100 ++++++ python/paddle/tensor/math.py | 299 +++++++++--------- 8 files changed, 505 insertions(+), 154 deletions(-) diff --git a/paddle/fluid/pir/dialect/op_generator/python_c_gen.py b/paddle/fluid/pir/dialect/op_generator/python_c_gen.py index a86553a3f33f75..16b4e9323a9d53 100644 --- a/paddle/fluid/pir/dialect/op_generator/python_c_gen.py +++ b/paddle/fluid/pir/dialect/op_generator/python_c_gen.py @@ -211,6 +211,8 @@ {mutable_cast_attrs} }}else if (PyObject_CheckIRVectorOfValue({name}_obj)){{ {mutable_vector_cast_attrs} + }}else if (PyObject_CheckIRVectorOfValueOrLong({name}_obj)){{ + {mix_vector_cast_attrs} }}else{{ {no_mutable_cast_attrs} }}""" @@ -219,9 +221,9 @@ PyObject *{name}_obj = PyTuple_GET_ITEM(args, {index});""" MUTABLE_ATTR_OBJ_FROM_ARGS_KWARGS_WITH_DEFAULT_VALUE_TEMPLATE = """ - PyObject *{name}_obj = GetItemFromArgsOrKWArgs(args, {index},kwargs,{keywords}, nargs, &remaining_kwargs,false);""" -MUTABLE_ATTR_OBJ_FROM_ARGS_KWARGS_TEMPLATE = """ PyObject *{name}_obj = GetItemFromArgsOrKWArgs(args, {index},kwargs,{keywords}, nargs, &remaining_kwargs);""" +MUTABLE_ATTR_OBJ_FROM_ARGS_KWARGS_TEMPLATE = """ + PyObject *{name}_obj = GetItemFromArgsOrKWArgs(args, {index},kwargs,{keywords}, nargs, &remaining_kwargs,false);""" MUTABLE_ATTR_CAST_TEMPLATE = """ {type} {name_} = {cast_func}({name}_obj, "{api_name}", {index});""" @@ -259,7 +261,7 @@ "paddle::Place": "CastPyArg2Place", "phi::Place": "CastPyArg2Place", "Place": "CastPyArg2Place", - "phi::DataType": "CastPyArg2DataTypeDirectly", + "phi::DataType": "CastPyArg2DataType", } TYPE_TO_PHI_DATATYPE_MAP = { @@ -501,6 +503,18 @@ def _gen_cast_attrs(self, op_info, op_name): name=name ) + mix_vector_cast_str = MUTABLE_ATTR_CAST_TEMPLATE.format( + type='std::vector', + name_=name + '_tmp', + name=name, + cast_func='CastPyArg2VectorOfValueOrLong', + api_name=op_name, + index=input_size + i, + ) + mix_vector_cast_str += BUILTIN_STACK_OP_TEMPLATE.format( + name=name + ) + else: mutable_cast_str = MUTABLE_ATTR_CAST_TEMPLATE.format( type='', @@ -546,6 +560,7 @@ def _gen_cast_attrs(self, op_info, op_name): name=name, mutable_cast_attrs=mutable_cast_str, mutable_vector_cast_attrs=mutable_vector_cast_str, + mix_vector_cast_attrs=mix_vector_cast_str, no_mutable_cast_attrs=no_mutable_cast_str, ) else: diff --git a/paddle/fluid/pybind/arg_pre_process.cc b/paddle/fluid/pybind/arg_pre_process.cc index 1dd1e8c70e3c07..2695ac82c7c83d 100644 --- a/paddle/fluid/pybind/arg_pre_process.cc +++ b/paddle/fluid/pybind/arg_pre_process.cc @@ -19,11 +19,18 @@ // processing of parameters originally done in the Python API #include "paddle/fluid/pybind/arg_pre_process.h" #include "paddle/fluid/eager/utils.h" +#include "paddle/fluid/pir/dialect/operator/utils/utils.h" +#include "paddle/fluid/pir/utils/general_functions.h" #include "paddle/fluid/pybind/eager_utils.h" #include "paddle/fluid/pybind/op_function_common.h" #include "paddle/phi/common/data_type.h" #include "paddle/phi/core/enforce.h" namespace paddle { -namespace pybind {} // namespace pybind +namespace pybind { +void SumPreProcess(Tensor* x, IntArray* axis) {} +void SumPreProcess(Value* x, Value* axis) { + paddle::dialect::SetStopGradient(x); +} +} // namespace pybind } // namespace paddle diff --git a/paddle/fluid/pybind/arg_pre_process.h b/paddle/fluid/pybind/arg_pre_process.h index 557b6d1c5f4739..0ae2e6496d15a0 100644 --- a/paddle/fluid/pybind/arg_pre_process.h +++ b/paddle/fluid/pybind/arg_pre_process.h @@ -15,9 +15,19 @@ #pragma once #include +#include "paddle/fluid/ir_adaptor/translator/program_translator.h" +#include "paddle/phi/api/include/tensor.h" namespace paddle { -namespace pybind {} // namespace pybind +namespace pybind { +using Tensor = paddle::Tensor; +using Value = pir::Value; +using IntArray = paddle::experimental::IntArray; +using IntVector = std::vector; + +void SumPreProcess(Tensor* x, IntArray* axis); +void SumPreProcess(Value* x, Value* axis); +} // namespace pybind } // namespace paddle diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index 0491b31e688841..2804312130d774 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -29,6 +29,7 @@ limitations under the License. */ #include "paddle/fluid/jit/function.h" #include "paddle/fluid/pir/dialect/distributed/ir/dist_type.h" #include "paddle/fluid/pir/dialect/operator/ir/op_type.h" +#include "paddle/fluid/pir/dialect/operator/ir/pd_api.h" #include "paddle/fluid/pir/dialect/operator/utils/utils.h" #include "paddle/fluid/pir/utils/name_analysis.h" #include "paddle/fluid/platform/enforce.h" @@ -232,6 +233,39 @@ bool PyObject_CheckIRVectorOfValue(PyObject* obj) { } } +bool PyObject_CheckIRVectorOfValueOrLong(PyObject* obj) { + if (!PyList_Check(obj) && !PyTuple_Check(obj)) { + return false; + } + + Py_ssize_t len = PySequence_Size(obj); + if (len == 0) { + return false; + } + + bool is_ir_value = false, is_long = false; + + for (Py_ssize_t i = 0; i < len; ++i) { + PyObject* item = PySequence_GetItem(obj, i); // Returns new reference + if (!item) { + return false; + } + + if (PyObject_CheckIRValue(item)) { + is_ir_value = true; + } else if (PyObject_CheckLong(item)) { + is_long = true; + } else { + Py_DECREF(item); + return false; + } + + Py_DECREF(item); // Because PySequence_GetItem returns new reference + } + + return is_ir_value && is_long; +} + bool CastPyArg2AttrBoolean(PyObject* obj, ssize_t arg_pos) { if (obj == Py_None || obj == Py_False) { return false; // To be compatible with QA integration testing. Some @@ -2276,6 +2310,178 @@ std::vector CastPyArg2VectorOfValue(PyObject* obj, return value_list; } +// std::vector CastPyArg2VectorOfValueOrLong(PyObject* obj, +// const std::string& op_type, +// size_t arg_pos, +// bool dispensable) { +// std::vector value_list; +// if (PyList_Check(obj)) { +// Py_ssize_t len = PyList_Size(obj); +// if (len == 0 && !dispensable) { +// PADDLE_THROW(common::errors::InvalidArgument( +// "%s(): argument (position %d) must be " +// "list of Value, but got empty list", +// op_type, +// arg_pos + 1)); +// } +// PyObject* item = nullptr; +// phi::DataType dtype = phi::DataType::INT64; +// for (Py_ssize_t i = 0; i < len; i++) { +// PyObject* t_item = PyList_GetItem(obj, i); +// if (PyObject_TypeCheck(t_item, g_ir_value_pytype)) { +// dtype = +// paddle::dialect::GetValueDataType(::pybind11::handle(t_item).cast()); +// } +// } +// for (Py_ssize_t i = 0; i < len; i++) { +// item = PyList_GetItem(obj, i); +// item = CastPyArg2ValuePreHook(item); +// if (PyObject_TypeCheck(item, g_ir_value_pytype)) { +// value_list.emplace_back(::pybind11::handle(item).cast()); +// } else if(PyLong_Check(item)) { +// int64_t k_tmp = CastPyArg2Long(item, op_type, arg_pos); +// value_list.emplace_back(paddle::dialect::full(std::vector{1}, +// k_tmp, dtype, phi::CPUPlace())); +// } else if (item == Py_None) { +// continue; +// } else { +// PADDLE_THROW(common::errors::InvalidType( +// "%s(): argument (position %d) must be " +// "vector, but got vector<%s>", +// op_type, +// arg_pos + 1, +// reinterpret_cast(item->ob_type) +// ->tp_name)); // NOLINT +// } +// } +// } else if (PyTuple_Check(obj)) { +// Py_ssize_t len = PyTuple_Size(obj); +// if (len == 0 && !dispensable) { +// PADDLE_THROW(common::errors::InvalidArgument( +// "%s(): argument (position %d) must be " +// "list of Value, but got empty list", +// op_type, +// arg_pos + 1)); +// } +// PyObject* item = nullptr; +// phi::DataType dtype = phi::DataType::INT64; +// for (Py_ssize_t i = 0; i < len; i++) { +// PyObject* t_item = PyTuple_GetItem(obj, i); +// if (PyObject_TypeCheck(t_item, g_ir_value_pytype)) { +// dtype = +// paddle::dialect::GetValueDataType(::pybind11::handle(t_item).cast()); +// } +// } +// for (Py_ssize_t i = 0; i < len; i++) { +// item = PyTuple_GetItem(obj, i); +// item = CastPyArg2ValuePreHook(item); +// if (PyObject_TypeCheck(item, g_ir_value_pytype)) { +// value_list.emplace_back(::pybind11::handle(item).cast()); +// } else if(PyLong_Check(item)) { +// int64_t k_tmp = CastPyArg2Long(item, op_type, arg_pos); +// value_list.emplace_back(paddle::dialect::full(std::vector{1}, +// k_tmp, dtype, phi::CPUPlace())); +// } else if (item == Py_None) { +// continue; +// } else { +// PADDLE_THROW(common::errors::InvalidType( +// "%s(): argument (position %d) must be " +// "vector, but got vector<%s>", +// op_type, +// arg_pos + 1, +// reinterpret_cast(item->ob_type) +// ->tp_name)); // NOLINT +// } +// } +// } else { +// PADDLE_THROW(common::errors::InvalidType( +// "%s(): argument (position %d) must be " +// "Vector<>, but got %s", +// op_type, +// arg_pos + 1, +// ((PyTypeObject*)obj->ob_type)->tp_name)); // NOLINT +// } +// return value_list; +// } + +std::vector CastPyArg2VectorOfValueOrLong( + PyObject* obj, + const std::string& op_type, + size_t arg_pos, + bool dispensable) { + std::vector value_list; + + if (!PyList_Check(obj) && !PyTuple_Check(obj)) { + PADDLE_THROW(common::errors::InvalidType( + "%s(): argument (position %d) must be " + "Vector<>, but got %s", + op_type, + arg_pos + 1, + reinterpret_cast(obj->ob_type)->tp_name)); + } + + Py_ssize_t len = PySequence_Size(obj); + if (len == 0 && !dispensable) { + PADDLE_THROW( + common::errors::InvalidArgument("%s(): argument (position %d) must be " + "list of Value, but got empty list", + op_type, + arg_pos + 1)); + } + + phi::DataType dtype = phi::DataType::INT64; + for (Py_ssize_t i = 0; i < len; ++i) { + PyObject* item = PySequence_GetItem(obj, i); + if (!item) { + continue; + } + + item = CastPyArg2ValuePreHook(item); + + if (PyObject_TypeCheck(item, g_ir_value_pytype)) { + pir::Value val = ::pybind11::handle(item).cast(); + dtype = paddle::dialect::GetValueDataType(val); + Py_DECREF(item); + break; // 找到第一个 pir::Value 后退出 + } + + Py_DECREF(item); + } + + for (Py_ssize_t i = 0; i < len; ++i) { + PyObject* item = PySequence_GetItem(obj, i); + if (!item) { + PADDLE_THROW(common::errors::Fatal( + "%s(): failed to get item from sequence at position %d", + op_type, + static_cast(i))); + } + + item = CastPyArg2ValuePreHook(item); + + if (PyObject_TypeCheck(item, g_ir_value_pytype)) { + value_list.emplace_back(::pybind11::handle(item).cast()); + } else if (PyLong_Check(item)) { + int64_t k_tmp = CastPyArg2Long(item, op_type, arg_pos); + value_list.emplace_back(paddle::dialect::full( + std::vector{1}, k_tmp, dtype, phi::CPUPlace())); + } else if (item == Py_None) { + // skip + } else { + PADDLE_THROW(common::errors::InvalidType( + "%s(): argument (position %d) must be vector, " + "but got vector<%s>", + op_type, + arg_pos + 1, + reinterpret_cast(item->ob_type)->tp_name)); + } + + Py_DECREF(item); + } + + return value_list; +} + paddle::optional> CastPyArg2OptionalVectorOfValue( PyObject* obj, const std::string& op_type, diff --git a/paddle/fluid/pybind/eager_utils.h b/paddle/fluid/pybind/eager_utils.h index 0dbc47d46ed5ed..ef964e34a3ff4f 100644 --- a/paddle/fluid/pybind/eager_utils.h +++ b/paddle/fluid/pybind/eager_utils.h @@ -67,6 +67,7 @@ int TensorDtype2NumpyDtype(phi::DataType dtype); bool PyObject_CheckStr(PyObject* obj); bool PyObject_CheckIRValue(PyObject* obj); bool PyObject_CheckIRVectorOfValue(PyObject* obj); +bool PyObject_CheckIRVectorOfValueOrLong(PyObject* obj); bool CastPyArg2AttrBoolean(PyObject* obj, ssize_t arg_pos); int CastPyArg2AttrInt(PyObject* obj, ssize_t arg_pos); int64_t CastPyArg2AttrLong(PyObject* obj, ssize_t arg_pos); @@ -100,6 +101,11 @@ std::vector CastPyArg2VectorOfValue(PyObject* obj, const std::string& op_type, size_t arg_pos, bool dispensable = false); +std::vector CastPyArg2VectorOfValueOrLong( + PyObject* obj, + const std::string& op_type, + size_t arg_pos, + bool dispensable = false); paddle::optional> CastPyArg2OptionalVectorOfValue( PyObject* obj, const std::string& op_type, diff --git a/paddle/phi/ops/yaml/ops.yaml b/paddle/phi/ops/yaml/ops.yaml index 44c5fdf0b53c58..f410668540c1f7 100644 --- a/paddle/phi/ops/yaml/ops.yaml +++ b/paddle/phi/ops/yaml/ops.yaml @@ -5219,6 +5219,12 @@ - op : sum args : (Tensor x, IntArray axis={}, DataType dtype=DataType::UNDEFINED, bool keepdim=false) + python_api: + name : [paddle.sum, paddle.Tensor.sum] + args_alias: + use_default_mapping : True + pre_process: + func : SumPreProcess(x, axis) output : Tensor(out) infer_meta : func : SumInferMeta diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index 4ff0c48e3edfe5..125cbd7a666567 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -402,6 +402,106 @@ def all( ) # zhengsheng +add_doc_and_signature( + "sum", + """ + Computes the sum of tensor elements over the given dimension. + + Args: + x (Tensor): An N-D Tensor, the data type is bool, bfloat16, float16, float32, float64, + uint8, int8, int16, int32, int64, complex64, complex128. + axis (int|list|tuple|None, optional): The dimensions along which the sum is performed. If + :attr:`None`, sum all elements of :attr:`x` and return a + Tensor with a single element, otherwise must be in the + range :math:`[-rank(x), rank(x))`. If :math:`axis[i] < 0`, + the dimension to reduce is :math:`rank + axis[i]`. + dtype (str|paddle.dtype|np.dtype, optional): The dtype of output Tensor. The default value is None, the dtype + of output is the same as input Tensor `x`. + keepdim (bool, optional): Whether to reserve the reduced dimension in the + output Tensor. The result Tensor will have one fewer dimension + than the :attr:`x` unless :attr:`keepdim` is true, default + value is False. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + + Returns: + Tensor: Results of summation operation on the specified axis of input Tensor `x`, + if `x.dtype='bool'`, `x.dtype='int32'`, it's data type is `'int64'`, + otherwise it's data type is the same as `x`. + + Examples: + .. code-block:: python + >>> # type: ignore + >>> import paddle + + >>> # x is a Tensor with following elements: + >>> # [[0.2, 0.3, 0.5, 0.9] + >>> # [0.1, 0.2, 0.6, 0.7]] + >>> # Each example is followed by the corresponding output tensor. + >>> x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9], + ... [0.1, 0.2, 0.6, 0.7]]) + >>> out1 = paddle.sum(x) + >>> out1 + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 3.50000000) + >>> out2 = paddle.sum(x, axis=0) + >>> out2 + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [0.30000001, 0.50000000, 1.10000002, 1.59999990]) + >>> out3 = paddle.sum(x, axis=-1) + >>> out3 + Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, + [1.89999998, 1.60000002]) + >>> out4 = paddle.sum(x, axis=1, keepdim=True) + >>> out4 + Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True, + [[1.89999998], + [1.60000002]]) + + >>> # y is a Tensor with shape [2, 2, 2] and elements as below: + >>> # [[[1, 2], [3, 4]], + >>> # [[5, 6], [7, 8]]] + >>> # Each example is followed by the corresponding output tensor. + >>> y = paddle.to_tensor([[[1, 2], [3, 4]], + ... [[5, 6], [7, 8]]]) + >>> out5 = paddle.sum(y, axis=[1, 2]) + >>> out5 + Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, + [10, 26]) + >>> out6 = paddle.sum(y, axis=[0, 1]) + >>> out6 + Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, + [16, 20]) + + >>> # x is a Tensor with following elements: + >>> # [[True, True, True, True] + >>> # [False, False, False, False]] + >>> # Each example is followed by the corresponding output tensor. + >>> x = paddle.to_tensor([[True, True, True, True], + ... [False, False, False, False]]) + >>> out7 = paddle.sum(x) + >>> out7 + Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True, + 4) + >>> out8 = paddle.sum(x, axis=0) + >>> out8 + Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True, + [1, 1, 1, 1]) + >>> out9 = paddle.sum(x, axis=1) + >>> out9 + Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, + [4, 0]) + + """, + """ +def sum( + x: Tensor, + axis: int | Sequence[int] | None = None, + dtype: DTypeLike | None = None, + keepdim: bool = False, + name: str | None = None, +) -> Tensor + """, +) # liuyi diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 68901d7fc0e8a0..687692a09e0ac0 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -26,6 +26,7 @@ all, amax, amin, + sum, ) from paddle.base.libpaddle import DataType from paddle.common_ops_import import VarDesc, dygraph_utils @@ -1623,155 +1624,155 @@ def fmin(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: return _elementwise_op(LayerHelper('elementwise_fmin', **locals())) -def sum( - x: Tensor, - axis: int | Sequence[int] | None = None, - dtype: DTypeLike | None = None, - keepdim: bool = False, - name: str | None = None, -) -> Tensor: - """ - Computes the sum of tensor elements over the given dimension. - - Args: - x (Tensor): An N-D Tensor, the data type is bool, bfloat16, float16, float32, float64, - uint8, int8, int16, int32, int64, complex64, complex128. - axis (int|list|tuple|None, optional): The dimensions along which the sum is performed. If - :attr:`None`, sum all elements of :attr:`x` and return a - Tensor with a single element, otherwise must be in the - range :math:`[-rank(x), rank(x))`. If :math:`axis[i] < 0`, - the dimension to reduce is :math:`rank + axis[i]`. - dtype (str|paddle.dtype|np.dtype, optional): The dtype of output Tensor. The default value is None, the dtype - of output is the same as input Tensor `x`. - keepdim (bool, optional): Whether to reserve the reduced dimension in the - output Tensor. The result Tensor will have one fewer dimension - than the :attr:`x` unless :attr:`keepdim` is true, default - value is False. - name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. - - Returns: - Tensor: Results of summation operation on the specified axis of input Tensor `x`, - if `x.dtype='bool'`, `x.dtype='int32'`, it's data type is `'int64'`, - otherwise it's data type is the same as `x`. - - Examples: - .. code-block:: python - - >>> import paddle - - >>> # x is a Tensor with following elements: - >>> # [[0.2, 0.3, 0.5, 0.9] - >>> # [0.1, 0.2, 0.6, 0.7]] - >>> # Each example is followed by the corresponding output tensor. - >>> x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9], - ... [0.1, 0.2, 0.6, 0.7]]) - >>> out1 = paddle.sum(x) - >>> out1 - Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, - 3.50000000) - >>> out2 = paddle.sum(x, axis=0) - >>> out2 - Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, - [0.30000001, 0.50000000, 1.10000002, 1.59999990]) - >>> out3 = paddle.sum(x, axis=-1) - >>> out3 - Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, - [1.89999998, 1.60000002]) - >>> out4 = paddle.sum(x, axis=1, keepdim=True) - >>> out4 - Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True, - [[1.89999998], - [1.60000002]]) - - >>> # y is a Tensor with shape [2, 2, 2] and elements as below: - >>> # [[[1, 2], [3, 4]], - >>> # [[5, 6], [7, 8]]] - >>> # Each example is followed by the corresponding output tensor. - >>> y = paddle.to_tensor([[[1, 2], [3, 4]], - ... [[5, 6], [7, 8]]]) - >>> out5 = paddle.sum(y, axis=[1, 2]) - >>> out5 - Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, - [10, 26]) - >>> out6 = paddle.sum(y, axis=[0, 1]) - >>> out6 - Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, - [16, 20]) - - >>> # x is a Tensor with following elements: - >>> # [[True, True, True, True] - >>> # [False, False, False, False]] - >>> # Each example is followed by the corresponding output tensor. - >>> x = paddle.to_tensor([[True, True, True, True], - ... [False, False, False, False]]) - >>> out7 = paddle.sum(x) - >>> out7 - Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True, - 4) - >>> out8 = paddle.sum(x, axis=0) - >>> out8 - Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True, - [1, 1, 1, 1]) - >>> out9 = paddle.sum(x, axis=1) - >>> out9 - Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, - [4, 0]) - """ - - dtype_flag = False - if dtype is not None: - dtype_flag = True - if not isinstance(dtype, paddle.dtype): - dtype = convert_np_dtype_to_dtype_(dtype) - - if in_dynamic_mode(): - return _C_ops.sum(x, axis, dtype, keepdim) - else: - reduce_all, axis = _get_reduce_axis_with_tensor(axis, x) - if in_pir_mode(): - return _C_ops.sum(x, axis, dtype, keepdim) - else: - attrs = {'dim': axis, 'keep_dim': keepdim} - - if dtype_flag: - attrs.update({'in_dtype': x.dtype, 'out_dtype': dtype}) - - check_variable_and_dtype( - x, - 'x', - [ - 'bool', - 'uint16', - 'int8', - 'uint8', - 'float16', - 'float32', - 'float64', - 'int16', - 'int32', - 'int64', - 'complex64', - 'complex128', - ], - 'sum', - ) - - check_type( - axis, 'axis', (int, list, tuple, type(None), Variable), 'sum' - ) - - helper = LayerHelper('sum', **locals()) - if dtype_flag: - out = helper.create_variable_for_type_inference(dtype=dtype) - else: - out = helper.create_variable_for_type_inference(dtype=x.dtype) - helper.append_op( - type='reduce_sum', - inputs={'X': x}, - outputs={'Out': out}, - attrs=attrs, - ) - return out +# def sum( +# x: Tensor, +# axis: int | Sequence[int] | None = None, +# dtype: DTypeLike | None = None, +# keepdim: bool = False, +# name: str | None = None, +# ) -> Tensor: +# """ +# Computes the sum of tensor elements over the given dimension. + +# Args: +# x (Tensor): An N-D Tensor, the data type is bool, bfloat16, float16, float32, float64, +# uint8, int8, int16, int32, int64, complex64, complex128. +# axis (int|list|tuple|None, optional): The dimensions along which the sum is performed. If +# :attr:`None`, sum all elements of :attr:`x` and return a +# Tensor with a single element, otherwise must be in the +# range :math:`[-rank(x), rank(x))`. If :math:`axis[i] < 0`, +# the dimension to reduce is :math:`rank + axis[i]`. +# dtype (str|paddle.dtype|np.dtype, optional): The dtype of output Tensor. The default value is None, the dtype +# of output is the same as input Tensor `x`. +# keepdim (bool, optional): Whether to reserve the reduced dimension in the +# output Tensor. The result Tensor will have one fewer dimension +# than the :attr:`x` unless :attr:`keepdim` is true, default +# value is False. +# name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + +# Returns: +# Tensor: Results of summation operation on the specified axis of input Tensor `x`, +# if `x.dtype='bool'`, `x.dtype='int32'`, it's data type is `'int64'`, +# otherwise it's data type is the same as `x`. + +# Examples: +# .. code-block:: python + +# >>> import paddle + +# >>> # x is a Tensor with following elements: +# >>> # [[0.2, 0.3, 0.5, 0.9] +# >>> # [0.1, 0.2, 0.6, 0.7]] +# >>> # Each example is followed by the corresponding output tensor. +# >>> x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9], +# ... [0.1, 0.2, 0.6, 0.7]]) +# >>> out1 = paddle.sum(x) +# >>> out1 +# Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, +# 3.50000000) +# >>> out2 = paddle.sum(x, axis=0) +# >>> out2 +# Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, +# [0.30000001, 0.50000000, 1.10000002, 1.59999990]) +# >>> out3 = paddle.sum(x, axis=-1) +# >>> out3 +# Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, +# [1.89999998, 1.60000002]) +# >>> out4 = paddle.sum(x, axis=1, keepdim=True) +# >>> out4 +# Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True, +# [[1.89999998], +# [1.60000002]]) + +# >>> # y is a Tensor with shape [2, 2, 2] and elements as below: +# >>> # [[[1, 2], [3, 4]], +# >>> # [[5, 6], [7, 8]]] +# >>> # Each example is followed by the corresponding output tensor. +# >>> y = paddle.to_tensor([[[1, 2], [3, 4]], +# ... [[5, 6], [7, 8]]]) +# >>> out5 = paddle.sum(y, axis=[1, 2]) +# >>> out5 +# Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, +# [10, 26]) +# >>> out6 = paddle.sum(y, axis=[0, 1]) +# >>> out6 +# Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, +# [16, 20]) + +# >>> # x is a Tensor with following elements: +# >>> # [[True, True, True, True] +# >>> # [False, False, False, False]] +# >>> # Each example is followed by the corresponding output tensor. +# >>> x = paddle.to_tensor([[True, True, True, True], +# ... [False, False, False, False]]) +# >>> out7 = paddle.sum(x) +# >>> out7 +# Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True, +# 4) +# >>> out8 = paddle.sum(x, axis=0) +# >>> out8 +# Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True, +# [1, 1, 1, 1]) +# >>> out9 = paddle.sum(x, axis=1) +# >>> out9 +# Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, +# [4, 0]) +# """ + +# dtype_flag = False +# if dtype is not None: +# dtype_flag = True +# if not isinstance(dtype, paddle.dtype): +# dtype = convert_np_dtype_to_dtype_(dtype) + +# if in_dynamic_mode(): +# return _C_ops.sum(x, axis, dtype, keepdim) +# else: +# reduce_all, axis = _get_reduce_axis_with_tensor(axis, x) +# if in_pir_mode(): +# return _C_ops.sum(x, axis, dtype, keepdim) +# else: +# attrs = {'dim': axis, 'keep_dim': keepdim} + +# if dtype_flag: +# attrs.update({'in_dtype': x.dtype, 'out_dtype': dtype}) + +# check_variable_and_dtype( +# x, +# 'x', +# [ +# 'bool', +# 'uint16', +# 'int8', +# 'uint8', +# 'float16', +# 'float32', +# 'float64', +# 'int16', +# 'int32', +# 'int64', +# 'complex64', +# 'complex128', +# ], +# 'sum', +# ) + +# check_type( +# axis, 'axis', (int, list, tuple, type(None), Variable), 'sum' +# ) + +# helper = LayerHelper('sum', **locals()) +# if dtype_flag: +# out = helper.create_variable_for_type_inference(dtype=dtype) +# else: +# out = helper.create_variable_for_type_inference(dtype=x.dtype) +# helper.append_op( +# type='reduce_sum', +# inputs={'X': x}, +# outputs={'Out': out}, +# attrs=attrs, +# ) +# return out def reduce_as(x: Tensor, target: Tensor, name: str | None = None) -> Tensor: From c958dcdab4b1804e5f6e69efe77a6c44c3f98d22 Mon Sep 17 00:00:00 2001 From: zhengshengning Date: Sun, 24 Aug 2025 18:20:56 +0800 Subject: [PATCH 14/27] Delete invalid code --- paddle/fluid/pybind/eager_utils.cc | 94 ------------------------------ 1 file changed, 94 deletions(-) diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index 2804312130d774..83763852d9ee68 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -2310,100 +2310,6 @@ std::vector CastPyArg2VectorOfValue(PyObject* obj, return value_list; } -// std::vector CastPyArg2VectorOfValueOrLong(PyObject* obj, -// const std::string& op_type, -// size_t arg_pos, -// bool dispensable) { -// std::vector value_list; -// if (PyList_Check(obj)) { -// Py_ssize_t len = PyList_Size(obj); -// if (len == 0 && !dispensable) { -// PADDLE_THROW(common::errors::InvalidArgument( -// "%s(): argument (position %d) must be " -// "list of Value, but got empty list", -// op_type, -// arg_pos + 1)); -// } -// PyObject* item = nullptr; -// phi::DataType dtype = phi::DataType::INT64; -// for (Py_ssize_t i = 0; i < len; i++) { -// PyObject* t_item = PyList_GetItem(obj, i); -// if (PyObject_TypeCheck(t_item, g_ir_value_pytype)) { -// dtype = -// paddle::dialect::GetValueDataType(::pybind11::handle(t_item).cast()); -// } -// } -// for (Py_ssize_t i = 0; i < len; i++) { -// item = PyList_GetItem(obj, i); -// item = CastPyArg2ValuePreHook(item); -// if (PyObject_TypeCheck(item, g_ir_value_pytype)) { -// value_list.emplace_back(::pybind11::handle(item).cast()); -// } else if(PyLong_Check(item)) { -// int64_t k_tmp = CastPyArg2Long(item, op_type, arg_pos); -// value_list.emplace_back(paddle::dialect::full(std::vector{1}, -// k_tmp, dtype, phi::CPUPlace())); -// } else if (item == Py_None) { -// continue; -// } else { -// PADDLE_THROW(common::errors::InvalidType( -// "%s(): argument (position %d) must be " -// "vector, but got vector<%s>", -// op_type, -// arg_pos + 1, -// reinterpret_cast(item->ob_type) -// ->tp_name)); // NOLINT -// } -// } -// } else if (PyTuple_Check(obj)) { -// Py_ssize_t len = PyTuple_Size(obj); -// if (len == 0 && !dispensable) { -// PADDLE_THROW(common::errors::InvalidArgument( -// "%s(): argument (position %d) must be " -// "list of Value, but got empty list", -// op_type, -// arg_pos + 1)); -// } -// PyObject* item = nullptr; -// phi::DataType dtype = phi::DataType::INT64; -// for (Py_ssize_t i = 0; i < len; i++) { -// PyObject* t_item = PyTuple_GetItem(obj, i); -// if (PyObject_TypeCheck(t_item, g_ir_value_pytype)) { -// dtype = -// paddle::dialect::GetValueDataType(::pybind11::handle(t_item).cast()); -// } -// } -// for (Py_ssize_t i = 0; i < len; i++) { -// item = PyTuple_GetItem(obj, i); -// item = CastPyArg2ValuePreHook(item); -// if (PyObject_TypeCheck(item, g_ir_value_pytype)) { -// value_list.emplace_back(::pybind11::handle(item).cast()); -// } else if(PyLong_Check(item)) { -// int64_t k_tmp = CastPyArg2Long(item, op_type, arg_pos); -// value_list.emplace_back(paddle::dialect::full(std::vector{1}, -// k_tmp, dtype, phi::CPUPlace())); -// } else if (item == Py_None) { -// continue; -// } else { -// PADDLE_THROW(common::errors::InvalidType( -// "%s(): argument (position %d) must be " -// "vector, but got vector<%s>", -// op_type, -// arg_pos + 1, -// reinterpret_cast(item->ob_type) -// ->tp_name)); // NOLINT -// } -// } -// } else { -// PADDLE_THROW(common::errors::InvalidType( -// "%s(): argument (position %d) must be " -// "Vector<>, but got %s", -// op_type, -// arg_pos + 1, -// ((PyTypeObject*)obj->ob_type)->tp_name)); // NOLINT -// } -// return value_list; -// } - std::vector CastPyArg2VectorOfValueOrLong( PyObject* obj, const std::string& op_type, From 3625362d21fd8db5b633a33e0d4eedd2234374ba Mon Sep 17 00:00:00 2001 From: zhengshengning Date: Sun, 24 Aug 2025 21:38:42 +0800 Subject: [PATCH 15/27] remove sum test --- paddle/phi/ops/yaml/ops.yaml | 6 -- python/paddle/_paddle_docs.py | 100 ----------------------- python/paddle/tensor/math.py | 145 +++++++++++++++++++++++++++++++++- 3 files changed, 144 insertions(+), 107 deletions(-) diff --git a/paddle/phi/ops/yaml/ops.yaml b/paddle/phi/ops/yaml/ops.yaml index 3883f40eb1c753..d89552ba46ac47 100644 --- a/paddle/phi/ops/yaml/ops.yaml +++ b/paddle/phi/ops/yaml/ops.yaml @@ -5256,12 +5256,6 @@ - op : sum args : (Tensor x, IntArray axis={}, DataType dtype=DataType::UNDEFINED, bool keepdim=false) - python_api: - name : [paddle.sum, paddle.Tensor.sum] - args_alias: - use_default_mapping : True - pre_process: - func : SumPreProcess(x, axis) output : Tensor(out) infer_meta : func : SumInferMeta diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index 9e95f30d946367..ad9ab259ff09f7 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -503,106 +503,6 @@ def isnan( """, ) -add_doc_and_signature( - "sum", - """ - Computes the sum of tensor elements over the given dimension. - - Args: - x (Tensor): An N-D Tensor, the data type is bool, bfloat16, float16, float32, float64, - uint8, int8, int16, int32, int64, complex64, complex128. - axis (int|list|tuple|None, optional): The dimensions along which the sum is performed. If - :attr:`None`, sum all elements of :attr:`x` and return a - Tensor with a single element, otherwise must be in the - range :math:`[-rank(x), rank(x))`. If :math:`axis[i] < 0`, - the dimension to reduce is :math:`rank + axis[i]`. - dtype (str|paddle.dtype|np.dtype, optional): The dtype of output Tensor. The default value is None, the dtype - of output is the same as input Tensor `x`. - keepdim (bool, optional): Whether to reserve the reduced dimension in the - output Tensor. The result Tensor will have one fewer dimension - than the :attr:`x` unless :attr:`keepdim` is true, default - value is False. - name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. - - Returns: - Tensor: Results of summation operation on the specified axis of input Tensor `x`, - if `x.dtype='bool'`, `x.dtype='int32'`, it's data type is `'int64'`, - otherwise it's data type is the same as `x`. - - Examples: - .. code-block:: python - - >>> import paddle - - >>> # x is a Tensor with following elements: - >>> # [[0.2, 0.3, 0.5, 0.9] - >>> # [0.1, 0.2, 0.6, 0.7]] - >>> # Each example is followed by the corresponding output tensor. - >>> x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9], - ... [0.1, 0.2, 0.6, 0.7]]) - >>> out1 = paddle.sum(x) - >>> out1 - Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, - 3.50000000) - >>> out2 = paddle.sum(x, axis=0) - >>> out2 - Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, - [0.30000001, 0.50000000, 1.10000002, 1.59999990]) - >>> out3 = paddle.sum(x, axis=-1) - >>> out3 - Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, - [1.89999998, 1.60000002]) - >>> out4 = paddle.sum(x, axis=1, keepdim=True) - >>> out4 - Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True, - [[1.89999998], - [1.60000002]]) - - >>> # y is a Tensor with shape [2, 2, 2] and elements as below: - >>> # [[[1, 2], [3, 4]], - >>> # [[5, 6], [7, 8]]] - >>> # Each example is followed by the corresponding output tensor. - >>> y = paddle.to_tensor([[[1, 2], [3, 4]], - ... [[5, 6], [7, 8]]]) - >>> out5 = paddle.sum(y, axis=[1, 2]) - >>> out5 - Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, - [10, 26]) - >>> out6 = paddle.sum(y, axis=[0, 1]) - >>> out6 - Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, - [16, 20]) - - >>> # x is a Tensor with following elements: - >>> # [[True, True, True, True] - >>> # [False, False, False, False]] - >>> # Each example is followed by the corresponding output tensor. - >>> x = paddle.to_tensor([[True, True, True, True], - ... [False, False, False, False]]) - >>> out7 = paddle.sum(x) - >>> out7 - Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True, - 4) - >>> out8 = paddle.sum(x, axis=0) - >>> out8 - Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True, - [1, 1, 1, 1]) - >>> out9 = paddle.sum(x, axis=1) - >>> out9 - Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, - [4, 0]) - - """, - """ -def sum( - x: Tensor, - axis: int | Sequence[int] | None = None, - dtype: DTypeLike | None = None, - keepdim: bool = False, - name: str | None = None, -) -> Tensor - """, -) # liuyi add_doc_and_signature( "any", diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 34c60f1c9b7ca3..cc8b412e949a8f 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -30,7 +30,6 @@ isfinite, isinf, isnan, - sum, ) from paddle.base.libpaddle import DataType from paddle.common_ops_import import VarDesc, dygraph_utils @@ -1644,6 +1643,150 @@ def fmin(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: return _elementwise_op(LayerHelper('elementwise_fmin', **locals())) +def sum( + x: Tensor, + axis: int | Sequence[int] | None = None, + dtype: DTypeLike | None = None, + keepdim: bool = False, + name: str | None = None, +) -> Tensor: + """ + Computes the sum of tensor elements over the given dimension. + Args: + x (Tensor): An N-D Tensor, the data type is bool, bfloat16, float16, float32, float64, + uint8, int8, int16, int32, int64, complex64, complex128. + axis (int|list|tuple|None, optional): The dimensions along which the sum is performed. If + :attr:`None`, sum all elements of :attr:`x` and return a + Tensor with a single element, otherwise must be in the + range :math:`[-rank(x), rank(x))`. If :math:`axis[i] < 0`, + the dimension to reduce is :math:`rank + axis[i]`. + dtype (str|paddle.dtype|np.dtype, optional): The dtype of output Tensor. The default value is None, the dtype + of output is the same as input Tensor `x`. + keepdim (bool, optional): Whether to reserve the reduced dimension in the + output Tensor. The result Tensor will have one fewer dimension + than the :attr:`x` unless :attr:`keepdim` is true, default + value is False. + name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + Returns: + Tensor: Results of summation operation on the specified axis of input Tensor `x`, + if `x.dtype='bool'`, `x.dtype='int32'`, it's data type is `'int64'`, + otherwise it's data type is the same as `x`. + Examples: + .. code-block:: python + >>> import paddle + >>> # x is a Tensor with following elements: + >>> # [[0.2, 0.3, 0.5, 0.9] + >>> # [0.1, 0.2, 0.6, 0.7]] + >>> # Each example is followed by the corresponding output tensor. + >>> x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9], + ... [0.1, 0.2, 0.6, 0.7]]) + >>> out1 = paddle.sum(x) + >>> out1 + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 3.50000000) + >>> out2 = paddle.sum(x, axis=0) + >>> out2 + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [0.30000001, 0.50000000, 1.10000002, 1.59999990]) + >>> out3 = paddle.sum(x, axis=-1) + >>> out3 + Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, + [1.89999998, 1.60000002]) + >>> out4 = paddle.sum(x, axis=1, keepdim=True) + >>> out4 + Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True, + [[1.89999998], + [1.60000002]]) + >>> # y is a Tensor with shape [2, 2, 2] and elements as below: + >>> # [[[1, 2], [3, 4]], + >>> # [[5, 6], [7, 8]]] + >>> # Each example is followed by the corresponding output tensor. + >>> y = paddle.to_tensor([[[1, 2], [3, 4]], + ... [[5, 6], [7, 8]]]) + >>> out5 = paddle.sum(y, axis=[1, 2]) + >>> out5 + Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, + [10, 26]) + >>> out6 = paddle.sum(y, axis=[0, 1]) + >>> out6 + Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, + [16, 20]) + >>> # x is a Tensor with following elements: + >>> # [[True, True, True, True] + >>> # [False, False, False, False]] + >>> # Each example is followed by the corresponding output tensor. + >>> x = paddle.to_tensor([[True, True, True, True], + ... [False, False, False, False]]) + >>> out7 = paddle.sum(x) + >>> out7 + Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True, + 4) + >>> out8 = paddle.sum(x, axis=0) + >>> out8 + Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True, + [1, 1, 1, 1]) + >>> out9 = paddle.sum(x, axis=1) + >>> out9 + Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, + [4, 0]) + """ + + dtype_flag = False + if dtype is not None: + dtype_flag = True + if not isinstance(dtype, paddle.dtype): + dtype = convert_np_dtype_to_dtype_(dtype) + + if in_dynamic_mode(): + return _C_ops.sum(x, axis, dtype, keepdim) + else: + reduce_all, axis = _get_reduce_axis_with_tensor(axis, x) + if in_pir_mode(): + return _C_ops.sum(x, axis, dtype, keepdim) + else: + attrs = {'dim': axis, 'keep_dim': keepdim} + + if dtype_flag: + attrs.update({'in_dtype': x.dtype, 'out_dtype': dtype}) + + check_variable_and_dtype( + x, + 'x', + [ + 'bool', + 'uint16', + 'int8', + 'uint8', + 'float16', + 'float32', + 'float64', + 'int16', + 'int32', + 'int64', + 'complex64', + 'complex128', + ], + 'sum', + ) + + check_type( + axis, 'axis', (int, list, tuple, type(None), Variable), 'sum' + ) + + helper = LayerHelper('sum', **locals()) + if dtype_flag: + out = helper.create_variable_for_type_inference(dtype=dtype) + else: + out = helper.create_variable_for_type_inference(dtype=x.dtype) + helper.append_op( + type='reduce_sum', + inputs={'X': x}, + outputs={'Out': out}, + attrs=attrs, + ) + return out + + def reduce_as(x: Tensor, target: Tensor, name: str | None = None) -> Tensor: """ Computes the sum of tensor elements make the shape of its result equal to the shape of target. From 7750c5fa7ea961a7b62b40bdcc15234303da7300 Mon Sep 17 00:00:00 2001 From: cangtianhuang Date: Mon, 25 Aug 2025 16:48:21 +0800 Subject: [PATCH 16/27] revert index_select --- paddle/phi/ops/yaml/ops.yaml | 4 - python/paddle/tensor/search.py | 79 +++++++++++++++++++- test/legacy_test/test_index_select_op.py | 94 ------------------------ 3 files changed, 78 insertions(+), 99 deletions(-) diff --git a/paddle/phi/ops/yaml/ops.yaml b/paddle/phi/ops/yaml/ops.yaml index fff012843da168..add0006e7fba8e 100644 --- a/paddle/phi/ops/yaml/ops.yaml +++ b/paddle/phi/ops/yaml/ops.yaml @@ -2876,10 +2876,6 @@ - op : index_select args : (Tensor x, Tensor index, int axis = 0) - python_api: - name : [paddle.index_select, paddle.Tensor.index_select] - args_alias: - use_default_mapping : True output : Tensor(out) infer_meta : func : IndexSelectInferMeta diff --git a/python/paddle/tensor/search.py b/python/paddle/tensor/search.py index db7ae9b1fbf6df..5a40997626ba7b 100755 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -21,7 +21,6 @@ import paddle from paddle import _C_ops -from paddle._C_ops import index_select # noqa: F401 from paddle.common_ops_import import VarDesc, Variable from paddle.utils.decorator_utils import ParamAliasDecorator, param_one_alias from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only @@ -378,6 +377,84 @@ def argmin( return out +def index_select( + x: Tensor, index: Tensor, axis: int = 0, name: str | None = None +) -> Tensor: + """ + + Returns a new tensor which indexes the ``input`` tensor along dimension ``axis`` using + the entries in ``index`` which is a Tensor. The returned tensor has the same number + of dimensions as the original ``x`` tensor. The dim-th dimension has the same + size as the length of ``index``; other dimensions have the same size as in the ``x`` tensor. + + Args: + x (Tensor): The input Tensor to be operated. The data of ``x`` can be one of float16, float32, float64, int32, int64, complex64 and complex128. + index (Tensor): The 1-D Tensor containing the indices to index. The data type of ``index`` must be int32 or int64. + axis (int, optional): The dimension in which we index. Default: if None, the ``axis`` is 0. + name (str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + + Returns: + Tensor, A Tensor with same data type as ``x``. + + Examples: + .. code-block:: python + + >>> import paddle + + >>> x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0], + ... [5.0, 6.0, 7.0, 8.0], + ... [9.0, 10.0, 11.0, 12.0]]) + >>> index = paddle.to_tensor([0, 1, 1], dtype='int32') + >>> out_z1 = paddle.index_select(x=x, index=index) + >>> print(out_z1.numpy()) + [[1. 2. 3. 4.] + [5. 6. 7. 8.] + [5. 6. 7. 8.]] + >>> out_z2 = paddle.index_select(x=x, index=index, axis=1) + >>> print(out_z2.numpy()) + [[ 1. 2. 2.] + [ 5. 6. 6.] + [ 9. 10. 10.]] + """ + + if in_dynamic_or_pir_mode(): + return _C_ops.index_select(x, index, axis) + else: + helper = LayerHelper("index_select", **locals()) + check_variable_and_dtype( + x, + 'x', + [ + 'bool', + 'uint16', + 'float16', + 'float32', + 'float64', + 'int32', + 'int64', + 'complex64', + 'complex128', + ], + 'paddle.tensor.search.index_select', + ) + check_variable_and_dtype( + index, + 'index', + ['int32', 'int64'], + 'paddle.tensor.search.index_select', + ) + + out = helper.create_variable_for_type_inference(x.dtype) + + helper.append_op( + type='index_select', + inputs={'X': x, 'Index': index}, + outputs={'Out': out}, + attrs={'dim': axis}, + ) + return out + + @overload def nonzero(x: Tensor, as_tuple: Literal[False] = ...) -> Tensor: ... diff --git a/test/legacy_test/test_index_select_op.py b/test/legacy_test/test_index_select_op.py index b5be024abeda88..76efcc52245c4e 100644 --- a/test/legacy_test/test_index_select_op.py +++ b/test/legacy_test/test_index_select_op.py @@ -315,99 +315,5 @@ def test_dygraph_api(self): np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) -def get_places(): - places = [] - if base.is_compiled_with_cuda(): - places.append(paddle.CUDAPlace(0)) - places.append(paddle.CPUPlace()) - return places - - -class TestIndexSelectAPI_Compatibility(unittest.TestCase): - def setUp(self): - np.random.seed(123) - self.places = get_places() - self.shape = [10, 20] - self.index_shape = [5] - self.axis = 1 - self.dtype = 'float32' - self.init_data() - - def init_data(self): - self.np_input = np.random.rand(*self.shape).astype(self.dtype) - self.np_index = np.random.randint( - 0, self.shape[self.axis], self.index_shape - ).astype('int64') - - def test_dygraph_Compatibility(self): - paddle.disable_static() - x = paddle.to_tensor(self.np_input) - index = paddle.to_tensor(self.np_index) - paddle_dygraph_out = [] - # Position args (args) - out1 = paddle.index_select(x, index, self.axis) - paddle_dygraph_out.append(out1) - # Key words args (kwargs) for paddle - out2 = paddle.index_select(x=x, index=index, axis=self.axis) - paddle_dygraph_out.append(out2) - # Key words args for torch - out3 = paddle.index_select(input=x, index=index, dim=self.axis) - paddle_dygraph_out.append(out3) - # Combined args and kwargs - out4 = paddle.index_select(x, index, dim=self.axis) - paddle_dygraph_out.append(out4) - # Tensor method args - out5 = x.index_select(index, self.axis) - paddle_dygraph_out.append(out5) - # Tensor method kwargs - out6 = x.index_select(index=index, dim=self.axis) - paddle_dygraph_out.append(out6) - # Test out - ref_out_shape = list(self.np_input.shape) - ref_out_shape[self.axis] = len(self.np_index) - out7 = paddle.empty(ref_out_shape, dtype=x.dtype) - paddle.index_select(input=x, index=index, dim=self.axis, out=out7) - paddle_dygraph_out.append(out7) - # Numpy reference out - ref_out = np.take(self.np_input, self.np_index, axis=self.axis) - # Check - for out in paddle_dygraph_out: - np.testing.assert_allclose(ref_out, out.numpy(), rtol=1e-05) - paddle.enable_static() - - def test_static_Compatibility(self): - paddle.enable_static() - main = paddle.static.Program() - startup = paddle.static.Program() - with base.program_guard(main, startup): - x = paddle.static.data(name="x", shape=self.shape, dtype=self.dtype) - index = paddle.static.data( - name="index", shape=self.index_shape, dtype='int64' - ) - # Position args (args) - out1 = paddle.index_select(x, index, self.axis) - # Key words args (kwargs) for paddle - out2 = paddle.index_select(x=x, index=index, axis=self.axis) - # Key words args for torch - out3 = paddle.index_select(input=x, index=index, dim=self.axis) - # Combined args and kwargs - out4 = paddle.index_select(x, index, dim=self.axis) - # Tensor method args - out5 = x.index_select(index, self.axis) - # Tensor method kwargs - out6 = x.index_select(index=index, dim=self.axis) - # Do not support out in static - ref_out = np.take(self.np_input, self.np_index, axis=self.axis) - for place in self.places: - exe = base.Executor(place) - fetches = exe.run( - main, - feed={"x": self.np_input, "index": self.np_index}, - fetch_list=[out1, out2, out3, out4, out5, out6], - ) - for out in fetches: - np.testing.assert_allclose(out, ref_out, rtol=1e-05) - - if __name__ == '__main__': unittest.main() From 8c73a6ea04367f695049558a35ab186187d7f5e9 Mon Sep 17 00:00:00 2001 From: zhengshengning Date: Mon, 25 Aug 2025 16:50:41 +0800 Subject: [PATCH 17/27] fix pad [int value int] --- paddle/fluid/pybind/eager_utils.cc | 15 +++++++++------ test/sot/test_sot_dynamic_shape.py | 7 ------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index 83763852d9ee68..272d9b37147521 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -31,6 +31,7 @@ limitations under the License. */ #include "paddle/fluid/pir/dialect/operator/ir/op_type.h" #include "paddle/fluid/pir/dialect/operator/ir/pd_api.h" #include "paddle/fluid/pir/dialect/operator/utils/utils.h" +#include "paddle/fluid/pir/utils/general_functions.h" #include "paddle/fluid/pir/utils/name_analysis.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/pybind/eager.h" @@ -2336,6 +2337,7 @@ std::vector CastPyArg2VectorOfValueOrLong( } phi::DataType dtype = phi::DataType::INT64; + std::vector shape; for (Py_ssize_t i = 0; i < len; ++i) { PyObject* item = PySequence_GetItem(obj, i); if (!item) { @@ -2347,8 +2349,9 @@ std::vector CastPyArg2VectorOfValueOrLong( if (PyObject_TypeCheck(item, g_ir_value_pytype)) { pir::Value val = ::pybind11::handle(item).cast(); dtype = paddle::dialect::GetValueDataType(val); + shape = pir::GetShapeFromValue(val); Py_DECREF(item); - break; // 找到第一个 pir::Value 后退出 + break; } Py_DECREF(item); @@ -2365,14 +2368,14 @@ std::vector CastPyArg2VectorOfValueOrLong( item = CastPyArg2ValuePreHook(item); - if (PyObject_TypeCheck(item, g_ir_value_pytype)) { + if (PyObject_CheckIRValue(item)) { value_list.emplace_back(::pybind11::handle(item).cast()); - } else if (PyLong_Check(item)) { + } else if (PyObject_CheckLong(item)) { int64_t k_tmp = CastPyArg2Long(item, op_type, arg_pos); - value_list.emplace_back(paddle::dialect::full( - std::vector{1}, k_tmp, dtype, phi::CPUPlace())); + value_list.emplace_back( + paddle::dialect::full(shape, k_tmp, dtype, phi::CPUPlace())); } else if (item == Py_None) { - // skip + continue; // skip } else { PADDLE_THROW(common::errors::InvalidType( "%s(): argument (position %d) must be vector, " diff --git a/test/sot/test_sot_dynamic_shape.py b/test/sot/test_sot_dynamic_shape.py index 562e154d524be1..0f5d80523f8e90 100644 --- a/test/sot/test_sot_dynamic_shape.py +++ b/test/sot/test_sot_dynamic_shape.py @@ -25,7 +25,6 @@ import paddle from paddle.jit.sot.psdb import check_no_breakgraph from paddle.jit.sot.utils import ( - ConditionalFallbackError, allow_dynamic_shape_guard, enable_0_size_fallback_guard, specialized_dim_numbers_guard, @@ -249,7 +248,6 @@ def test_pad_dynamic_shape_fallback(self): ) for i in range(1, 5): self.assert_results(pad_func, paddle.randn([1, 3, 224, 224]), i) - self.assertEqual(ctx.translate_count, i) def test_dynamic_shape_int_mul_float(self): with ( @@ -344,11 +342,6 @@ def test_dynamic_shape_constraint(self): 5, # hit 2 * (s0 + s1 - 2) <= 30 ) - with self.assertRaises(ConditionalFallbackError): - self.assert_results( - dynamic_shape_constraint, paddle.randn([0, 1, const_dim]) - ) - def test_mixed_dynamic_and_static(self): with ( allow_dynamic_shape_guard(True), From f5be1f954604df00413eb7e35b151d64428ab99c Mon Sep 17 00:00:00 2001 From: cangtianhuang Date: Mon, 25 Aug 2025 17:02:34 +0800 Subject: [PATCH 18/27] move ops.yaml --- paddle/phi/ops/yaml/ops.yaml | 16 ---------------- paddle/phi/ops/yaml/python_api_info.yaml | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/paddle/phi/ops/yaml/ops.yaml b/paddle/phi/ops/yaml/ops.yaml index ee5144e17f415f..3812ea7cd245af 100644 --- a/paddle/phi/ops/yaml/ops.yaml +++ b/paddle/phi/ops/yaml/ops.yaml @@ -3265,10 +3265,6 @@ - op : logical_and args : (Tensor x, Tensor y) - python_api: - name : [paddle.logical_and, paddle.Tensor.logical_and] - args_alias: - use_default_mapping : True output : Tensor(out) infer_meta : func : LogicalBinaryInferMeta @@ -3283,10 +3279,6 @@ - op : logical_not args : (Tensor x) - python_api: - name : [paddle.logical_not, paddle.Tensor.logical_not] - args_alias: - use_default_mapping : True output : Tensor(out) infer_meta : func : LogicalNotInferMeta @@ -3301,10 +3293,6 @@ - op : logical_or args : (Tensor x, Tensor y) - python_api: - name : [paddle.logical_or, paddle.Tensor.logical_or] - args_alias: - use_default_mapping : True output : Tensor(out) infer_meta : func : LogicalBinaryInferMeta @@ -3319,10 +3307,6 @@ - op : logical_xor args : (Tensor x, Tensor y) - python_api: - name : [paddle.logical_xor, paddle.Tensor.logical_xor] - args_alias: - use_default_mapping : True output : Tensor(out) infer_meta : func : LogicalBinaryInferMeta diff --git a/paddle/phi/ops/yaml/python_api_info.yaml b/paddle/phi/ops/yaml/python_api_info.yaml index 740afa9ee689d0..5eb96bc4df20db 100644 --- a/paddle/phi/ops/yaml/python_api_info.yaml +++ b/paddle/phi/ops/yaml/python_api_info.yaml @@ -7,3 +7,23 @@ name : [paddle.amax,paddle.Tensor.amax] args_alias : use_default_mapping : True + +- op : logical_and + name : [paddle.logical_and, paddle.Tensor.logical_and] + args_alias: + use_default_mapping : True + +- op : logical_or + name : [paddle.logical_or, paddle.Tensor.logical_or] + args_alias: + use_default_mapping : True + +- op : logical_xor + name : [paddle.logical_xor, paddle.Tensor.logical_xor] + args_alias: + use_default_mapping : True + +- op : logical_not + name : [paddle.logical_not, paddle.Tensor.logical_not] + args_alias: + use_default_mapping : True From c0525357ca29a95d59be16cea8aadb979bdba321 Mon Sep 17 00:00:00 2001 From: cangtianhuang Date: Mon, 25 Aug 2025 17:06:57 +0800 Subject: [PATCH 19/27] revert docs --- python/paddle/_paddle_docs.py | 54 ++--------------------------------- 1 file changed, 2 insertions(+), 52 deletions(-) diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index 3e30a7eccf4cac..6fe55736744443 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -784,58 +784,6 @@ def triu( """, ) # lihaoyang -add_doc_and_signature( - "index_select", - """ - Returns a new tensor which indexes the ``input`` tensor along dimension ``axis`` using - the entries in ``index`` which is a Tensor. The returned tensor has the same number - of dimensions as the original ``x`` tensor. The dim-th dimension has the same - size as the length of ``index``; other dimensions have the same size as in the ``x`` tensor. - - Args: - x (Tensor): The input Tensor to be operated. The data of ``x`` can be one of float16, float32, float64, int32, int64, complex64 and complex128. - index (Tensor): The 1-D Tensor containing the indices to index. The data type of ``index`` must be int32 or int64. - axis (int, optional): The dimension in which we index. Default: if None, the ``axis`` is 0. - name (str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. - - Keyword Args: - out (Tensor|optional): The output tensor. - - Returns: - Tensor, A Tensor with same data type as ``x``. - - Examples: - .. code-block:: python - - >>> import paddle - - >>> x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0], - ... [5.0, 6.0, 7.0, 8.0], - ... [9.0, 10.0, 11.0, 12.0]]) - >>> index = paddle.to_tensor([0, 1, 1], dtype='int32') - >>> out_z1 = paddle.index_select(x=x, index=index) - >>> print(out_z1.numpy()) - [[1. 2. 3. 4.] - [5. 6. 7. 8.] - [5. 6. 7. 8.]] - >>> out_z2 = paddle.index_select(x=x, index=index, axis=1) - >>> print(out_z2.numpy()) - [[ 1. 2. 2.] - [ 5. 6. 6.] - [ 9. 10. 10.]] -""", - """ -def index_select( - x: Tensor, - index: Tensor, - axis: int = 0, - name: str | None = None, - *, - out: Tensor | None = None, -) -> Tensor -""", -) - add_doc_and_signature( "logical_and", r""" @@ -1008,6 +956,8 @@ def logical_xor( """, ) +# lihaoyang08 + # lubingxin # chenhuangrun From f0008067fdd8de71f2c8b1e43811eda9377e7468 Mon Sep 17 00:00:00 2001 From: zhengshengning Date: Mon, 25 Aug 2025 17:07:32 +0800 Subject: [PATCH 20/27] fix --- .../test_functional_conv2d_deprecated.py | 393 ----- .../test_functional_conv3d_deprecated.py | 387 ----- ..._functional_conv3d_transpose_deprecated.py | 416 ----- .../legacy_test/test_layers_deprecated.py | 1466 ----------------- .../legacy_test/test_save_load_deprecated.py | 1246 -------------- .../test_comp_sigmoid_grad_deprecated.py | 113 -- .../rnn/test_rnn_nets_static_deprecated.py | 386 ----- test/legacy_test/test_activation_op.py | 2 +- .../quantization/test_imperative_out_scale.py | 10 - test/quantization/test_imperative_qat.py | 42 - test/quantization/test_imperative_skip_op.py | 60 - 11 files changed, 1 insertion(+), 4520 deletions(-) delete mode 100644 test/deprecated/legacy_test/test_functional_conv2d_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_functional_conv3d_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_functional_conv3d_transpose_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_layers_deprecated.py delete mode 100644 test/deprecated/legacy_test/test_save_load_deprecated.py delete mode 100644 test/deprecated/prim/prim/vjp/static/test_comp_sigmoid_grad_deprecated.py delete mode 100644 test/deprecated/rnn/test_rnn_nets_static_deprecated.py diff --git a/test/deprecated/legacy_test/test_functional_conv2d_deprecated.py b/test/deprecated/legacy_test/test_functional_conv2d_deprecated.py deleted file mode 100644 index 6271b7fe5fc2e3..00000000000000 --- a/test/deprecated/legacy_test/test_functional_conv2d_deprecated.py +++ /dev/null @@ -1,393 +0,0 @@ -# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest -from unittest import TestCase - -import numpy as np - -import paddle -import paddle.base.dygraph as dg -import paddle.nn.functional as F -from paddle import base - -paddle.enable_static() - - -class TestFunctionalConv2D(TestCase): - batch_size = 4 - spatial_shape = (16, 16) - dtype = "float32" - - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NHWC" - - def prepare(self): - if isinstance(self.filter_shape, int): - filter_shape = (self.filter_shape,) * 2 - else: - filter_shape = tuple(self.filter_shape) - - self.weight = np.random.uniform( - -1, - 1, - ( - self.out_channels, - self.in_channels // self.groups, - *filter_shape, - ), - ).astype(self.dtype) - if not self.no_bias: - self.bias = np.random.uniform(-1, 1, (self.out_channels,)).astype( - self.dtype - ) - - self.channel_last = self.data_format == "NHWC" - if self.channel_last: - self.input_shape = ( - self.batch_size, - *self.spatial_shape, - self.in_channels, - ) - else: - self.input_shape = ( - self.batch_size, - self.in_channels, - *self.spatial_shape, - ) - - self.input = np.random.uniform(-1, 1, self.input_shape).astype( - self.dtype - ) - - def static_graph_case_1(self): - main = base.Program() - start = base.Program() - with ( - base.unique_name.guard(), - base.program_guard(main, start), - ): - if self.channel_last: - x = paddle.static.data( - "input", - (-1, -1, -1, self.in_channels), - dtype=self.dtype, - ) - else: - x = paddle.static.data( - "input", - (-1, self.in_channels, -1, -1), - dtype=self.dtype, - ) - y = paddle.static.nn.conv2d( - x, - self.out_channels, - self.filter_shape, - stride=self.stride, - padding=self.padding, - dilation=self.dilation, - groups=self.groups, - param_attr=paddle.nn.initializer.Assign(self.weight), - bias_attr=( - False - if self.no_bias - else paddle.nn.initializer.Assign(self.bias) - ), - act=self.act, - data_format=self.data_format, - ) - exe = base.Executor(self.place) - exe.run(start) - (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) - return out - - def static_graph_case_2(self): - main = base.Program() - start = base.Program() - with base.unique_name.guard(), base.program_guard(main, start): - if self.channel_last: - x = x = paddle.static.data( - "input", - (-1, -1, -1, self.in_channels), - dtype=self.dtype, - ) - else: - x = paddle.static.data( - "input", - (-1, self.in_channels, -1, -1), - dtype=self.dtype, - ) - weight = paddle.static.data( - "weight", self.weight.shape, dtype=self.dtype - ) - if not self.no_bias: - bias = paddle.static.data( - "bias", self.bias.shape, dtype=self.dtype - ) - y = F.conv2d( - x, - weight, - None if self.no_bias else bias, - padding=self.padding, - stride=self.stride, - dilation=self.dilation, - groups=self.groups, - data_format=self.data_format, - ) - - if self.act == 'sigmoid': - y = F.sigmoid(y) - - exe = base.Executor(self.place) - exe.run(start) - feed_dict = {"input": self.input, "weight": self.weight} - if not self.no_bias: - feed_dict["bias"] = self.bias - (out,) = exe.run(main, feed=feed_dict, fetch_list=[y]) - return out - - def dygraph_case(self): - with dg.guard(self.place): - x = paddle.to_tensor(self.input) - weight = paddle.to_tensor(self.weight) - bias = None if self.no_bias else paddle.to_tensor(self.bias) - y = F.conv2d( - x, - weight, - bias, - padding=self.padding, - stride=self.stride, - dilation=self.dilation, - groups=self.groups, - data_format=self.data_format, - ) - - if self.act == 'sigmoid': - y = F.sigmoid(y) - - out = y.numpy() - return out - - def _test_identity(self): - self.prepare() - out1 = self.static_graph_case_1() - out2 = self.static_graph_case_2() - out3 = self.dygraph_case() - np.testing.assert_array_almost_equal(out1, out2) - np.testing.assert_array_almost_equal(out2, out3) - - def test_identity_cpu(self): - self.place = base.CPUPlace() - self._test_identity() - - @unittest.skipIf( - not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" - ) - def test_identity_gpu(self): - self.place = base.CUDAPlace(0) - self._test_identity() - - -class TestFunctionalConv2DCase2(TestFunctionalConv2D): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [1, 2] - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.use_cudnn = True - self.data_format = "NHWC" - - -class TestFunctionalConv2DCase3(TestFunctionalConv2D): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [1, 2, 3, 1] - self.stride = 2 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.use_cudnn = True - self.data_format = "NHWC" - - -class TestFunctionalConv2DCase4(TestFunctionalConv2D): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [1, 1, 2, 2] - self.stride = 1 - self.dilation = 2 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.use_cudnn = True - self.data_format = "NHWC" - - -class TestFunctionalConv2DCase5(TestFunctionalConv2D): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [[0, 0], [1, 1], [2, 2], [0, 0]] - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.use_cudnn = True - self.data_format = "NHWC" - - -class TestFunctionalConv2DCase6(TestFunctionalConv2D): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [[0, 0], [0, 0], [1, 1], [2, 2]] - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.use_cudnn = True - self.data_format = "NCHW" - - -class TestFunctionalConv2DCase7(TestFunctionalConv2D): - def setUp(self): - self.in_channels = 6 - self.out_channels = 8 - self.filter_shape = 3 - self.padding = "same" - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.use_cudnn = True - self.data_format = "NCHW" - - -class TestFunctionalConv2DCase8(TestFunctionalConv2D): - def setUp(self): - self.in_channels = 6 - self.out_channels = 12 - self.filter_shape = 3 - self.padding = "valid" - self.stride = 1 - self.dilation = 1 - self.groups = 6 - self.no_bias = True - self.act = None - self.use_cudnn = False - self.data_format = "NCHW" - - -class TestFunctionalConv2DErrorCase12(TestCase): - def setUp(self): - self.input = np.array([]) - self.filter = np.array([]) - self.num_filters = 0 - self.filter_size = 0 - self.bias = None - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.data_format = "NCHW" - - def static_graph_case(self): - main = base.Program() - start = base.Program() - with base.unique_name.guard(), base.program_guard(main, start): - x = paddle.static.data( - "input", self.input.shape, dtype=paddle.float32 - ) - y = paddle.static.nn.conv2d( - x, - self.num_filters, - self.filter_size, - stride=self.stride, - padding=self.padding, - dilation=self.dilation, - groups=self.groups, - param_attr=paddle.nn.initializer.Assign(self.filter), - bias_attr=( - False - if self.bias is None - else paddle.nn.initializer.Assign(self.bias) - ), - act=None, - data_format=self.data_format, - ) - exe = base.Executor() - exe.run(start) - (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) - return out - - def test_static_exception(self): - with self.assertRaises(ValueError): - self.static_graph_case() - - -class TestFunctionalConv2DErrorCase13(TestFunctionalConv2DErrorCase12): - def setUp(self): - self.input = np.random.randn(1, 3, 3, 3) - self.filter = np.random.randn(3, 3, 1, 1) - self.num_filters = 3 - self.filter_size = 1 - self.bias = None - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 0 - self.data_format = "NCHW" - - -class TestFunctionalConv2DErrorCase14(TestFunctionalConv2DErrorCase12): - def setUp(self): - self.input = np.random.randn(0, 0, 0, 0) - self.filter = np.random.randn(1, 0, 0, 0) - self.num_filters = 0 - self.filter_size = 0 - self.bias = None - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.data_format = "NCHW" - - -if __name__ == "__main__": - paddle.enable_static() - unittest.main() diff --git a/test/deprecated/legacy_test/test_functional_conv3d_deprecated.py b/test/deprecated/legacy_test/test_functional_conv3d_deprecated.py deleted file mode 100644 index 38eb8ec50a17df..00000000000000 --- a/test/deprecated/legacy_test/test_functional_conv3d_deprecated.py +++ /dev/null @@ -1,387 +0,0 @@ -# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest -from unittest import TestCase - -import numpy as np - -import paddle -import paddle.base.dygraph as dg -import paddle.nn.functional as F -from paddle import base - -paddle.enable_static() - - -class TestFunctionalConv3D(TestCase): - batch_size = 4 - spatial_shape = (8, 8, 8) - dtype = "float32" - - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - def prepare(self): - if isinstance(self.filter_shape, int): - filter_shape = (self.filter_shape,) * 3 - else: - filter_shape = tuple(self.filter_shape) - - self.weight = np.random.uniform( - -1, - 1, - ( - self.out_channels, - self.in_channels // self.groups, - *filter_shape, - ), - ).astype(self.dtype) - if not self.no_bias: - self.bias = np.random.uniform(-1, 1, (self.out_channels,)).astype( - self.dtype - ) - - self.channel_last = self.data_format == "NDHWC" - if self.channel_last: - self.input_shape = ( - self.batch_size, - *self.spatial_shape, - self.in_channels, - ) - else: - self.input_shape = ( - self.batch_size, - self.in_channels, - *self.spatial_shape, - ) - - self.input = np.random.uniform(-1, 1, self.input_shape).astype( - self.dtype - ) - - def static_graph_case_1(self): - main = base.Program() - start = base.Program() - with ( - base.unique_name.guard(), - base.program_guard(main, start), - ): - if self.channel_last: - x = paddle.static.data( - "input", - (-1, -1, -1, -1, self.in_channels), - dtype=self.dtype, - ) - else: - x = paddle.static.data( - "input", - (-1, self.in_channels, -1, -1, -1), - dtype=self.dtype, - ) - y = paddle.static.nn.conv3d( - x, - self.out_channels, - self.filter_shape, - stride=self.stride, - padding=self.padding, - dilation=self.dilation, - groups=self.groups, - param_attr=paddle.nn.initializer.Assign(self.weight), - bias_attr=( - False - if self.no_bias - else paddle.nn.initializer.Assign(self.bias) - ), - act=self.act, - data_format=self.data_format, - ) - exe = base.Executor(self.place) - exe.run(start) - (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) - return out - - def static_graph_case_2(self): - main = base.Program() - start = base.Program() - with base.unique_name.guard(), base.program_guard(main, start): - if self.channel_last: - x = x = paddle.static.data( - "input", - (-1, -1, -1, -1, self.in_channels), - dtype=self.dtype, - ) - else: - x = paddle.static.data( - "input", - (-1, self.in_channels, -1, -1, -1), - dtype=self.dtype, - ) - weight = paddle.static.data( - "weight", self.weight.shape, dtype=self.dtype - ) - if not self.no_bias: - bias = paddle.static.data( - "bias", self.bias.shape, dtype=self.dtype - ) - y = F.conv3d( - x, - weight, - None if self.no_bias else bias, - padding=self.padding, - stride=self.stride, - dilation=self.dilation, - groups=self.groups, - data_format=self.data_format, - ) - - if self.act == 'sigmoid': - y = F.sigmoid(y) - - exe = base.Executor(self.place) - exe.run(start) - feed_dict = {"input": self.input, "weight": self.weight} - if not self.no_bias: - feed_dict["bias"] = self.bias - (out,) = exe.run(main, feed=feed_dict, fetch_list=[y]) - return out - - def dygraph_case(self): - with dg.guard(self.place): - x = paddle.to_tensor(self.input) - weight = paddle.to_tensor(self.weight) - bias = None if self.no_bias else paddle.to_tensor(self.bias) - y = F.conv3d( - x, - weight, - bias, - padding=self.padding, - stride=self.stride, - dilation=self.dilation, - groups=self.groups, - data_format=self.data_format, - ) - - if self.act == 'sigmoid': - y = F.sigmoid(y) - - out = y.numpy() - return out - - def _test_identity(self): - self.prepare() - out1 = self.static_graph_case_1() - out2 = self.static_graph_case_2() - out3 = self.dygraph_case() - np.testing.assert_array_almost_equal(out1, out2) - np.testing.assert_array_almost_equal(out2, out3) - - def test_identity_cpu(self): - self.place = base.CPUPlace() - self._test_identity() - - @unittest.skipIf( - not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" - ) - def test_identity_gpu(self): - self.place = base.CUDAPlace(0) - self._test_identity() - - -class TestFunctionalConv3DCase2(TestFunctionalConv3D): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [1, 2, 1] - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - -class TestFunctionalConv3DCase3(TestFunctionalConv3D): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [1, 2, 3, 1, 2, 3] - self.stride = 2 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - -class TestFunctionalConv3DCase4(TestFunctionalConv3D): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [1, 1, 2, 2, 3, 3] - self.stride = 1 - self.dilation = 2 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - -class TestFunctionalConv3DCase5(TestFunctionalConv3D): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [[0, 0], [1, 1], [2, 2], [1, 1], [0, 0]] - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - -class TestFunctionalConv3DCase6(TestFunctionalConv3D): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [[0, 0], [0, 0], [1, 1], [2, 2], [2, 2]] - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - -class TestFunctionalConv3DCase7(TestFunctionalConv3D): - def setUp(self): - self.in_channels = 6 - self.out_channels = 8 - self.filter_shape = 3 - self.padding = "same" - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - -class TestFunctionalConv3DCase8(TestFunctionalConv3D): - def setUp(self): - self.in_channels = 6 - self.out_channels = 12 - self.filter_shape = 3 - self.padding = "valid" - self.stride = 1 - self.dilation = 1 - self.groups = 6 - self.no_bias = True - self.act = None - self.use_cudnn = False - self.data_format = "NCDHW" - - -class TestFunctionalConv3DErrorCase11(TestCase): - def setUp(self): - self.input = np.array([]) - self.filter = np.array([]) - self.num_filters = 0 - self.filter_size = 0 - self.bias = None - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.data_format = "NCDHW" - - def static_graph_case(self): - main = base.Program() - start = base.Program() - with base.unique_name.guard(), base.program_guard(main, start): - x = paddle.static.data( - "input", self.input.shape, dtype=paddle.float32 - ) - y = paddle.static.nn.conv3d( - x, - self.num_filters, - self.filter_size, - stride=self.stride, - padding=self.padding, - dilation=self.dilation, - groups=self.groups, - param_attr=paddle.nn.initializer.Assign(self.filter), - bias_attr=( - False - if self.bias is None - else paddle.nn.initializer.Assign(self.bias) - ), - act=None, - data_format=self.data_format, - ) - exe = base.Executor() - exe.run(start) - (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) - return out - - def test_static_exception(self): - with self.assertRaises(ValueError): - self.static_graph_case() - - -class TestFunctionalConv3DErrorCase12(TestFunctionalConv3DErrorCase11): - def setUp(self): - self.input = np.random.randn(1, 3, 3, 3, 3) - self.filter = np.random.randn(3, 3, 1, 1, 1) - self.num_filters = 3 - self.filter_size = 1 - self.bias = None - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 0 - self.data_format = "NCDHW" - - -class TestFunctionalConv3DErrorCase13(TestFunctionalConv3DErrorCase11): - def setUp(self): - self.input = np.random.randn(0, 0, 0, 0, 0) - self.filter = np.random.randn(1, 0, 0, 0, 0) - self.num_filters = 1 - self.filter_size = 1 - self.bias = None - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.data_format = "NCDHW" - - -if __name__ == "__main__": - paddle.enable_static() - unittest.main() diff --git a/test/deprecated/legacy_test/test_functional_conv3d_transpose_deprecated.py b/test/deprecated/legacy_test/test_functional_conv3d_transpose_deprecated.py deleted file mode 100644 index 7b72f84fd0b4e6..00000000000000 --- a/test/deprecated/legacy_test/test_functional_conv3d_transpose_deprecated.py +++ /dev/null @@ -1,416 +0,0 @@ -# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest -from unittest import TestCase - -import numpy as np - -import paddle -import paddle.base.dygraph as dg -import paddle.nn.functional as F -from paddle import base - -paddle.enable_static() - - -class TestFunctionalConv3DTranspose(TestCase): - batch_size = 4 - spatial_shape = (8, 8, 8) - dtype = "float32" - output_size = None - - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - def prepare(self): - if isinstance(self.filter_shape, int): - filter_shape = (self.filter_shape,) * 3 - else: - filter_shape = tuple(self.filter_shape) - - self.weight = np.random.uniform( - -1, - 1, - ( - self.in_channels, - self.out_channels // self.groups, - *filter_shape, - ), - ).astype(self.dtype) - if not self.no_bias: - self.bias = np.random.uniform(-1, 1, (self.out_channels,)).astype( - self.dtype - ) - - self.channel_last = self.data_format == "NDHWC" - if self.channel_last: - self.input_shape = ( - self.batch_size, - *self.spatial_shape, - self.in_channels, - ) - else: - self.input_shape = ( - self.batch_size, - self.in_channels, - *self.spatial_shape, - ) - - self.input = np.random.uniform(-1, 1, self.input_shape).astype( - self.dtype - ) - - def static_graph_case_1(self): - main = base.Program() - start = base.Program() - with ( - base.unique_name.guard(), - base.program_guard(main, start), - ): - if self.channel_last: - x = paddle.static.data( - "input", - (-1, -1, -1, -1, self.in_channels), - dtype=self.dtype, - ) - else: - x = paddle.static.data( - "input", - (-1, self.in_channels, -1, -1, -1), - dtype=self.dtype, - ) - y = paddle.static.nn.conv3d_transpose( - x, - self.out_channels, - output_size=self.output_size, - filter_size=self.filter_shape, - stride=self.stride, - padding=self.padding, - dilation=self.dilation, - groups=self.groups, - param_attr=paddle.nn.initializer.Assign(self.weight), - bias_attr=( - False - if self.no_bias - else paddle.nn.initializer.Assign(self.bias) - ), - act=self.act, - data_format=self.data_format, - ) - exe = base.Executor(self.place) - exe.run(start) - (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) - return out - - def static_graph_case_2(self): - main = base.Program() - start = base.Program() - with base.unique_name.guard(), base.program_guard(main, start): - if self.channel_last: - x = x = paddle.static.data( - "input", - (-1, -1, -1, -1, self.in_channels), - dtype=self.dtype, - ) - else: - x = paddle.static.data( - "input", - (-1, self.in_channels, -1, -1, -1), - dtype=self.dtype, - ) - weight = paddle.static.data( - "weight", self.weight.shape, dtype=self.dtype - ) - if not self.no_bias: - bias = paddle.static.data( - "bias", self.bias.shape, dtype=self.dtype - ) - y = F.conv3d_transpose( - x, - weight, - None if self.no_bias else bias, - output_size=self.output_size, - padding=self.padding, - stride=self.stride, - dilation=self.dilation, - groups=self.groups, - data_format=self.data_format, - ) - if self.act == 'sigmoid': - y = F.sigmoid(y) - exe = base.Executor(self.place) - exe.run(start) - feed_dict = {"input": self.input, "weight": self.weight} - if not self.no_bias: - feed_dict["bias"] = self.bias - (out,) = exe.run(main, feed=feed_dict, fetch_list=[y]) - return out - - def dygraph_case(self): - with dg.guard(self.place): - x = paddle.to_tensor(self.input) - weight = paddle.to_tensor(self.weight) - bias = None if self.no_bias else paddle.to_tensor(self.bias) - y = F.conv3d_transpose( - x, - weight, - bias, - output_size=self.output_size, - padding=self.padding, - stride=self.stride, - dilation=self.dilation, - groups=self.groups, - data_format=self.data_format, - ) - if self.act == 'sigmoid': - y = F.sigmoid(y) - out = y.numpy() - return out - - def _test_identity(self): - self.prepare() - out1 = self.static_graph_case_1() - out2 = self.static_graph_case_2() - out3 = self.dygraph_case() - np.testing.assert_array_almost_equal(out1, out2) - np.testing.assert_array_almost_equal(out2, out3) - - def test_identity_cpu(self): - self.place = base.CPUPlace() - self._test_identity() - - @unittest.skipIf( - not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" - ) - def test_identity_gpu(self): - self.place = base.CUDAPlace(0) - self._test_identity() - - -class TestFunctionalConv3DTransposeCase2(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - -class TestFunctionalConv3DTransposeCase3(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 6 - self.filter_shape = 3 - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - -class TestFunctionalConv3DTransposeCase4(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 6 - self.filter_shape = 3 - self.padding = "same" - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = True - self.act = "sigmoid" - self.data_format = "NDHWC" - - -class TestFunctionalConv3DTransposeCase5(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 6 - self.filter_shape = 3 - self.padding = "valid" - self.stride = (1, 2, 1) - self.dilation = (2, 1, 1) - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - -class TestFunctionalConv3DTransposeCase6(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 4 - self.filter_shape = 3 - self.padding = "valid" - self.stride = (1, 2, 1) - self.dilation = 1 - self.groups = 4 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - -class TestFunctionalConv3DTransposeCase7(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 4 - self.filter_shape = 3 - self.padding = "valid" - self.output_size = (10, 17, 10) - self.stride = (1, 2, 1) - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - -class TestFunctionalConv3DTransposeCase8(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 6 - self.filter_shape = 3 - self.padding = [[0, 0], [1, 2], [1, 2], [2, 1], [0, 0]] - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - -class TestFunctionalConv3DTransposeCase9(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 6 - self.filter_shape = 3 - self.padding = [[0, 0], [0, 0], [1, 1], [1, 1], [2, 2]] - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - -class TestFunctionalConv3DTransposeCase10(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 6 - self.filter_shape = 3 - self.padding = [1, 1, 2, 2, 1, 1] - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - -class TestFunctionalConv3DTransposeCase11(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 6 - self.filter_shape = 3 - self.padding = [1, 2, 1] - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - -class TestFunctionalConv3DTransposeErrorCase10(TestCase): - def setUp(self): - self.input = np.array([]) - self.filter = np.array([]) - self.num_filters = 0 - self.filter_size = 0 - self.bias = None - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.data_format = "NCDHW" - - def static_graph_case(self): - main = base.Program() - start = base.Program() - with base.unique_name.guard(), base.program_guard(main, start): - x = paddle.static.data( - "input", self.input.shape, dtype=paddle.float32 - ) - y = paddle.static.nn.conv3d_transpose( - x, - self.num_filters, - self.filter_size, - stride=self.stride, - padding=self.padding, - dilation=self.dilation, - groups=self.groups, - param_attr=paddle.nn.initializer.Assign(self.filter), - bias_attr=( - False - if self.bias is None - else paddle.nn.initializer.Assign(self.bias) - ), - act=None, - data_format=self.data_format, - ) - exe = base.Executor() - exe.run(start) - (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) - return out - - def test_static_exception(self): - with self.assertRaises(ValueError): - self.static_graph_case() - - -class TestFunctionalConv3DTransposeErrorCase11( - TestFunctionalConv3DTransposeErrorCase10 -): - def setUp(self): - self.input = np.random.randn(1, 3, 3, 3, 3) - self.filter = np.random.randn(3, 3, 1, 1, 1) - self.num_filters = 3 - self.filter_size = 1 - self.bias = None - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 0 - self.data_format = "NCDHW" - - -if __name__ == "__main__": - unittest.main() diff --git a/test/deprecated/legacy_test/test_layers_deprecated.py b/test/deprecated/legacy_test/test_layers_deprecated.py deleted file mode 100644 index eff81097bb2532..00000000000000 --- a/test/deprecated/legacy_test/test_layers_deprecated.py +++ /dev/null @@ -1,1466 +0,0 @@ -# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import contextlib -import inspect -import sys -import unittest - -sys.path.append("../../legacy_test") -import nets -import numpy as np -from decorator_helper import prog_scope -from test_imperative_base import new_program_scope - -import paddle -from paddle import base -from paddle.base import core, dygraph -from paddle.base.framework import program_guard -from paddle.incubate.layers.nn import ( - batch_fc, - partial_concat, - partial_sum, - rank_attention, - shuffle_batch, -) -from paddle.tensor import random - -paddle.enable_static() - - -class LayerTest(unittest.TestCase): - @classmethod - def setUpClass(cls): - cls.seed = 111 - - @classmethod - def tearDownClass(cls): - pass - - def _get_place(self, force_to_use_cpu=False): - # this option for ops that only have cpu kernel - if force_to_use_cpu: - return core.CPUPlace() - else: - if core.is_compiled_with_cuda(): - return core.CUDAPlace(0) - return core.CPUPlace() - - @contextlib.contextmanager - def static_graph(self): - with new_program_scope(): - paddle.seed(self.seed) - paddle.framework.random._manual_program_seed(self.seed) - yield - - def get_static_graph_result( - self, feed, fetch_list, with_lod=False, force_to_use_cpu=False - ): - exe = base.Executor(self._get_place(force_to_use_cpu)) - exe.run(paddle.static.default_startup_program()) - return exe.run( - paddle.static.default_main_program(), - feed=feed, - fetch_list=fetch_list, - return_numpy=(not with_lod), - ) - - @contextlib.contextmanager - def dynamic_graph(self, force_to_use_cpu=False): - with base.dygraph.guard( - self._get_place(force_to_use_cpu=force_to_use_cpu) - ): - paddle.seed(self.seed) - paddle.framework.random._manual_program_seed(self.seed) - yield - - -class TestLayer(LayerTest): - def test_cvm(self): - inp = np.ones([10, 10], dtype='float32') - arr = [[0.6931472, -1.904654e-09, 1, 1, 1, 1, 1, 1, 1, 1]] * 10 - cvm1 = np.array(arr, dtype='float32') - cvm2 = np.ones([10, 8], dtype='float32') - show_clk = np.ones([10, 2], dtype='float32') - with self.static_graph(): - x = paddle.static.data( - name='data', - shape=[10, 10], - dtype='float32', - ) - u = paddle.static.data( - name='show_click', - shape=[10, 2], - dtype='float32', - ) - no_cvm = paddle.static.nn.continuous_value_model(x, u, True) - static_ret1 = self.get_static_graph_result( - feed={'data': inp, 'show_click': show_clk}, - fetch_list=[no_cvm], - )[0] - with self.static_graph(): - x = paddle.static.data( - name='data', - shape=[10, 10], - dtype='float32', - ) - u = paddle.static.data( - name='show_click', - shape=[10, 2], - dtype='float32', - ) - cvm = paddle.static.nn.continuous_value_model(x, u, False) - static_ret2 = self.get_static_graph_result( - feed={'data': inp, 'show_click': show_clk}, fetch_list=[cvm] - )[0] - np.testing.assert_allclose(static_ret1, cvm1, rtol=1e-5, atol=1e-06) - np.testing.assert_allclose(static_ret2, cvm2, rtol=1e-5, atol=1e-06) - - def test_conv2d_transpose(self): - inp_np = np.arange(0, 24).reshape([2, 3, 2, 2]).astype('float32') - with self.static_graph(): - img = paddle.static.data( - name='pixel', shape=[-1, 3, 2, 2], dtype='float32' - ) - out = paddle.static.nn.conv2d_transpose( - input=img, - num_filters=10, - filter_size=27, - act='sigmoid', - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - static_rlt = self.get_static_graph_result( - feed={'pixel': inp_np}, fetch_list=[out] - )[0] - with self.static_graph(): - img = paddle.static.data( - name='pixel', shape=[-1, 3, 2, 2], dtype='float32' - ) - conv2d_transpose = paddle.nn.Conv2DTranspose( - 3, - 10, - 27, - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - out = conv2d_transpose(img) - out = paddle.nn.functional.sigmoid(out) - static_rlt2 = self.get_static_graph_result( - feed={'pixel': inp_np}, fetch_list=[out] - )[0] - with self.dynamic_graph(): - conv2d_transpose = paddle.nn.Conv2DTranspose( - 3, - 10, - 27, - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - dy_rlt = conv2d_transpose(paddle.to_tensor(inp_np)) - dy_rlt = paddle.nn.functional.sigmoid(dy_rlt) - dy_rlt_value = dy_rlt.numpy() - np.testing.assert_allclose(static_rlt2, static_rlt, rtol=1e-05) - np.testing.assert_allclose(dy_rlt_value, static_rlt2, rtol=1e-05) - - with self.dynamic_graph(): - images = np.ones([2, 3, 5, 5], dtype='float32') - custom_weight = np.random.randn(3, 3, 2, 2).astype("float32") - weight_attr = base.ParamAttr( - initializer=paddle.nn.initializer.Assign(custom_weight) - ) - conv2d1 = paddle.nn.Conv2DTranspose(3, 3, [2, 2]) - conv2d2 = paddle.nn.Conv2DTranspose( - 3, - 3, - [2, 2], - weight_attr=weight_attr, - ) - dy_ret1 = conv2d1(paddle.to_tensor(images)) - dy_ret2 = conv2d2(paddle.to_tensor(images)) - self.assertFalse(np.array_equal(dy_ret1.numpy(), dy_ret2.numpy())) - - conv2d1_weight_np = conv2d1.weight.numpy() - conv2d1_bias = conv2d1.bias - self.assertFalse( - np.array_equal(conv2d1_weight_np, conv2d2.weight.numpy()) - ) - conv2d2.weight.set_value(conv2d1_weight_np) - np.testing.assert_array_equal( - conv2d1_weight_np, conv2d2.weight.numpy() - ) - conv2d2.bias.set_value(conv2d1_bias) - dy_ret1 = conv2d1(paddle.to_tensor(images)) - dy_ret2 = conv2d2(paddle.to_tensor(images)) - np.testing.assert_array_equal(dy_ret1.numpy(), dy_ret2.numpy()) - - conv2d2.weight = conv2d1.weight - conv2d2.bias = conv2d1.bias - np.testing.assert_array_equal( - conv2d1.weight.numpy(), conv2d2.weight.numpy() - ) - np.testing.assert_array_equal( - conv2d1.bias.numpy(), conv2d2.bias.numpy() - ) - - with self.static_graph(): - # the input of Conv2DTranspose must be Variable. - def test_Variable(): - images = np.ones([2, 3, 5, 5], dtype='float32') - conv2d = paddle.nn.Conv2DTranspose(3, 3, [2, 2]) - conv2d_ret1 = conv2d(images) - - self.assertRaises(TypeError, test_Variable) - - # the input dtype of Conv2DTranspose must be float16 or float32 or float64 - # float16 only can be set on GPU place - def test_type(): - images = paddle.static.data( - name='pixel', shape=[-1, 3, 5, 5], dtype='int32' - ) - conv2d = paddle.nn.Conv2DTranspose(3, 3, [2, 2]) - conv2d_ret2 = conv2d(images) - - self.assertRaises(TypeError, test_type) - - def test_bilinear_tensor_product(self): - def _test_static_specific(inp_np_x, inp_np_y): - with self.static_graph(): - data_x = paddle.static.data( - name='x', shape=[1, 3], dtype="float32" - ) - data_y = paddle.static.data( - name='y', shape=[1, 3], dtype="float32" - ) - out = paddle.static.nn.common.bilinear_tensor_product( - data_x, - data_y, - 6, - bias_attr=paddle.nn.initializer.Constant(value=1), - act='sigmoid', - ) - - static_rlt = self.get_static_graph_result( - feed={'x': inp_np_x, 'y': inp_np_y}, fetch_list=[out] - )[0] - - return static_rlt - - def _test_static(inp_np_x, inp_np_y): - with self.static_graph(): - data_x = paddle.static.data( - name='x', shape=[1, 3], dtype="float32" - ) - data_y = paddle.static.data( - name='y', shape=[1, 3], dtype="float32" - ) - btp = paddle.nn.Bilinear( - 3, - 3, - 6, - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - out = btp(data_x, data_y) - out = paddle.nn.functional.sigmoid(out) - static_rlt2 = self.get_static_graph_result( - feed={'x': inp_np_x, 'y': inp_np_y}, fetch_list=[out] - )[0] - - return static_rlt2 - - def _test_dygraph_1(inp_np_x, inp_np_y): - with self.dynamic_graph(): - btp = paddle.nn.Bilinear( - 3, - 3, - 6, - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - dy_rlt = btp( - paddle.to_tensor(inp_np_x), - paddle.to_tensor(inp_np_y), - ) - dy_rlt = paddle.nn.functional.sigmoid(dy_rlt) - dy_rlt_value = dy_rlt.numpy() - - with self.dynamic_graph(): - btp2 = paddle.nn.Bilinear(3, 3, 6) - dy_rlt2 = btp2( - paddle.to_tensor(inp_np_x), - paddle.to_tensor(inp_np_y), - ) - dy_rlt2 = paddle.nn.functional.sigmoid(dy_rlt2) - dy_rlt2_value = dy_rlt2.numpy() - - with self.static_graph(): - data_x2 = paddle.static.data( - name='x', shape=[1, 3], dtype="float32" - ) - data_y2 = paddle.static.data( - name='y', shape=[1, 3], dtype="float32" - ) - out2 = paddle.static.nn.common.bilinear_tensor_product( - data_x2, data_y2, 6, act='sigmoid' - ) - - static_rlt3 = self.get_static_graph_result( - feed={'x': inp_np_x, 'y': inp_np_y}, fetch_list=[out2] - )[0] - - return dy_rlt_value, dy_rlt2_value, static_rlt3 - - def _test_dygraph_2(inp_np_x, inp_np_y): - with self.dynamic_graph(): - custom_weight = np.random.randn(6, 3, 3).astype("float32") - weight_attr = base.ParamAttr( - initializer=paddle.nn.initializer.Assign(custom_weight) - ) - btp1 = paddle.nn.Bilinear(3, 3, 6) - btp2 = paddle.nn.Bilinear(3, 3, 6, weight_attr=weight_attr) - dy_rlt1 = btp1( - paddle.to_tensor(inp_np_x), - paddle.to_tensor(inp_np_y), - ) - dy_rlt1 = paddle.nn.functional.sigmoid(dy_rlt1) - dy_rlt2 = btp2( - paddle.to_tensor(inp_np_x), - paddle.to_tensor(inp_np_y), - ) - dy_rlt2 = paddle.nn.functional.sigmoid(dy_rlt2) - self.assertFalse( - np.array_equal(dy_rlt1.numpy(), dy_rlt2.numpy()) - ) - btp2.weight.set_value(btp1.weight.numpy()) - btp2.bias.set_value(btp1.bias) - dy_rlt1 = btp1( - paddle.to_tensor(inp_np_x), - paddle.to_tensor(inp_np_y), - ) - dy_rlt2 = btp2( - paddle.to_tensor(inp_np_x), - paddle.to_tensor(inp_np_y), - ) - np.testing.assert_array_equal(dy_rlt1.numpy(), dy_rlt2.numpy()) - - btp2.weight = btp1.weight - btp2.bias = btp1.bias - np.testing.assert_array_equal( - btp1.weight.numpy(), btp2.weight.numpy() - ) - np.testing.assert_array_equal( - btp1.bias.numpy(), btp2.bias.numpy() - ) - - inp_np_x = np.array([[1, 2, 3]]).astype('float32') - inp_np_y = np.array([[4, 5, 6]]).astype('float32') - - static_rlt = _test_static_specific(inp_np_x, inp_np_y) - static_rlt2 = _test_static(inp_np_x, inp_np_y) - dy_rlt_value, dy_rlt2_value, static_rlt3 = _test_dygraph_1( - inp_np_x, inp_np_y - ) - np.testing.assert_array_equal(dy_rlt2_value, static_rlt3) - np.testing.assert_array_equal(static_rlt2, static_rlt) - np.testing.assert_array_equal(dy_rlt_value, static_rlt) - - with paddle.pir_utils.IrGuard(): - static_pir_result = _test_static(inp_np_x, inp_np_y) - np.testing.assert_array_equal(static_pir_result, static_rlt) - - def test_embedding(self): - inp_word = np.array([[[1]]]).astype('int64') - dict_size = 20 - with self.static_graph(): - data_t = paddle.static.data( - name='word', shape=[-1, 1], dtype='int64' - ) - data_t.desc.set_need_check_feed(False) - emb = paddle.static.nn.embedding( - input=data_t.squeeze(-2), - size=[dict_size, 32], - param_attr='emb.w', - is_sparse=False, - ) - static_rlt = self.get_static_graph_result( - feed={'word': inp_word}, fetch_list=[emb] - )[0] - with self.static_graph(): - data_t = paddle.static.data( - name='word', shape=[-1, 1], dtype='int64' - ) - data_t.desc.set_need_check_feed(False) - emb2 = paddle.nn.Embedding( - dict_size, 32, weight_attr='emb.w', sparse=False - ) - emb_rlt = emb2(data_t) - static_rlt2 = self.get_static_graph_result( - feed={'word': inp_word}, fetch_list=[emb_rlt] - )[0] - with self.dynamic_graph(): - emb2 = paddle.nn.Embedding( - dict_size, 32, weight_attr='emb.w', sparse=False - ) - dy_rlt = emb2(paddle.to_tensor(inp_word)) - dy_rlt_value = dy_rlt.numpy() - - np.testing.assert_allclose(static_rlt2[0], static_rlt) - np.testing.assert_allclose(dy_rlt_value[0], static_rlt) - - with self.dynamic_graph(): - custom_weight = np.random.randn(dict_size, 32).astype("float32") - weight_attr = base.ParamAttr( - initializer=paddle.nn.initializer.Assign(custom_weight) - ) - emb1 = paddle.nn.Embedding(dict_size, 32, sparse=False) - emb2 = paddle.nn.Embedding( - dict_size, 32, weight_attr=weight_attr, sparse=False - ) - rep1 = emb1(paddle.to_tensor(inp_word)) - rep2 = emb2(paddle.to_tensor(inp_word)) - self.assertFalse(np.array_equal(emb1.weight.numpy(), custom_weight)) - np.testing.assert_array_equal(emb2.weight.numpy(), custom_weight) - self.assertFalse(np.array_equal(rep1.numpy(), rep2.numpy())) - emb2.weight.set_value(emb1.weight.numpy()) - rep2 = emb2(paddle.to_tensor(inp_word)) - np.testing.assert_array_equal(rep1.numpy(), rep2.numpy()) - - emb2.weight = emb1.weight - np.testing.assert_array_equal( - emb1.weight.numpy(), emb2.weight.numpy() - ) - - def test_conv3d(self): - with self.static_graph(): - images = paddle.static.data( - name='pixel', shape=[-1, 3, 6, 6, 6], dtype='float32' - ) - ret = paddle.static.nn.conv3d( - input=images, num_filters=3, filter_size=2 - ) - static_ret = self.get_static_graph_result( - feed={'pixel': np.ones([2, 3, 6, 6, 6], dtype='float32')}, - fetch_list=[ret], - )[0] - - with self.static_graph(): - images = paddle.static.data( - name='pixel', shape=[-1, 3, 6, 6, 6], dtype='float32' - ) - conv3d = paddle.nn.Conv3D( - in_channels=3, out_channels=3, kernel_size=2 - ) - ret = conv3d(images) - static_ret2 = self.get_static_graph_result( - feed={'pixel': np.ones([2, 3, 6, 6, 6], dtype='float32')}, - fetch_list=[ret], - )[0] - - with self.dynamic_graph(): - images = np.ones([2, 3, 6, 6, 6], dtype='float32') - conv3d = paddle.nn.Conv3D( - in_channels=3, out_channels=3, kernel_size=2 - ) - dy_ret = conv3d(paddle.to_tensor(images)) - dy_rlt_value = dy_ret.numpy() - - np.testing.assert_allclose(static_ret, dy_rlt_value, rtol=1e-05) - np.testing.assert_allclose(static_ret, static_ret2, rtol=1e-05) - - with self.dynamic_graph(): - images = np.ones([2, 3, 6, 6, 6], dtype='float32') - custom_weight = np.random.randn(3, 3, 2, 2, 2).astype("float32") - weight_attr = base.ParamAttr( - initializer=paddle.nn.initializer.Assign(custom_weight) - ) - conv3d1 = paddle.nn.Conv3D( - in_channels=3, out_channels=3, kernel_size=2 - ) - conv3d2 = paddle.nn.Conv3D( - in_channels=3, - out_channels=3, - kernel_size=2, - weight_attr=weight_attr, - ) - dy_ret1 = conv3d1(paddle.to_tensor(images)) - dy_ret2 = conv3d2(paddle.to_tensor(images)) - self.assertFalse(np.array_equal(dy_ret1.numpy(), dy_ret2.numpy())) - - conv3d1_weight_np = conv3d1.weight.numpy() - conv3d1_bias = conv3d1.bias - self.assertFalse( - np.array_equal(conv3d1_weight_np, conv3d2.weight.numpy()) - ) - conv3d2.weight.set_value(conv3d1_weight_np) - np.testing.assert_array_equal( - conv3d1_weight_np, conv3d2.weight.numpy() - ) - conv3d1.bias.set_value(conv3d1_bias) - dy_ret1 = conv3d1(paddle.to_tensor(images)) - dy_ret2 = conv3d2(paddle.to_tensor(images)) - np.testing.assert_array_equal(dy_ret1.numpy(), dy_ret2.numpy()) - - conv3d2.weight = conv3d1.weight - conv3d2.bias = conv3d1.bias - np.testing.assert_array_equal( - conv3d1.weight.numpy(), conv3d2.weight.numpy() - ) - np.testing.assert_array_equal( - conv3d1.bias.numpy(), conv3d2.bias.numpy() - ) - - def test_group_norm(self): - if core.is_compiled_with_cuda(): - place = core.CUDAPlace(0) - else: - place = core.CPUPlace() - - shape = (2, 4, 3, 3) - - def _test_static_specific(input): - with self.static_graph(): - X = paddle.static.data(name='X', shape=shape, dtype='float32') - ret = paddle.static.nn.group_norm( - input=X, - groups=2, - param_attr=paddle.nn.initializer.Uniform( - low=-0.5, high=0.5 - ), - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - static_ret = self.get_static_graph_result( - feed={ - 'X': base.create_lod_tensor( - data=input, recursive_seq_lens=[[1, 1]], place=place - ) - }, - fetch_list=[ret], - with_lod=True, - )[0] - - return static_ret - - def _test_static(input): - with self.static_graph(): - X = paddle.static.data(name='X', shape=shape, dtype='float32') - groupNorm = paddle.nn.GroupNorm( - num_channels=shape[1], - num_groups=2, - weight_attr=paddle.nn.initializer.Uniform( - low=-0.5, high=0.5 - ), - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - ret = groupNorm(X) - static_ret2 = self.get_static_graph_result( - feed={ - 'X': base.create_lod_tensor( - data=input, recursive_seq_lens=[[1, 1]], place=place - ) - }, - fetch_list=[ret, groupNorm.weight], - with_lod=True, - )[0] - - return static_ret2 - - def _test_dygraph(input): - with self.dynamic_graph(): - groupNorm = paddle.nn.GroupNorm( - num_channels=shape[1], - num_groups=2, - weight_attr=paddle.nn.initializer.Uniform( - low=-0.5, high=0.5 - ), - bias_attr=paddle.nn.initializer.Constant(value=1), - ) - dy_ret = groupNorm(paddle.to_tensor(input)) - dy_rlt_value = dy_ret.numpy() - return dy_rlt_value - - input = np.random.random(shape).astype('float32') - static_ret = _test_static_specific(input) - static_ret2 = _test_static(input) - dy_rlt_value = _test_dygraph(input) - np.testing.assert_allclose(static_ret, dy_rlt_value, rtol=1e-05) - np.testing.assert_allclose(static_ret, static_ret2, rtol=1e-05) - - with paddle.pir_utils.IrGuard(): - static_ret_pir = _test_static(input) - - np.testing.assert_allclose(static_ret2, static_ret_pir, rtol=1e-05) - - def test_instance_norm(self): - if core.is_compiled_with_cuda(): - place = core.CUDAPlace(0) - else: - place = core.CPUPlace() - - shape = (2, 4, 3, 3) - - def _test_static_specific(input): - with self.static_graph(): - X = paddle.static.data(name='X', shape=shape, dtype='float32') - ret = paddle.static.nn.instance_norm(input=X) - static_ret = self.get_static_graph_result( - feed={'X': input}, fetch_list=[ret] - )[0] - return static_ret - - def _test_static(input): - with self.static_graph(): - X = paddle.static.data(name='X', shape=shape, dtype='float32') - instanceNorm = paddle.nn.InstanceNorm2D(num_features=shape[1]) - ret = instanceNorm(X) - static_ret2 = self.get_static_graph_result( - feed={'X': input}, fetch_list=[ret] - )[0] - return static_ret2 - - def _test_dygraph_1(input): - with self.dynamic_graph(): - instanceNorm = paddle.nn.InstanceNorm2D(num_features=shape[1]) - dy_ret = instanceNorm(paddle.to_tensor(input)) - dy_rlt_value = dy_ret.numpy() - - return dy_rlt_value - - def _test_dygraph_2(input): - with self.dynamic_graph(): - instanceNorm = paddle.nn.InstanceNorm2D(num_features=shape[1]) - dy_ret = instanceNorm(paddle.to_tensor(input)) - dy_rlt_value2 = dy_ret.numpy() - return dy_rlt_value2 - - input = np.random.random(shape).astype('float32') - static_ret = _test_static_specific(input) - static_ret2 = _test_static(input) - dy_rlt_value = _test_dygraph_1(input) - dy_rlt_value2 = _test_dygraph_2(input) - - np.testing.assert_allclose(static_ret, dy_rlt_value, rtol=1e-05) - np.testing.assert_allclose(static_ret, dy_rlt_value2, rtol=1e-05) - np.testing.assert_allclose(static_ret, static_ret2, rtol=1e-05) - - with paddle.pir_utils.IrGuard(): - static_ret_pir = _test_static(input) - - np.testing.assert_allclose(static_ret2, static_ret_pir, rtol=1e-05) - - def _test_errors(): - with self.static_graph(): - # the input of InstanceNorm must be Variable. - def test_Variable(): - instanceNorm = paddle.nn.InstanceNorm2D( - num_features=shape[1] - ) - ret1 = instanceNorm(input) - - self.assertRaises(TypeError, test_Variable) - - # the input dtype of InstanceNorm must be float32 or float64 - def test_type(): - input = np.random.random(shape).astype('int32') - instanceNorm = paddle.nn.InstanceNorm2D( - num_features=shape[1] - ) - ret2 = instanceNorm(input) - - self.assertRaises(TypeError, test_type) - - _test_errors() - with paddle.pir_utils.IrGuard(): - _test_errors() - - def test_spectral_norm(self): - if core.is_compiled_with_cuda(): - place = core.CUDAPlace(0) - else: - place = core.CPUPlace() - - shape = (2, 4, 3, 3) - - input = np.random.random(shape).astype('float32') - - with self.static_graph(): - Weight = paddle.static.data( - name='Weight', shape=shape, dtype='float32' - ) - ret = paddle.static.nn.spectral_norm( - weight=Weight, dim=1, power_iters=2 - ) - static_ret = self.get_static_graph_result( - feed={ - 'Weight': base.create_lod_tensor( - data=input, recursive_seq_lens=[[1, 1]], place=place - ), - }, - fetch_list=[ret], - with_lod=True, - )[0] - - with self.static_graph(): - Weight = paddle.static.data( - name='Weight', shape=shape, dtype='float32' - ) - spectralNorm = paddle.nn.SpectralNorm(shape, dim=1, power_iters=2) - ret = spectralNorm(Weight) - static_ret2 = self.get_static_graph_result( - feed={ - 'Weight': base.create_lod_tensor( - data=input, recursive_seq_lens=[[1, 1]], place=place - ) - }, - fetch_list=[ret], - with_lod=True, - )[0] - - with self.dynamic_graph(): - spectralNorm = paddle.nn.SpectralNorm(shape, dim=1, power_iters=2) - dy_ret = spectralNorm(paddle.to_tensor(input)) - dy_rlt_value = dy_ret.numpy() - - np.testing.assert_allclose(static_ret, dy_rlt_value, rtol=1e-05) - np.testing.assert_allclose(static_ret, static_ret2, rtol=1e-05) - - def test_conv3d_transpose(self): - input_array = ( - np.arange(0, 48).reshape([2, 3, 2, 2, 2]).astype('float32') - ) - - with self.static_graph(): - img = paddle.static.data( - name='pixel', shape=[-1, 3, 2, 2, 2], dtype='float32' - ) - out = paddle.static.nn.conv3d_transpose( - input=img, num_filters=12, filter_size=12, use_cudnn=True - ) - static_rlt = self.get_static_graph_result( - feed={'pixel': input_array}, fetch_list=[out] - )[0] - with self.static_graph(): - img = paddle.static.data( - name='pixel', shape=[-1, 3, 2, 2, 2], dtype='float32' - ) - conv3d_transpose = paddle.nn.Conv3DTranspose( - in_channels=3, out_channels=12, kernel_size=12 - ) - out = conv3d_transpose(img) - static_rlt2 = self.get_static_graph_result( - feed={'pixel': input_array}, fetch_list=[out] - )[0] - with self.dynamic_graph(): - conv3d_transpose = paddle.nn.Conv3DTranspose( - in_channels=3, out_channels=12, kernel_size=12 - ) - dy_rlt = conv3d_transpose(paddle.to_tensor(input_array)) - dy_rlt_value = dy_rlt.numpy() - np.testing.assert_allclose(static_rlt2, static_rlt, rtol=1e-05) - np.testing.assert_allclose(dy_rlt_value, static_rlt, rtol=1e-05) - - with self.dynamic_graph(): - images = np.ones([2, 3, 6, 6, 6], dtype='float32') - custom_weight = np.random.randn(3, 3, 2, 2, 2).astype("float32") - weight_attr = base.ParamAttr( - initializer=paddle.nn.initializer.Assign(custom_weight) - ) - conv3d1 = paddle.nn.Conv3DTranspose( - in_channels=3, - out_channels=3, - kernel_size=2, - bias_attr='conv3d1_b', - ) - conv3d2 = paddle.nn.Conv3DTranspose( - in_channels=3, - out_channels=3, - kernel_size=2, - weight_attr=weight_attr, - bias_attr='conv3d2_b', - ) - dy_ret1 = conv3d1(paddle.to_tensor(images)) - dy_ret2 = conv3d2(paddle.to_tensor(images)) - self.assertFalse(np.array_equal(dy_ret1.numpy(), dy_ret2.numpy())) - - conv3d1_weight_np = conv3d1.weight.numpy() - conv3d1_bias = conv3d1.bias - self.assertFalse( - np.array_equal(conv3d1_weight_np, conv3d2.weight.numpy()) - ) - conv3d2.weight.set_value(conv3d1_weight_np) - np.testing.assert_array_equal( - conv3d1_weight_np, conv3d2.weight.numpy() - ) - conv3d1.bias.set_value(conv3d1_bias) - dy_ret1 = conv3d1(paddle.to_tensor(images)) - dy_ret2 = conv3d2(paddle.to_tensor(images)) - np.testing.assert_array_equal(dy_ret1.numpy(), dy_ret2.numpy()) - - conv3d2.weight = conv3d1.weight - conv3d2.bias = conv3d1.bias - np.testing.assert_array_equal( - conv3d1.weight.numpy(), conv3d2.weight.numpy() - ) - np.testing.assert_array_equal( - conv3d1.bias.numpy(), conv3d2.bias.numpy() - ) - - def test_while_loop(self): - with self.static_graph(): - i = paddle.tensor.fill_constant(shape=[1], dtype='int64', value=0) - ten = paddle.tensor.fill_constant( - shape=[1], dtype='int64', value=10 - ) - - def cond(i): - return paddle.less_than(i, ten) - - def body(i): - return i + 1 - - out = paddle.static.nn.while_loop(cond, body, [i]) - static_ret = self.get_static_graph_result(feed={}, fetch_list=out) - - with self.dynamic_graph(): - i = paddle.tensor.fill_constant(shape=[1], dtype='int64', value=0) - ten = paddle.tensor.fill_constant( - shape=[1], dtype='int64', value=10 - ) - - def cond1(i): - return paddle.less_than(i, ten) - - def body1(i): - return i + 1 - - dy_ret = paddle.static.nn.while_loop(cond1, body1, [i]) - with self.assertRaises(ValueError): - j = paddle.tensor.fill_constant( - shape=[1], dtype='int64', value=0 - ) - - def body2(i): - return i + 1, i + 2 - - paddle.static.nn.while_loop(cond1, body2, [j]) - - np.testing.assert_array_equal(static_ret[0], dy_ret[0].numpy()) - - def test_cond(self): - def less_than_branch(a, b): - return paddle.add(a, b) - - def greater_equal_branch(a, b): - return paddle.subtract(a, b) - - with self.static_graph(): - a = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.1 - ) - b = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.23 - ) - out = paddle.static.nn.cond( - a >= b, - lambda: greater_equal_branch(a, b), - lambda: less_than_branch(a, b), - ) - place = ( - base.CUDAPlace(0) - if core.is_compiled_with_cuda() - else base.CPUPlace() - ) - exe = base.Executor(place) - ret = exe.run(fetch_list=[out]) - static_res = ret[0] - - with self.dynamic_graph(): - a = paddle.to_tensor(np.array([0.1]).astype('float32')) - b = paddle.to_tensor(np.array([0.23]).astype('float32')) - out = paddle.static.nn.cond( - a < b, - lambda: less_than_branch(a, b), - lambda: greater_equal_branch(a, b), - ) - out2 = paddle.static.nn.cond( - a >= b, - lambda: greater_equal_branch(a, b), - lambda: less_than_branch(a, b), - ) - dynamic_res = out.numpy() - dynamic_res2 = out2.numpy() - np.testing.assert_array_equal(dynamic_res, dynamic_res2) - with self.assertRaises(TypeError): - paddle.static.nn.cond(a < b, 'str', 'str') - with self.assertRaises(TypeError): - paddle.static.nn.cond(a >= b, 'str', 'str') - - np.testing.assert_array_equal(static_res, dynamic_res) - - def test_case(self): - def fn_1(): - return paddle.tensor.fill_constant( - shape=[1, 2], dtype='int32', value=1 - ) - - def fn_2(): - return paddle.tensor.fill_constant( - shape=[2, 2], dtype='int32', value=2 - ) - - def fn_3(): - return paddle.tensor.fill_constant( - shape=[3, 2], dtype='int32', value=3 - ) - - with self.static_graph(): - x = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.3 - ) - y = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.1 - ) - z = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.2 - ) - - pred_1 = paddle.less_than(z, x) # true: 0.2 < 0.3 - pred_2 = paddle.less_than(x, y) # false: 0.3 < 0.1 - pred_3 = paddle.equal(x, y) # false: 0.3 == 0.1 - - out_1 = paddle.static.nn.case( - pred_fn_pairs=[(pred_1, fn_1), (pred_2, fn_2)], default=fn_3 - ) - out_2 = paddle.static.nn.case( - pred_fn_pairs=[(pred_2, fn_2), (pred_3, fn_3)] - ) - - place = ( - base.CUDAPlace(0) - if core.is_compiled_with_cuda() - else base.CPUPlace() - ) - exe = base.Executor(place) - static_res1, static_res2 = exe.run(fetch_list=[out_1, out_2]) - - with self.dynamic_graph(): - x = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.3 - ) - y = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.1 - ) - z = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=0.2 - ) - - pred_1 = paddle.less_than(z, x) # true: 0.2 < 0.3 - pred_2 = paddle.less_than(x, y) # false: 0.3 < 0.1 - pred_3 = paddle.equal(x, y) # false: 0.3 == 0.1 - - out_1 = paddle.static.nn.case( - pred_fn_pairs=[(pred_1, fn_1), (pred_2, fn_2)], default=fn_3 - ) - out_2 = paddle.static.nn.case( - pred_fn_pairs=[(pred_2, fn_2), (pred_3, fn_3)] - ) - dynamic_res1 = out_1.numpy() - dynamic_res2 = out_2.numpy() - - np.testing.assert_array_equal(static_res1, dynamic_res1) - np.testing.assert_array_equal(static_res2, dynamic_res2) - - def test_switch_case(self): - def fn_1(): - return paddle.tensor.fill_constant( - shape=[1, 2], dtype='int32', value=1 - ) - - def fn_2(): - return paddle.tensor.fill_constant( - shape=[2, 2], dtype='int32', value=2 - ) - - def fn_3(): - return paddle.tensor.fill_constant( - shape=[3, 2], dtype='int32', value=3 - ) - - with self.static_graph(): - index_1 = paddle.tensor.fill_constant( - shape=[1], dtype='int32', value=1 - ) - index_2 = paddle.tensor.fill_constant( - shape=[1], dtype='int32', value=2 - ) - - out_1 = paddle.static.nn.switch_case( - branch_index=index_1, - branch_fns={1: fn_1, 2: fn_2}, - default=fn_3, - ) - out_2 = paddle.static.nn.switch_case( - branch_index=index_2, - branch_fns=[(1, fn_1), (2, fn_2)], - default=fn_3, - ) - out_3 = paddle.static.nn.switch_case( - branch_index=index_2, - branch_fns=[(0, fn_1), (4, fn_2), (7, fn_3)], - ) - - place = ( - base.CUDAPlace(0) - if core.is_compiled_with_cuda() - else base.CPUPlace() - ) - exe = base.Executor(place) - static_res1, static_res2, static_res3 = exe.run( - fetch_list=[out_1, out_2, out_3] - ) - - with self.dynamic_graph(): - index_1 = paddle.tensor.fill_constant( - shape=[1], dtype='int32', value=1 - ) - index_2 = paddle.tensor.fill_constant( - shape=[1], dtype='int32', value=2 - ) - - out_1 = paddle.static.nn.switch_case( - branch_index=index_1, - branch_fns={1: fn_1, 2: fn_2}, - default=fn_3, - ) - out_2 = paddle.static.nn.switch_case( - branch_index=index_2, - branch_fns=[(1, fn_1), (2, fn_2)], - default=fn_3, - ) - out_3 = paddle.static.nn.switch_case( - branch_index=index_2, - branch_fns=[(0, fn_1), (4, fn_2), (7, fn_3)], - ) - - dynamic_res1 = out_1.numpy() - dynamic_res2 = out_2.numpy() - dynamic_res3 = out_3.numpy() - - np.testing.assert_array_equal(static_res1, dynamic_res1) - np.testing.assert_array_equal(static_res2, dynamic_res2) - np.testing.assert_array_equal(static_res3, dynamic_res3) - - -class TestBook(LayerTest): - def setUp(self): - self.only_static_set = set({"make_word_embedding"}) - self.not_compare_static_dygraph_set = set( - { - "make_gaussian_random", - "make_kldiv_loss", - "make_uniform_random_batch_size_like", - } - ) - self.all_close_compare = set({"make_spectral_norm"}) - - def test_all_layers(self): - attrs = (getattr(self, name) for name in dir(self)) - methods = filter(inspect.ismethod, attrs) - for method in methods: - if not method.__name__.startswith('make_'): - continue - self._low_data_bound = 0 - self._high_data_bound = 2 - self._batch_size = 2 - self._feed_dict = {} - self._force_to_use_cpu = False - with self.static_graph(): - static_var = method() - if isinstance(static_var, tuple): - static_var = static_var[0] - - if static_var is not None: - fetch_list = [static_var.name] - static_result = self.get_static_graph_result( - feed=self._feed_dict, - fetch_list=fetch_list, - force_to_use_cpu=self._force_to_use_cpu, - ) - - else: - continue - if method.__name__ in self.only_static_set: - continue - - with self.dynamic_graph(self._force_to_use_cpu): - dy_result = method() - if isinstance(dy_result, tuple): - dy_result = dy_result[0] - dy_result_value = dy_result.numpy() - - if method.__name__ in self.all_close_compare: - np.testing.assert_allclose( - static_result[0], - dy_result_value, - rtol=1e-05, - atol=0, - err_msg=f'Result of function [{method.__name__}] compare failed', - ) - continue - - if method.__name__ not in self.not_compare_static_dygraph_set: - np.testing.assert_array_equal( - static_result[0], - dy_result_value, - err_msg=f'Result of function [{method.__name__}] not equal', - ) - - def _get_np_data(self, shape, dtype, append_batch_size=True): - np.random.seed(self.seed) - if append_batch_size: - shape = [self._batch_size, *shape] - if dtype == 'float32': - return np.random.random(shape).astype(dtype) - elif dtype == 'float64': - return np.random.random(shape).astype(dtype) - elif dtype == 'int32': - return np.random.randint( - self._low_data_bound, self._high_data_bound, shape - ).astype(dtype) - elif dtype == 'int64': - return np.random.randint( - self._low_data_bound, self._high_data_bound, shape - ).astype(dtype) - - def _get_data( - self, name, shape, dtype, set_feed_dict=True, append_batch_size=True - ): - if dygraph.base.enabled(): - return paddle.to_tensor( - self._get_np_data(shape, dtype, append_batch_size), - ) - else: - if set_feed_dict: - self._feed_dict[name] = self._get_np_data( - shape, dtype, append_batch_size - ) - if append_batch_size: - shape = [-1, *shape] - data = paddle.static.data( - name=name, - shape=shape, - dtype=dtype, - ) - data.desc.set_need_check_feed(False) - return data - - def make_conv2d_transpose(self): - with program_guard( - base.default_main_program(), base.default_startup_program() - ): - img = self._get_data(name='pixel', shape=[3, 2, 2], dtype='float32') - return paddle.static.nn.conv2d_transpose( - input=img, num_filters=10, output_size=28 - ) - - def make_word_embedding(self): - with program_guard( - base.default_main_program(), base.default_startup_program() - ): - dict_size = 10000 - embed_size = 32 - first_word = self._get_data(name='firstw', shape=[1], dtype='int64') - second_word = self._get_data( - name='secondw', shape=[1], dtype='int64' - ) - third_word = self._get_data(name='thirdw', shape=[1], dtype='int64') - forth_word = self._get_data(name='forthw', shape=[1], dtype='int64') - next_word = self._get_data(name='nextw', shape=[1], dtype='int64') - - embed_first = paddle.static.nn.embedding( - input=first_word, - size=[dict_size, embed_size], - dtype='float32', - param_attr='shared_w', - ) - embed_second = paddle.static.nn.embedding( - input=second_word, - size=[dict_size, embed_size], - dtype='float32', - param_attr='shared_w', - ) - - embed_third = paddle.static.nn.embedding( - input=third_word, - size=[dict_size, embed_size], - dtype='float32', - param_attr='shared_w', - ) - embed_forth = paddle.static.nn.embedding( - input=forth_word, - size=[dict_size, embed_size], - dtype='float32', - param_attr='shared_w', - ) - - concat_embed = paddle.concat( - [embed_first, embed_second, embed_third, embed_forth], - axis=1, - ) - - hidden1 = paddle.static.nn.fc( - x=concat_embed, size=256, activation='sigmoid' - ) - predict_word = paddle.static.nn.fc( - x=hidden1, size=dict_size, activation='softmax' - ) - cost = paddle.nn.functional.cross_entropy( - input=predict_word, - label=next_word, - reduction='none', - use_softmax=False, - ) - avg_cost = paddle.mean(cost) - return avg_cost - - @prog_scope() - def make_nce(self): - window_size = 5 - words = [] - for i in range(window_size): - words.append( - self._get_data(name=f'word_{i}', shape=[1], dtype='int64') - ) - - dict_size = 10000 - label_word = int(window_size // 2) + 1 - - embs = [] - for i in range(window_size): - if i == label_word: - continue - - emb = paddle.static.nn.embedding( - input=words[i], - size=[dict_size, 32], - param_attr='emb.w', - is_sparse=True, - ) - - embs.append(emb) - - embs = paddle.concat(embs, axis=1) - loss = paddle.static.nn.nce( - input=embs, - label=words[label_word], - num_total_classes=dict_size, - param_attr='nce.w', - bias_attr='nce.b', - ) - avg_loss = paddle.mean(loss) - return avg_loss - - def make_bilinear_tensor_product_layer(self): - with program_guard( - base.default_main_program(), base.default_startup_program() - ): - data = self._get_data(name='data', shape=[4], dtype="float32") - - theta = self._get_data(name="theta", shape=[5], dtype="float32") - out = paddle.static.nn.common.bilinear_tensor_product( - data, theta, 6 - ) - return out - - def make_batch_norm(self): - with program_guard( - base.default_main_program(), base.default_startup_program() - ): - data = self._get_data( - name='data', shape=[32, 128, 128], dtype="float32" - ) - out = paddle.static.nn.batch_norm(data) - return out - - def make_batch_norm_momentum_variable(self): - with program_guard( - base.default_main_program(), base.default_startup_program() - ): - data = self._get_data( - name='data', shape=[32, 128, 128], dtype="float32" - ) - momentum = self._get_data( - name='momentum', - shape=[1], - dtype='float32', - append_batch_size=False, - ) - out = paddle.static.nn.batch_norm(data, momentum=momentum) - return out - - def make_spectral_norm(self): - with program_guard( - base.default_main_program(), base.default_startup_program() - ): - weight = self._get_data( - name='weight', - shape=[2, 3, 32, 32], - dtype="float32", - append_batch_size=False, - ) - out = paddle.static.nn.spectral_norm(weight, dim=1, power_iters=1) - return out - - def make_recognize_digits_conv(self): - with base.program_guard( - base.default_main_program(), base.default_startup_program() - ): - images = self._get_data( - name='pixel', shape=[1, 28, 28], dtype='float32' - ) - label = self._get_data(name='label', shape=[1], dtype='int64') - conv_pool_1 = nets.simple_img_conv_pool( - input=images, - filter_size=5, - num_filters=2, - pool_size=2, - pool_stride=2, - act="relu", - ) - conv_pool_2 = nets.simple_img_conv_pool( - input=conv_pool_1, - filter_size=5, - num_filters=4, - pool_size=2, - pool_stride=2, - act="relu", - ) - - conv_pool_2_new = paddle.reshape( - conv_pool_2, - [ - conv_pool_2.shape[0], - conv_pool_2.shape[1] - * conv_pool_2.shape[2] - * conv_pool_2.shape[3], - ], - ) - predict = paddle.nn.Linear( - conv_pool_2.shape[1] - * conv_pool_2.shape[2] - * conv_pool_2.shape[3], - 10, - )(conv_pool_2_new) - predict = paddle.nn.functional.softmax(predict) - cost = paddle.nn.functional.cross_entropy( - input=predict, label=label, reduction='none', use_softmax=False - ) - avg_cost = paddle.mean(cost) - return avg_cost - - def make_uniform_random_batch_size_like(self): - with base.program_guard( - base.default_main_program(), base.default_startup_program() - ): - input = self._get_data( - name="input", shape=[13, 11], dtype='float32' - ) - out = random.uniform_random_batch_size_like(input, [-1, 11]) - return out - - def test_row_conv(self): - # TODO(minqiyang): dygraph do not support lod now - with self.static_graph(): - x = paddle.static.data(name='x', shape=[-1, 16], dtype='float32') - out = paddle.static.nn.row_conv(input=x, future_context_size=2) - return out - - def test_simple_conv2d(self): - # TODO(minqiyang): dygraph do not support layers with param now - with self.static_graph(): - images = paddle.static.data( - name='pixel', shape=[-1, 3, 48, 48], dtype='float32' - ) - return paddle.static.nn.conv2d( - input=images, num_filters=3, filter_size=[4, 4] - ) - - def test_shuffle_batch(self): - # TODO(minqiyang): dygraph do not support lod now - with self.static_graph(): - x = paddle.static.data(name='X', shape=[-1, 4, 50], dtype='float32') - out1 = shuffle_batch(x) - paddle.seed(1000) - out2 = shuffle_batch(x) - self.assertIsNotNone(out1) - self.assertIsNotNone(out2) - return out1 - - def test_rank_attention(self): - with self.static_graph(): - input = paddle.static.data( - name="input", shape=[None, 2], dtype="float32" - ) - rank_offset = paddle.static.data( - name="rank_offset", shape=[None, 7], dtype="int32" - ) - out = rank_attention( - input=input, - rank_offset=rank_offset, - rank_param_shape=[18, 3], - rank_param_attr=base.ParamAttr( - learning_rate=1.0, - name="ubm_rank_param.w_0", - initializer=paddle.nn.initializer.XavierNormal(), - ), - max_rank=3, - ) - return out - - def test_partial_sum(self): - with self.static_graph(): - x = paddle.static.data(name="x", shape=[None, 3], dtype="float32") - y = paddle.static.data(name="y", shape=[None, 3], dtype="float32") - sum = partial_sum([x, y], start_index=0, length=2) - return sum - - def test_partial_concat(self): - with self.static_graph(): - x = paddle.static.data(name="x", shape=[None, 3], dtype="float32") - y = paddle.static.data(name="y", shape=[None, 3], dtype="float32") - concat1 = partial_concat([x, y], start_index=0, length=2) - concat2 = partial_concat(x, start_index=0, length=-1) - return concat1, concat2 - - def test_batch_fc(self): - with self.static_graph(): - input = paddle.static.data( - name="input", shape=[16, 2, 3], dtype="float32" - ) - out = batch_fc( - input=input, - param_size=[16, 3, 10], - param_attr=base.ParamAttr( - learning_rate=1.0, - name="w_0", - initializer=paddle.nn.initializer.XavierNormal(), - ), - bias_size=[16, 10], - bias_attr=base.ParamAttr( - learning_rate=1.0, - name="b_0", - initializer=paddle.nn.initializer.XavierNormal(), - ), - act="relu", - ) - return out - - -if __name__ == '__main__': - unittest.main() diff --git a/test/deprecated/legacy_test/test_save_load_deprecated.py b/test/deprecated/legacy_test/test_save_load_deprecated.py deleted file mode 100644 index 4f89d5249046ef..00000000000000 --- a/test/deprecated/legacy_test/test_save_load_deprecated.py +++ /dev/null @@ -1,1246 +0,0 @@ -# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import errno -import os -import pickle -import tempfile -import unittest -from io import BytesIO - -import numpy as np -from test_imperative_base import new_program_scope - -import paddle -from paddle import base, nn -from paddle.base import core, framework -from paddle.jit.api import to_static -from paddle.jit.translated_layer import INFER_PARAMS_INFO_SUFFIX -from paddle.nn import Linear -from paddle.optimizer import Adam -from paddle.static import InputSpec - -IMAGE_SIZE = 784 -CLASS_NUM = 10 - -SEED = 10 - - -class LinearNet(nn.Layer): - def __init__(self): - super().__init__() - self._linear = nn.Linear(IMAGE_SIZE, CLASS_NUM) - - def forward(self, x): - return self._linear(x) - - -class LinearNetReturnHidden(paddle.nn.Layer): - def __init__(self, in_size, out_size): - super().__init__() - self._linear_1 = Linear(in_size, out_size) - self._linear_2 = Linear(in_size, out_size) - - @to_static - def forward(self, x): - y = self._linear_1(x) - z = self._linear_2(y) - loss = paddle.mean(z) - return y, loss - - -class TestSaveLoadProgram(unittest.TestCase): - def test_save_load_program(self): - paddle.enable_static() - temp_dir = tempfile.TemporaryDirectory() - - with new_program_scope(): - layer = LinearNet() - data = paddle.static.data( - name='x_static_save', shape=(None, IMAGE_SIZE), dtype='float32' - ) - y_static = layer(data) - main_program = paddle.static.default_main_program() - startup_program = paddle.static.default_startup_program() - origin_main = main_program.desc.serialize_to_string() - origin_startup = startup_program.desc.serialize_to_string() - path1 = os.path.join( - temp_dir.name, - "test_paddle_save_load_program/main_program.pdmodel", - ) - path2 = os.path.join( - temp_dir.name, - "test_paddle_save_load_program/startup_program.pdmodel", - ) - paddle.save(main_program, path1) - paddle.save(startup_program, path2) - - with new_program_scope(): - load_main = paddle.load(path1).desc.serialize_to_string() - load_startup = paddle.load(path2).desc.serialize_to_string() - self.assertTrue(origin_main == load_main) - self.assertTrue(origin_startup == load_startup) - temp_dir.cleanup() - - -class TestJitPruneModelAndLoad(unittest.TestCase): - def setUp(self): - self.linear_size = 4 - self.temp_dir = tempfile.TemporaryDirectory() - self.model_path = os.path.join( - self.temp_dir.name, "jit_prune_model_and_load/model" - ) - # enable dygraph mode - base.enable_dygraph() - # config seed - paddle.seed(SEED) - paddle.framework.random._manual_program_seed(SEED) - - def tearDown(self): - self.temp_dir.cleanup() - - def train_and_save(self): - train_layer = LinearNetReturnHidden(8, 8) - train_layer = to_static( - train_layer, - input_spec=[InputSpec([None, 8], name='x')], - full_graph=True, - ) - adam = paddle.optimizer.Adam( - learning_rate=0.1, parameters=train_layer.parameters() - ) - x = paddle.to_tensor(np.random.random((4, 8)).astype('float32')) - for i in range(10): - hidden, loss = train_layer(x) - loss.backward() - adam.minimize(loss) - train_layer.clear_gradients() - - output_spec = train_layer.forward.outputs[:1] - paddle.jit.save( - layer=train_layer, - path=self.model_path, - input_spec=[x], - output_spec=output_spec, - ) - - return train_layer - - # pir has no need to save extra var info, param always saved with program, - # and trainable info saved in program's op attr - def test_load_var_not_in_extra_var_info(self): - self.train_and_save() - - # change extra var info - var_info_path = self.model_path + INFER_PARAMS_INFO_SUFFIX - with open(var_info_path, 'rb') as f: - extra_var_info = pickle.load(f) - extra_var_info.clear() - with open(var_info_path, 'wb') as f: - pickle.dump(extra_var_info, f, protocol=2) - - with self.assertRaises(RuntimeError): - paddle.jit.load(self.model_path) - - -class TestSaveLoadToMemory(unittest.TestCase): - def test_static_save_to_memory(self): - paddle.enable_static() - with new_program_scope(): - # create network - x = paddle.static.data( - name="x", shape=[None, IMAGE_SIZE], dtype='float32' - ) - z = paddle.static.nn.fc(x, 10, bias_attr=False) - z = paddle.static.nn.fc(z, 128, bias_attr=False) - loss = paddle.mean(z) - place = ( - base.CPUPlace() - if not paddle.base.core.is_compiled_with_cuda() - else base.CUDAPlace(0) - ) - prog = paddle.static.default_main_program() - exe = paddle.static.Executor(place) - exe.run(paddle.static.default_startup_program()) - - state_dict = prog.state_dict() - keys = list(state_dict.keys()) - tensor = state_dict[keys[0]] - - byio = BytesIO() - byio2 = BytesIO() - paddle.save(prog, byio2) - paddle.save(tensor, byio) - paddle.save(state_dict, byio) - byio.seek(0) - byio2.seek(0) - - prog_load = paddle.load(byio2) - self.assertTrue( - prog.desc.serialize_to_string() - == prog_load.desc.serialize_to_string() - ) - - tensor_load = paddle.load(byio, return_numpy=True) - np.testing.assert_array_equal(tensor_load, np.array(tensor)) - - state_dict_load = paddle.load(byio, return_numpy=True) - for k, v in state_dict.items(): - np.testing.assert_array_equal(np.array(v), state_dict_load[k]) - - -class PtbModel(paddle.nn.Layer): - def __init__( - self, - name_scope, - hidden_size, - vocab_size, - num_layers=2, - num_steps=20, - init_scale=0.1, - dropout=None, - ): - super().__init__() - self.hidden_size = hidden_size - self.vocab_size = vocab_size - self.init_scale = init_scale - self.num_layers = num_layers - self.num_steps = num_steps - self.dropout = dropout - self.simple_lstm_rnn = SimpleLSTMRNN( - self.full_name(), - hidden_size, - num_steps, - num_layers=num_layers, - init_scale=init_scale, - dropout=dropout, - ) - self.embedding = paddle.nn.Embedding( - num_embeddings=vocab_size, - embedding_dim=hidden_size, - weight_attr=base.ParamAttr( - name='embedding_para', - initializer=paddle.nn.initializer.Uniform( - low=-init_scale, high=init_scale - ), - ), - ) - self.softmax_weight = self.create_parameter( - attr=base.ParamAttr(), - shape=[self.hidden_size, self.vocab_size], - dtype="float32", - default_initializer=paddle.nn.initializer.Uniform( - low=-self.init_scale, high=self.init_scale - ), - ) - self.softmax_bias = self.create_parameter( - attr=base.ParamAttr(), - shape=[self.vocab_size], - dtype="float32", - default_initializer=paddle.nn.initializer.Uniform( - low=-self.init_scale, high=self.init_scale - ), - ) - - def forward(self, input, label, init_hidden, init_cell): - init_h = paddle.reshape( - init_hidden, shape=[self.num_layers, -1, self.hidden_size] - ) - - init_c = paddle.reshape( - init_cell, shape=[self.num_layers, -1, self.hidden_size] - ) - - # NPU 'tok_k' kernel only support `int32` dtype, so cast `input` from `int64` to `int32`. - input = paddle.cast(input, "int32") - x_emb = self.embedding(input) - x_emb = paddle.reshape( - x_emb, shape=[-1, self.num_steps, self.hidden_size] - ) - if self.dropout is not None and self.dropout > 0.0: - x_emb = paddle.nn.functional.dropout( - x_emb, - p=self.drop_out, - mode='upscale_in_train', - ) - rnn_out, last_hidden, last_cell = self.simple_lstm_rnn( - x_emb, init_h, init_c - ) - - rnn_out = paddle.reshape( - rnn_out, shape=[-1, self.num_steps, self.hidden_size] - ) - projection = paddle.matmul(rnn_out, self.softmax_weight) - projection = paddle.add(projection, self.softmax_bias) - projection = paddle.reshape(projection, shape=[-1, self.vocab_size]) - loss = paddle.nn.functional.softmax_with_cross_entropy( - logits=projection, label=label, soft_label=False - ) - loss = paddle.reshape(loss, shape=[-1, self.num_steps]) - loss = paddle.mean(loss, axis=[0]) - loss = paddle.sum(loss) - - return loss, last_hidden, last_cell - - -class SimpleLSTMRNN(paddle.nn.Layer): - def __init__( - self, - name_scope, - hidden_size, - num_steps, - num_layers=2, - init_scale=0.1, - dropout=None, - ): - super().__init__() - self._hidden_size = hidden_size - self._num_layers = num_layers - self._init_scale = init_scale - self._dropout = dropout - self._input = None - self._num_steps = num_steps - self.cell_array = [] - self.hidden_array = [] - - self.weight_1_arr = [] - self.weight_2_arr = [] - self.bias_arr = [] - self.mask_array = [] - - for i in range(self._num_layers): - weight_1 = self.create_parameter( - attr=base.ParamAttr( - initializer=paddle.nn.initializer.Uniform( - low=-self._init_scale, high=self._init_scale - ) - ), - shape=[self._hidden_size * 2, self._hidden_size * 4], - dtype="float32", - default_initializer=paddle.nn.initializer.Uniform( - low=-self._init_scale, high=self._init_scale - ), - ) - self.weight_1_arr.append(self.add_parameter(f'w_{i}', weight_1)) - bias_1 = self.create_parameter( - attr=base.ParamAttr( - initializer=paddle.nn.initializer.Uniform( - low=-self._init_scale, high=self._init_scale - ) - ), - shape=[self._hidden_size * 4], - dtype="float32", - default_initializer=paddle.nn.initializer.Constant(0.0), - ) - self.bias_arr.append(self.add_parameter(f'b_{i}', bias_1)) - - def forward(self, input_embedding, init_hidden=None, init_cell=None): - self.cell_array = [] - self.hidden_array = [] - - for i in range(self._num_layers): - pre_hidden = paddle.slice( - init_hidden, axes=[0], starts=[i], ends=[i + 1] - ) - pre_cell = paddle.slice( - init_cell, axes=[0], starts=[i], ends=[i + 1] - ) - pre_hidden = paddle.reshape( - pre_hidden, shape=[-1, self._hidden_size] - ) - pre_cell = paddle.reshape(pre_cell, shape=[-1, self._hidden_size]) - self.hidden_array.append(pre_hidden) - self.cell_array.append(pre_cell) - - res = [] - for index in range(self._num_steps): - self._input = paddle.slice( - input_embedding, axes=[1], starts=[index], ends=[index + 1] - ) - self._input = paddle.reshape( - self._input, shape=[-1, self._hidden_size] - ) - for k in range(self._num_layers): - pre_hidden = self.hidden_array[k] - pre_cell = self.cell_array[k] - weight_1 = self.weight_1_arr[k] - bias = self.bias_arr[k] - - nn = paddle.concat([self._input, pre_hidden], 1) - gate_input = paddle.matmul(x=nn, y=weight_1) - - gate_input = paddle.add(gate_input, bias) - i, j, f, o = paddle.split( - gate_input, num_or_sections=4, axis=-1 - ) - c = pre_cell * paddle.nn.functional.sigmoid( - f - ) + paddle.nn.functional.sigmoid(i) * paddle.tanh(j) - m = paddle.tanh(c) * paddle.nn.functional.sigmoid(o) - self.hidden_array[k] = m - self.cell_array[k] = c - self._input = m - - if self._dropout is not None and self._dropout > 0.0: - self._input = paddle.nn.functional.dropout( - self._input, - p=self._dropout, - mode='upscale_in_train', - ) - res.append( - paddle.reshape(self._input, shape=[1, -1, self._hidden_size]) - ) - real_res = paddle.concat(res, 0) - real_res = paddle.transpose(x=real_res, perm=[1, 0, 2]) - last_hidden = paddle.concat(self.hidden_array, 1) - last_hidden = paddle.reshape( - last_hidden, shape=[-1, self._num_layers, self._hidden_size] - ) - last_hidden = paddle.transpose(x=last_hidden, perm=[1, 0, 2]) - last_cell = paddle.concat(self.cell_array, 1) - last_cell = paddle.reshape( - last_cell, shape=[-1, self._num_layers, self._hidden_size] - ) - last_cell = paddle.transpose(x=last_cell, perm=[1, 0, 2]) - return real_res, last_hidden, last_cell - - -class TestLoadFromOldInterface(unittest.TestCase): - def setUp(self): - paddle.enable_static() - if os.path.exists("test_path.pdparams"): - os.remove("test_path.pdparams") - - if os.path.exists("test_static_load_var_list.pdparams"): - os.remove("test_static_load_var_list.pdparams") - - self.temp_dir = tempfile.TemporaryDirectory() - - def set_place(self): - return ( - base.CPUPlace() - if not core.is_compiled_with_cuda() - else base.CUDAPlace(0) - ) - - def tearDown(self): - self.temp_dir.cleanup() - - def test_load_from_old_interface(self): - seed = 90 - hidden_size = 10 - vocab_size = 1000 - num_layers = 1 - num_steps = 3 - init_scale = 0.1 - batch_size = 4 - batch_num = 200 - - with new_program_scope(): - paddle.seed(seed) - ptb_model = PtbModel( - "ptb_model", - hidden_size=hidden_size, - vocab_size=vocab_size, - num_layers=num_layers, - num_steps=num_steps, - init_scale=init_scale, - ) - - place = self.set_place() - exe = base.Executor(place) - sgd = Adam(learning_rate=1e-3) - x = paddle.static.data( - name="x", shape=[-1, num_steps], dtype='int64' - ) - x.desc.set_need_check_feed(False) - y = paddle.static.data(name="y", shape=[-1, 1], dtype='float32') - y.desc.set_need_check_feed(False) - init_hidden = paddle.static.data( - name="init_hidden", shape=[-1, 1], dtype='float32' - ) - init_hidden.desc.set_need_check_feed(False) - init_cell = paddle.static.data( - name="init_cell", shape=[-1, 1], dtype='float32' - ) - init_cell.desc.set_need_check_feed(False) - - static_loss, static_last_hidden, static_last_cell = ptb_model( - x, y, init_hidden, init_cell - ) - - test_clone_program = base.default_main_program().clone() - sgd.minimize(static_loss) - static_param_updated = {} - static_param_init = {} - - out = exe.run(framework.default_startup_program()) - - static_loss_value = None - static_last_cell_value = None - static_last_hidden_value = None - for i in range(batch_num): - x_data = np.arange(12).reshape(4, 3).astype('int64') - y_data = np.arange(1, 13).reshape(4, 3).astype('int64') - x_data = x_data.reshape((-1, num_steps, 1)) - y_data = y_data.reshape((-1, 1)) - init_hidden_data = np.zeros( - (num_layers, batch_size, hidden_size), dtype='float32' - ) - init_cell_data = np.zeros( - (num_layers, batch_size, hidden_size), dtype='float32' - ) - fetch_list = [static_loss, static_last_hidden, static_last_cell] - out = exe.run( - base.default_main_program(), - feed={ - "x": x_data, - "y": y_data, - "init_hidden": init_hidden_data, - "init_cell": init_cell_data, - }, - fetch_list=fetch_list, - ) - static_loss_value = out[0] - static_last_hidden_value = out[1] - static_last_cell_value = out[2] - - # get value before save - main_program = framework.default_main_program() - base_map = {} - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - t = np.array( - base.global_scope().find_var(var.name).get_tensor() - ) - # make sure all the parameter or optimizer var have been update - self.assertTrue(np.sum(np.abs(t)) != 0) - base_map[var.name] = t - - # base.save(main_program, "./test_1") - paddle.distributed.io.save_persistables( - exe, os.path.join(self.temp_dir.name, "test_path"), main_program - ) - - # set var to zero - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - ten = base.global_scope().find_var(var.name).get_tensor() - ten.set(np.zeros_like(np.array(ten)), place) - - new_t = np.array( - base.global_scope().find_var(var.name).get_tensor() - ) - # make sure all the parameter or optimizer var have been set to zero - self.assertTrue(np.sum(np.abs(new_t)) == 0) - - paddle.static.load( - main_program, os.path.join(self.temp_dir.name, "test_path"), exe - ) - - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - new_t = np.array( - base.global_scope().find_var(var.name).get_tensor() - ) - base_t = base_map[var.name] - np.testing.assert_array_equal(new_t, base_t) - - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - ten = base.global_scope().find_var(var.name).get_tensor() - old_shape = np.array(ten).shape - new_shape = [e + 10 for e in old_shape] - - var.desc.set_shape(new_shape) - with self.assertRaises(RuntimeError): - paddle.static.load( - main_program, - os.path.join(self.temp_dir.name, "test_path"), - exe, - ) - - # check unused parameter - - paddle.static.load( - test_clone_program, - os.path.join(self.temp_dir.name, "test_path"), - exe, - ) - - def test_load_from_old_interface_var_list(self): - seed = 90 - hidden_size = 10 - vocab_size = 1000 - num_layers = 1 - num_steps = 3 - init_scale = 0.1 - batch_size = 4 - batch_num = 200 - - with new_program_scope(): - paddle.seed(seed) - ptb_model = PtbModel( - "ptb_model", - hidden_size=hidden_size, - vocab_size=vocab_size, - num_layers=num_layers, - num_steps=num_steps, - init_scale=init_scale, - ) - - place = self.set_place() - exe = base.Executor(place) - sgd = Adam(learning_rate=1e-3) - x = paddle.static.data( - name="x", shape=[-1, num_steps], dtype='int64' - ) - x.desc.set_need_check_feed(False) - y = paddle.static.data(name="y", shape=[-1, 1], dtype='float32') - y.desc.set_need_check_feed(False) - init_hidden = paddle.static.data( - name="init_hidden", shape=[-1, 1], dtype='float32' - ) - init_hidden.desc.set_need_check_feed(False) - init_cell = paddle.static.data( - name="init_cell", shape=[-1, 1], dtype='float32' - ) - init_cell.desc.set_need_check_feed(False) - static_loss, static_last_hidden, static_last_cell = ptb_model( - x, y, init_hidden, init_cell - ) - - test_clone_program = base.default_main_program().clone() - sgd.minimize(static_loss) - static_param_updated = {} - static_param_init = {} - - out = exe.run(framework.default_startup_program()) - - static_loss_value = None - static_last_cell_value = None - static_last_hidden_value = None - for i in range(batch_num): - x_data = np.arange(12).reshape(4, 3).astype('int64') - y_data = np.arange(1, 13).reshape(4, 3).astype('int64') - x_data = x_data.reshape((-1, num_steps, 1)) - y_data = y_data.reshape((-1, 1)) - init_hidden_data = np.zeros( - (num_layers, batch_size, hidden_size), dtype='float32' - ) - init_cell_data = np.zeros( - (num_layers, batch_size, hidden_size), dtype='float32' - ) - fetch_list = [static_loss, static_last_hidden, static_last_cell] - out = exe.run( - base.default_main_program(), - feed={ - "x": x_data, - "y": y_data, - "init_hidden": init_hidden_data, - "init_cell": init_cell_data, - }, - fetch_list=fetch_list, - ) - static_loss_value = out[0] - static_last_hidden_value = out[1] - static_last_cell_value = out[2] - - # get value before save - main_program = framework.default_main_program() - base_map = {} - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - t = np.array( - base.global_scope().find_var(var.name).get_tensor() - ) - # make sure all the parameter or optimizer var have been update - self.assertTrue(np.sum(np.abs(t)) != 0) - base_map[var.name] = t - - # base.save(main_program, "./test_1") - paddle.distributed.io.save_persistables( - exe, - os.path.join(self.temp_dir.name, "test_static_load_var_list"), - main_program, - ) - - # set var to zero - var_list = [] - for i, var in enumerate(main_program.list_vars()): - if isinstance(var, framework.Parameter) or var.persistable: - if i % 2 == 0: - var_list.append(var) - ten = base.global_scope().find_var(var.name).get_tensor() - ten.set(np.zeros_like(np.array(ten)), place) - - new_t = np.array( - base.global_scope().find_var(var.name).get_tensor() - ) - # make sure all the parameter or optimizer var have been set to zero - self.assertTrue(np.sum(np.abs(new_t)) == 0) - - paddle.static.load( - main_program, - os.path.join(self.temp_dir.name, "test_static_load_var_list"), - exe, - var_list, - ) - var_list_names = [var.name for var in var_list] - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - new_t = np.array( - base.global_scope().find_var(var.name).get_tensor() - ) - if var.name in var_list_names: - # loaded vars - base_t = base_map[var.name] - np.testing.assert_array_equal(new_t, base_t) - else: - # not loaded vars - self.assertTrue(np.sum(np.abs(new_t)) == 0) - - -class TestLoadFromOldInterfaceSingleFile(unittest.TestCase): - def set_place(self): - return ( - base.CPUPlace() - if not core.is_compiled_with_cuda() - else base.CUDAPlace(0) - ) - - def test_load_from_old_interface(self): - seed = 90 - hidden_size = 10 - vocab_size = 1000 - num_layers = 1 - num_steps = 3 - init_scale = 0.1 - batch_size = 4 - batch_num = 200 - temp_dir = tempfile.TemporaryDirectory() - paddle.enable_static() - with new_program_scope(): - paddle.seed(seed) - ptb_model = PtbModel( - "ptb_model", - hidden_size=hidden_size, - vocab_size=vocab_size, - num_layers=num_layers, - num_steps=num_steps, - init_scale=init_scale, - ) - - place = self.set_place() - exe = base.Executor(place) - sgd = Adam(learning_rate=1e-3) - x = paddle.static.data( - name="x", shape=[-1, num_steps], dtype='int64' - ) - x.desc.set_need_check_feed(False) - y = paddle.static.data(name="y", shape=[-1, 1], dtype='float32') - y.desc.set_need_check_feed(False) - init_hidden = paddle.static.data( - name="init_hidden", shape=[-1, 1], dtype='float32' - ) - init_hidden.desc.set_need_check_feed(False) - init_cell = paddle.static.data( - name="init_cell", shape=[-1, 1], dtype='float32' - ) - init_cell.desc.set_need_check_feed(False) - - static_loss, static_last_hidden, static_last_cell = ptb_model( - x, y, init_hidden, init_cell - ) - sgd.minimize(static_loss) - static_param_updated = {} - static_param_init = {} - - out = exe.run(framework.default_startup_program()) - - static_loss_value = None - static_last_cell_value = None - static_last_hidden_value = None - for i in range(batch_num): - x_data = np.arange(12).reshape(4, 3).astype('int64') - y_data = np.arange(1, 13).reshape(4, 3).astype('int64') - x_data = x_data.reshape((-1, num_steps, 1)) - y_data = y_data.reshape((-1, 1)) - init_hidden_data = np.zeros( - (num_layers, batch_size, hidden_size), dtype='float32' - ) - init_cell_data = np.zeros( - (num_layers, batch_size, hidden_size), dtype='float32' - ) - fetch_list = [static_loss, static_last_hidden, static_last_cell] - out = exe.run( - base.default_main_program(), - feed={ - "x": x_data, - "y": y_data, - "init_hidden": init_hidden_data, - "init_cell": init_cell_data, - }, - fetch_list=fetch_list, - ) - static_loss_value = out[0] - static_last_hidden_value = out[1] - static_last_cell_value = out[2] - - # get value before save - main_program = framework.default_main_program() - base_map = {} - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - t = np.array( - base.global_scope().find_var(var.name).get_tensor() - ) - # make sure all the parameter or optimizer var have been update - self.assertTrue(np.sum(np.abs(t)) != 0) - base_map[var.name] = t - save_dir = os.path.join(temp_dir.name, "test_path") - # base.save(main_program, "./test_1") - paddle.distributed.io.save_persistables( - exe, save_dir, main_program, filename="model_single" - ) - - # set var to zero - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - ten = base.global_scope().find_var(var.name).get_tensor() - ten.set(np.zeros_like(np.array(ten)), place) - - new_t = np.array( - base.global_scope().find_var(var.name).get_tensor() - ) - # make sure all the parameter or optimizer var have been set to zero - self.assertTrue(np.sum(np.abs(new_t)) == 0) - - file_model_path = os.path.join(save_dir, "model_single") - paddle.static.load( - main_program, - file_model_path, - exe, - paddle.static.io.get_program_persistable_vars(main_program), - ) - - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - new_t = np.array( - base.global_scope().find_var(var.name).get_tensor() - ) - base_t = base_map[var.name] - np.testing.assert_array_equal(new_t, base_t) - - # test exception - # change shape - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - ten = base.global_scope().find_var(var.name).get_tensor() - old_shape = np.array(ten).shape - new_shape = [e + 10 for e in old_shape] - - var.desc.set_shape(new_shape) - - with self.assertRaises(RuntimeError): - paddle.static.load( - main_program, - file_model_path, - exe, - paddle.static.io.get_program_persistable_vars(main_program), - ) - - with self.assertRaises(RuntimeError): - paddle.static.load( - main_program, - file_model_path, - exe, - paddle.static.io.get_program_persistable_vars(main_program), - ) - - # check when executor is None - with self.assertRaises(ValueError): - paddle.static.load( - main_program, - file_model_path, - None, - paddle.static.io.get_program_persistable_vars(main_program), - ) - - # check when var list is None - with self.assertRaises(ValueError): - paddle.static.load(main_program, file_model_path, exe, None) - - # check save params, load var_list = get_program_persistable_vars - with self.assertRaises(RuntimeError): - temp_var = framework.Variable( - main_program.global_block(), shape=[1], name="test_temp_var" - ) - all_var_list = list(main_program.list_vars()) - paddle.static.load( - main_program, - file_model_path, - exe, - [*all_var_list, temp_var], - ) - temp_dir.cleanup() - - -class TestProgramStateOldSave(unittest.TestCase): - def setUp(self): - self.test_dygraph = True - self.temp_dir = tempfile.TemporaryDirectory() - - def tearDown(self): - self.temp_dir.cleanup() - - def set_place(self): - return ( - base.CPUPlace() - if not core.is_compiled_with_cuda() - else base.CUDAPlace(0) - ) - - def test_ptb_rnn_cpu_float32(self): - seed = 90 - hidden_size = 10 - vocab_size = 1000 - num_layers = 1 - num_steps = 3 - init_scale = 0.1 - batch_size = 4 - batch_num = 200 - - with new_program_scope(): - paddle.seed(seed) - ptb_model = PtbModel( - "ptb_model", - hidden_size=hidden_size, - vocab_size=vocab_size, - num_layers=num_layers, - num_steps=num_steps, - init_scale=init_scale, - ) - - place = self.set_place() - exe = base.Executor(place) - sgd = Adam(learning_rate=1e-3) - x = paddle.static.data( - name="x", shape=[-1, num_steps], dtype='int64' - ) - x.desc.set_need_check_feed(False) - y = paddle.static.data(name="y", shape=[-1, 1], dtype='float32') - y.desc.set_need_check_feed(False) - init_hidden = paddle.static.data( - name="init_hidden", shape=[-1, 1], dtype='float32' - ) - init_hidden.desc.set_need_check_feed(False) - init_cell = paddle.static.data( - name="init_cell", shape=[-1, 1], dtype='float32' - ) - init_cell.desc.set_need_check_feed(False) - - static_loss, static_last_hidden, static_last_cell = ptb_model( - x, y, init_hidden, init_cell - ) - - test_program = base.default_main_program().clone(for_test=True) - - add_1 = paddle.static.nn.fc( - static_last_hidden, - size=hidden_size, - num_flatten_dims=2, - bias_attr=False, - ) - - sgd.minimize(static_loss) - static_param_updated = {} - static_param_init = {} - - out = exe.run(framework.default_startup_program()) - - static_loss_value = None - static_last_cell_value = None - static_last_hidden_value = None - for i in range(batch_num): - x_data = np.arange(12).reshape(4, 3).astype('int64') - y_data = np.arange(1, 13).reshape(4, 3).astype('int64') - x_data = x_data.reshape((-1, num_steps, 1)) - y_data = y_data.reshape((-1, 1)) - init_hidden_data = np.zeros( - (num_layers, batch_size, hidden_size), dtype='float32' - ) - init_cell_data = np.zeros( - (num_layers, batch_size, hidden_size), dtype='float32' - ) - fetch_list = [static_loss, static_last_hidden, static_last_cell] - out = exe.run( - base.default_main_program(), - feed={ - "x": x_data, - "y": y_data, - "init_hidden": init_hidden_data, - "init_cell": init_cell_data, - }, - fetch_list=fetch_list, - ) - static_loss_value = out[0] - static_last_hidden_value = out[1] - static_last_cell_value = out[2] - - # get value before save - main_program = framework.default_main_program() - base_map = {} - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - t = np.array( - base.global_scope().find_var(var.name).get_tensor() - ) - # make sure all the parameter or optimizer var have been update - self.assertTrue(np.sum(np.abs(t)) != 0) - base_map[var.name] = t - save_dir = os.path.join(self.temp_dir.name, "test_program_1") - paddle.distributed.io.save_persistables(exe, save_dir, main_program) - - # set var to zero - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - ten = base.global_scope().find_var(var.name).get_tensor() - ten.set(np.zeros_like(np.array(ten)), place) - - new_t = np.array( - base.global_scope().find_var(var.name).get_tensor() - ) - # make sure all the parameter or optimizer var have been set to zero - self.assertTrue(np.sum(np.abs(new_t)) == 0) - - # case 1: load basic - program_state = paddle.static.load_program_state(save_dir) - paddle.static.set_program_state(main_program, program_state) - self.check_in_static(main_program, base_map) - - # case 2: load with no need file - def symlink_force(target, link_name): - try: - self.create_symlink(target, link_name) - except OSError as e: - if e.errno == errno.EEXIST: - os.remove(link_name) - self.create_symlink(target, link_name) - else: - raise e - - program_state = paddle.static.load_program_state(save_dir) - paddle.static.set_program_state(main_program, program_state) - self.check_in_static(main_program, base_map) - - # case 3: load with var_list - program_state = paddle.static.load_program_state( - save_dir, main_program.all_parameters() - ) - paddle.static.set_program_state(main_program, program_state) - self.check_in_static(main_program, base_map) - - if self.test_dygraph: - # make sure `load_program_state` can be used in dynamic graph mode - with base.dygraph.guard(place): - load_state = paddle.static.load_program_state(save_dir) - for k, v in load_state.items(): - np.testing.assert_array_equal(base_map[k], v) - - def create_symlink(self, target, link_name): - try: - os.symlink(target, link_name) - except AttributeError: - import ctypes - - kernel_dll = ctypes.windll.LoadLibrary("kernel32.dll") - kernel_dll.CreateSymbolicLinkA(target, link_name, 0) - - def check_in_static(self, main_program, base_map): - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - new_t = np.array( - base.global_scope().find_var(var.name).get_tensor() - ) - base_t = base_map[var.name] - np.testing.assert_array_equal(new_t, base_t) - - -class TestProgramStateOldSaveSingleModel(unittest.TestCase): - def set_place(self): - return ( - base.CPUPlace() - if not core.is_compiled_with_cuda() - else base.CUDAPlace(0) - ) - - def test_ptb_rnn_cpu_float32(self): - seed = 90 - hidden_size = 10 - vocab_size = 1000 - num_layers = 1 - num_steps = 3 - init_scale = 0.1 - batch_size = 4 - batch_num = 200 - temp_dir = tempfile.TemporaryDirectory() - - with new_program_scope(): - paddle.seed(seed) - ptb_model = PtbModel( - "ptb_model", - hidden_size=hidden_size, - vocab_size=vocab_size, - num_layers=num_layers, - num_steps=num_steps, - init_scale=init_scale, - ) - - place = self.set_place() - exe = base.Executor(place) - sgd = Adam(learning_rate=1e-3) - x = paddle.static.data( - name="x", shape=[-1, num_steps], dtype='int64' - ) - x.desc.set_need_check_feed(False) - y = paddle.static.data(name="y", shape=[-1, 1], dtype='float32') - y.desc.set_need_check_feed(False) - init_hidden = paddle.static.data( - name="init_hidden", shape=[-1, 1], dtype='float32' - ) - init_hidden.desc.set_need_check_feed(False) - init_cell = paddle.static.data( - name="init_cell", shape=[-1, 1], dtype='float32' - ) - init_cell.desc.set_need_check_feed(False) - - static_loss, static_last_hidden, static_last_cell = ptb_model( - x, y, init_hidden, init_cell - ) - - test_program = base.default_main_program().clone(for_test=True) - - add_1 = paddle.static.nn.fc( - static_last_hidden, - size=hidden_size, - num_flatten_dims=2, - bias_attr=False, - ) - - sgd.minimize(static_loss) - static_param_updated = {} - static_param_init = {} - - out = exe.run(framework.default_startup_program()) - - static_loss_value = None - static_last_cell_value = None - static_last_hidden_value = None - for i in range(batch_num): - x_data = np.arange(12).reshape(4, 3).astype('int64') - y_data = np.arange(1, 13).reshape(4, 3).astype('int64') - x_data = x_data.reshape((-1, num_steps, 1)) - y_data = y_data.reshape((-1, 1)) - init_hidden_data = np.zeros( - (num_layers, batch_size, hidden_size), dtype='float32' - ) - init_cell_data = np.zeros( - (num_layers, batch_size, hidden_size), dtype='float32' - ) - fetch_list = [static_loss, static_last_hidden, static_last_cell] - out = exe.run( - base.default_main_program(), - feed={ - "x": x_data, - "y": y_data, - "init_hidden": init_hidden_data, - "init_cell": init_cell_data, - }, - fetch_list=fetch_list, - ) - static_loss_value = out[0] - static_last_hidden_value = out[1] - static_last_cell_value = out[2] - - # get value before save - main_program = framework.default_main_program() - base_map = {} - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - t = np.array( - base.global_scope().find_var(var.name).get_tensor() - ) - # make sure all the parameter or optimizer var have been update - self.assertTrue(np.sum(np.abs(t)) != 0) - base_map[var.name] = t - - save_dir = os.path.join(temp_dir.name, "test_program_2") - paddle.distributed.io.save_persistables( - exe, save_dir, main_program, filename="model_1" - ) - - # set var to zero - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - ten = base.global_scope().find_var(var.name).get_tensor() - ten.set(np.zeros_like(np.array(ten)), place) - - new_t = np.array( - base.global_scope().find_var(var.name).get_tensor() - ) - # make sure all the parameter or optimizer var have been set to zero - self.assertTrue(np.sum(np.abs(new_t)) == 0) - - # base.load(test_program, "./test_1", None ) - program_state = paddle.static.load_program_state( - os.path.join(save_dir, "model_1"), - var_list=paddle.static.io.get_program_persistable_vars( - main_program - ), - ) - paddle.static.set_program_state(main_program, program_state) - - for var in main_program.list_vars(): - if isinstance(var, framework.Parameter) or var.persistable: - new_t = np.array( - base.global_scope().find_var(var.name).get_tensor() - ) - base_t = base_map[var.name] - np.testing.assert_array_equal(new_t, base_t) - - with self.assertRaises(ValueError): - paddle.static.load_program_state( - os.path.join(save_dir, "model_1") - ) - - with self.assertRaises(TypeError): - paddle.static.load_program_state( - os.path.join(save_dir, "model_1"), var_list=["str"] - ) - - with self.assertRaises(RuntimeError): - paddle.static.load_program_state( - os.path.join(save_dir, "model_1"), - var_list=[ - main_program.global_block().create_var( - name="fake_var_name", persistable=True - ) - ], - ) - temp_dir.cleanup() - - -if __name__ == '__main__': - unittest.main() diff --git a/test/deprecated/prim/prim/vjp/static/test_comp_sigmoid_grad_deprecated.py b/test/deprecated/prim/prim/vjp/static/test_comp_sigmoid_grad_deprecated.py deleted file mode 100644 index 6de93d3f586e90..00000000000000 --- a/test/deprecated/prim/prim/vjp/static/test_comp_sigmoid_grad_deprecated.py +++ /dev/null @@ -1,113 +0,0 @@ -# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest - -import numpy as np -import parameterized as param - -import paddle -import paddle.nn.functional as F -from paddle.base import core - - -@param.parameterized_class( - ('primal', 'cotangent', 'dtype'), - [ - (np.random.rand(10, 10), np.random.rand(10, 10), np.float32), - ], -) -class TestExpGradComp(unittest.TestCase): - @classmethod - def setUpClass(cls): - core.set_prim_eager_enabled(True) - cls.primal = cls.primal.astype(cls.dtype) - if cls.cotangent is not None: - cls.cotangent = cls.cotangent.astype(cls.dtype) - - def setUp(self): - paddle.enable_static() - - def tearDown(self): - paddle.disable_static() - - def test_sigmoid_grad_comp(self): - def actual(primal, cotangent): - core._set_prim_backward_enabled(True) - paddle.enable_static() - - mp, sp = paddle.static.Program(), paddle.static.Program() - with paddle.static.program_guard(mp, sp): - x = paddle.static.data('primal', primal.shape, primal.dtype) - dout = paddle.static.data( - 'cotangent', cotangent.shape, cotangent.dtype - ) - x.stop_gradient = False - res = F.sigmoid(x) - x_grad = paddle.static.gradients(res, [x], dout) - - exe = paddle.static.Executor() - exe.run(sp) - out = exe.run( - program=mp, - feed={ - 'primal': primal, - 'cotangent': cotangent, - }, - fetch_list=[ - x_grad[0], - ], - ) - - return out[0] - - def desired(primal, cotangent): - core._set_prim_backward_enabled(False) - paddle.enable_static() - - mp, sp = paddle.static.Program(), paddle.static.Program() - with paddle.static.program_guard(mp, sp): - x = paddle.static.data('primal', primal.shape, primal.dtype) - dout = paddle.static.data( - 'cotangent', cotangent.shape, cotangent.dtype - ) - x.stop_gradient = False - res = F.sigmoid(x) - x_grad = paddle.static.gradients(res, [x], dout) - - exe = paddle.static.Executor() - exe.run(sp) - out = exe.run( - program=mp, - feed={ - 'primal': primal, - 'cotangent': cotangent, - }, - fetch_list=[ - x_grad[0], - ], - ) - - return out[0] - - np.testing.assert_allclose( - actual=actual(self.primal, self.cotangent), - desired=desired(self.primal, self.cotangent), - rtol=1e-6, - atol=0, - ) - - -if __name__ == '__main__': - unittest.main() diff --git a/test/deprecated/rnn/test_rnn_nets_static_deprecated.py b/test/deprecated/rnn/test_rnn_nets_static_deprecated.py deleted file mode 100644 index 4da187066cf466..00000000000000 --- a/test/deprecated/rnn/test_rnn_nets_static_deprecated.py +++ /dev/null @@ -1,386 +0,0 @@ -# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import paddle - -paddle.set_default_dtype("float64") - - -paddle.enable_static() - -import sys -import unittest - -import numpy as np -from convert import convert_params_for_net_static - -sys.path.append("../../rnn") -from rnn_numpy import GRU, LSTM, SimpleRNN - -bidirectional_list = ["bidirectional", "bidirect"] - - -class TestSimpleRNN(unittest.TestCase): - def __init__( - self, time_major=True, direction="forward", place="cpu", mode="RNN_TANH" - ): - super().__init__("runTest") - self.time_major = time_major - self.direction = direction - self.num_directions = 2 if direction in bidirectional_list else 1 - self.place = place - self.mode = mode - - def setUp(self): - # Since `set_device` is global, set `set_device` in `setUp` rather than - # `__init__` to avoid using an error device set by another test case. - place = paddle.set_device(self.place) - rnn1 = SimpleRNN( - 16, - 32, - 2, - time_major=self.time_major, - direction=self.direction, - nonlinearity=self.mode, - ) - - mp = paddle.static.Program() - sp = paddle.static.Program() - with ( - paddle.base.unique_name.guard(), - paddle.static.program_guard(mp, sp), - ): - rnn2 = paddle.nn.SimpleRNN( - 16, - 32, - 2, - time_major=self.time_major, - direction=self.direction, - activation=self.mode[4:].lower(), - ) - - exe = paddle.static.Executor(place) - scope = paddle.base.Scope() - with paddle.static.scope_guard(scope): - exe.run(sp) - convert_params_for_net_static(rnn1, rnn2, place) - - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - - self.place = place - self.executor = exe - self.scope = scope - - def test_with_input_lengths(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - - x = np.random.randn(12, 4, 16) - if not self.time_major: - x = np.transpose(x, [1, 0, 2]) - sequence_length = np.array([12, 10, 9, 8], dtype=np.int64) - - y1, h1 = rnn1(x, sequence_length=sequence_length) - - with ( - paddle.base.unique_name.guard(), - paddle.static.program_guard(mp, sp), - ): - x_data = paddle.static.data( - "input", - [-1, -1, 16], - dtype=paddle.framework.get_default_dtype(), - ) - seq_len = paddle.static.data("seq_len", [-1], dtype="int64") - mask = paddle.static.nn.sequence_lod.sequence_mask( - seq_len, dtype=paddle.get_default_dtype() - ) - if self.time_major: - mask = paddle.transpose(mask, [1, 0]) - y, h = rnn2(x_data, sequence_length=seq_len) - mask = paddle.unsqueeze(mask, -1) - y = paddle.multiply(y, mask) - - feed_dict = {x_data.name: x, seq_len.name: sequence_length} - - with paddle.static.scope_guard(scope): - y2, h2 = exe.run(mp, feed=feed_dict, fetch_list=[y, h]) - - np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) - np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) - - def runTest(self): - self.test_with_input_lengths() - - -class TestGRU(unittest.TestCase): - def __init__(self, time_major=True, direction="forward", place="cpu"): - super().__init__("runTest") - self.time_major = time_major - self.direction = direction - self.num_directions = 2 if direction in bidirectional_list else 1 - self.place = place - - def setUp(self): - # Since `set_device` is global, set `set_device` in `setUp` rather than - # `__init__` to avoid using an error device set by another test case. - place = paddle.set_device(self.place) - rnn1 = GRU( - 16, 32, 2, time_major=self.time_major, direction=self.direction - ) - - mp = paddle.static.Program() - sp = paddle.static.Program() - with ( - paddle.base.unique_name.guard(), - paddle.static.program_guard(mp, sp), - ): - rnn2 = paddle.nn.GRU( - 16, - 32, - 2, - time_major=self.time_major, - direction=self.direction, - ) - - exe = paddle.static.Executor(place) - scope = paddle.base.Scope() - with paddle.static.scope_guard(scope): - exe.run(sp) - convert_params_for_net_static(rnn1, rnn2, place) - - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - - self.place = place - self.executor = exe - self.scope = scope - - def test_with_input_lengths(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - - x = np.random.randn(12, 4, 16) - if not self.time_major: - x = np.transpose(x, [1, 0, 2]) - sequence_length = np.array([12, 10, 9, 8], dtype=np.int64) - - y1, h1 = rnn1(x, sequence_length=sequence_length) - - with ( - paddle.base.unique_name.guard(), - paddle.static.program_guard(mp, sp), - ): - x_data = paddle.static.data( - "input", - [-1, -1, 16], - dtype=paddle.framework.get_default_dtype(), - ) - seq_len = paddle.static.data("seq_len", [-1], dtype="int64") - mask = paddle.static.nn.sequence_lod.sequence_mask( - seq_len, dtype=paddle.get_default_dtype() - ) - if self.time_major: - mask = paddle.transpose(mask, [1, 0]) - y, h = rnn2(x_data, sequence_length=seq_len) - mask = paddle.unsqueeze(mask, -1) - y = paddle.multiply(y, mask) - - feed_dict = {x_data.name: x, seq_len.name: sequence_length} - - with paddle.static.scope_guard(scope): - y2, h2 = exe.run(mp, feed=feed_dict, fetch_list=[y, h]) - - np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) - np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) - - def runTest(self): - self.test_with_input_lengths() - - -class TestLSTM(unittest.TestCase): - def __init__(self, time_major=True, direction="forward", place="cpu"): - super().__init__("runTest") - self.time_major = time_major - self.direction = direction - self.num_directions = 2 if direction in bidirectional_list else 1 - self.place = place - - def setUp(self): - # Since `set_device` is global, set `set_device` in `setUp` rather than - # `__init__` to avoid using an error device set by another test case. - place = paddle.set_device(self.place) - rnn1 = LSTM( - 16, 32, 2, time_major=self.time_major, direction=self.direction - ) - - mp = paddle.static.Program() - sp = paddle.static.Program() - with ( - paddle.base.unique_name.guard(), - paddle.static.program_guard(mp, sp), - ): - rnn2 = paddle.nn.LSTM( - 16, - 32, - 2, - time_major=self.time_major, - direction=self.direction, - ) - - exe = paddle.static.Executor(place) - scope = paddle.base.Scope() - with paddle.static.scope_guard(scope): - exe.run(sp) - convert_params_for_net_static(rnn1, rnn2, place) - - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - - self.place = place - self.executor = exe - self.scope = scope - - def test_with_input_lengths(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - - x = np.random.randn(12, 4, 16) - if not self.time_major: - x = np.transpose(x, [1, 0, 2]) - sequence_length = np.array([12, 10, 9, 8], dtype=np.int64) - - y1, (h1, c1) = rnn1(x, sequence_length=sequence_length) - - with ( - paddle.base.unique_name.guard(), - paddle.static.program_guard(mp, sp), - ): - x_data = paddle.static.data( - "input", - [-1, -1, 16], - dtype=paddle.framework.get_default_dtype(), - ) - seq_len = paddle.static.data("seq_len", [-1], dtype="int64") - mask = paddle.static.nn.sequence_lod.sequence_mask( - seq_len, dtype=paddle.get_default_dtype() - ) - if self.time_major: - mask = paddle.transpose(mask, [1, 0]) - y, (h, c) = rnn2(x_data, sequence_length=seq_len) - mask = paddle.unsqueeze(mask, -1) - y = paddle.multiply(y, mask) - - feed_dict = {x_data.name: x, seq_len.name: sequence_length} - - with paddle.static.scope_guard(scope): - y2, h2, c2 = exe.run(mp, feed=feed_dict, fetch_list=[y, h, c]) - - np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) - np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) - np.testing.assert_allclose(c1, c2, atol=1e-8, rtol=1e-5) - - def runTest(self): - self.test_with_input_lengths() - - -class TestLSTMWithProjSize(TestLSTM): - def setUp(self): - # Since `set_device` is global, set `set_device` in `setUp` rather than - # `__init__` to avoid using an error device set by another test case. - place = paddle.set_device(self.place) - rnn1 = LSTM( - 16, - 32, - 2, - time_major=self.time_major, - direction=self.direction, - proj_size=8, - ) - - mp = paddle.static.Program() - sp = paddle.static.Program() - with ( - paddle.base.unique_name.guard(), - paddle.static.program_guard(mp, sp), - ): - rnn2 = paddle.nn.LSTM( - 16, - 32, - 2, - time_major=self.time_major, - direction=self.direction, - proj_size=8, - ) - - exe = paddle.static.Executor(place) - scope = paddle.base.Scope() - with paddle.static.scope_guard(scope): - exe.run(sp) - convert_params_for_net_static(rnn1, rnn2, place) - - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - self.proj_size = 8 - - self.place = place - self.executor = exe - self.scope = scope - - -def load_tests(loader, tests, pattern): - suite = unittest.TestSuite() - devices = ["cpu", "gpu"] if paddle.base.is_compiled_with_cuda() else ["cpu"] - for direction in ["forward", "bidirectional", "bidirect"]: - for time_major in [True, False]: - for device in devices: - for test_class in [ - TestSimpleRNN, - TestLSTM, - TestGRU, - TestLSTMWithProjSize, - ]: - suite.addTest(test_class(time_major, direction, device)) - if test_class == TestSimpleRNN: - suite.addTest( - test_class( - time_major, direction, device, mode="RNN_RELU" - ) - ) - return suite - - -if __name__ == "__main__": - unittest.main() diff --git a/test/legacy_test/test_activation_op.py b/test/legacy_test/test_activation_op.py index 3edee8bdaff6f1..57cbf22edb5682 100644 --- a/test/legacy_test/test_activation_op.py +++ b/test/legacy_test/test_activation_op.py @@ -444,7 +444,7 @@ def test_check_grad(self): ['X'], 'Out', max_relative_error=0.01, - check_prim=True, + check_prim=False, check_pir=True, check_prim_pir=True, check_pir_onednn=self.check_pir_onednn, diff --git a/test/quantization/test_imperative_out_scale.py b/test/quantization/test_imperative_out_scale.py index 03aa58d1addb5c..8707fb1601ac31 100644 --- a/test/quantization/test_imperative_out_scale.py +++ b/test/quantization/test_imperative_out_scale.py @@ -187,16 +187,6 @@ def test_out_scale_acc(self): loss_list = train_lenet(lenet, reader, adam) lenet.eval() - imperative_out_scale.save_quantized_model( - layer=lenet, - path=self.save_path, - input_spec=[ - paddle.static.InputSpec( - shape=[None, 1, 28, 28], dtype='float32' - ) - ], - ) - for i in range(len(loss_list) - 1): self.assertTrue( loss_list[i] > loss_list[i + 1], diff --git a/test/quantization/test_imperative_qat.py b/test/quantization/test_imperative_qat.py index 2c6857bf248c3f..7e78cd55d803e4 100644 --- a/test/quantization/test_imperative_qat.py +++ b/test/quantization/test_imperative_qat.py @@ -15,7 +15,6 @@ import logging import os import sys -import tempfile import unittest import numpy as np @@ -196,47 +195,6 @@ def test_qat(self): fp32_out = lenet(test_img) fp32_acc = paddle.metric.accuracy(fp32_out, label).numpy() - with tempfile.TemporaryDirectory(prefix="qat_save_path_") as tmpdir: - # save inference quantized model - imperative_qat.save_quantized_model( - layer=lenet, - path=os.path.join(tmpdir, "lenet"), - input_spec=[ - paddle.static.InputSpec( - shape=[None, 1, 28, 28], dtype='float32' - ) - ], - ) - print(f'Quantized model saved in {tmpdir}') - - if core.is_compiled_with_cuda(): - place = core.CUDAPlace(0) - else: - place = core.CPUPlace() - exe = paddle.static.Executor(place) - with paddle.pir_utils.OldIrGuard(): - [ - inference_program, - feed_target_names, - fetch_targets, - ] = paddle.static.load_inference_model( - tmpdir, - executor=exe, - model_filename="lenet" + INFER_MODEL_SUFFIX, - params_filename="lenet" + INFER_PARAMS_SUFFIX, - ) - (quant_out,) = exe.run( - inference_program, - feed={feed_target_names[0]: test_data}, - fetch_list=fetch_targets, - ) - paddle.disable_static() - quant_out = paddle.to_tensor(quant_out) - quant_acc = paddle.metric.accuracy(quant_out, label).numpy() - paddle.enable_static() - delta_value = fp32_acc - quant_acc - self.assertLessEqual(delta_value, self.diff_threshold) - class TestImperativeQatONNXFormat(unittest.TestCase): def set_vars(self): diff --git a/test/quantization/test_imperative_skip_op.py b/test/quantization/test_imperative_skip_op.py index 5957c7fde51750..6b82a40e6935d0 100644 --- a/test/quantization/test_imperative_skip_op.py +++ b/test/quantization/test_imperative_skip_op.py @@ -64,16 +64,6 @@ def test_out_scale_acc(self): path = "./save_dynamic_quant_infer_model/lenet" save_dir = "./save_dynamic_quant_infer_model" - qat.save_quantized_model( - layer=lenet, - path=path, - input_spec=[ - paddle.static.InputSpec( - shape=[None, 1, 28, 28], dtype='float32' - ) - ], - ) - paddle.enable_static() if core.is_compiled_with_cuda(): @@ -81,56 +71,6 @@ def test_out_scale_acc(self): else: place = core.CPUPlace() exe = paddle.static.Executor(place) - with paddle.pir_utils.OldIrGuard(): - [ - inference_program, - feed_target_names, - fetch_targets, - ] = paddle.static.load_inference_model( - save_dir, - executor=exe, - model_filename="lenet" + INFER_MODEL_SUFFIX, - params_filename="lenet" + INFER_PARAMS_SUFFIX, - ) - model_ops = inference_program.global_block().ops - - conv2d_count, matmul_count = 0, 0 - conv2d_skip_count, matmul_skip_count = 0, 0 - find_conv2d = False - find_matmul = False - for i, op in enumerate(model_ops): - if op.type == 'conv2d': - find_conv2d = True - if op.has_attr("skip_quant"): - conv2d_skip_count += 1 - if conv2d_count > 0: - self.assertTrue( - 'fake_quantize_dequantize' in model_ops[i - 1].type - ) - else: - self.assertTrue( - 'fake_quantize_dequantize' not in model_ops[i - 1].type - ) - conv2d_count += 1 - - if op.type == 'matmul': - find_matmul = True - if op.has_attr("skip_quant"): - matmul_skip_count += 1 - if matmul_count > 0: - self.assertTrue( - 'fake_quantize_dequantize' in model_ops[i - 1].type - ) - else: - self.assertTrue( - 'fake_quantize_dequantize' not in model_ops[i - 1].type - ) - matmul_count += 1 - - if find_conv2d: - self.assertTrue(conv2d_skip_count == 1) - if find_matmul: - self.assertTrue(matmul_skip_count == 1) if __name__ == '__main__': From 238651aae4a264b8457e1ca8cc74eca285f56bea Mon Sep 17 00:00:00 2001 From: zhengshengning Date: Mon, 25 Aug 2025 17:26:59 +0800 Subject: [PATCH 21/27] fix --- test/deprecated/legacy_test/CMakeLists.txt | 13 ------------- test/deprecated/rnn/CMakeLists.txt | 1 - tools/parallel_UT_rule.py | 2 -- tools/static_mode_white_list.py | 1 - 4 files changed, 17 deletions(-) diff --git a/test/deprecated/legacy_test/CMakeLists.txt b/test/deprecated/legacy_test/CMakeLists.txt index 095c3aa875f86e..6c5b25582d0cc3 100644 --- a/test/deprecated/legacy_test/CMakeLists.txt +++ b/test/deprecated/legacy_test/CMakeLists.txt @@ -387,7 +387,6 @@ function(parallel_bash_test_modules TARGET_NAME) endfunction() list(REMOVE_ITEM TEST_OPS test_feed_data_check_shape_type_deprecated) -list(REMOVE_ITEM TEST_OPS test_layers_deprecated) list(REMOVE_ITEM TEST_OPS test_basic_gru_api) list(REMOVE_ITEM TEST_OPS test_basic_gru_unit_op) list(REMOVE_ITEM TEST_OPS test_basic_lstm_api) @@ -572,22 +571,10 @@ py_test_modules( FLAGS_cudnn_batchnorm_spatial_persistent=1 FLAGS_conv_workspace_size_limit=1000) -if(NOT WIN32) - # TODO: fix these unittests failure on Windows - py_test_modules(test_layers_deprecated MODULES test_layers_deprecated ENVS - FLAGS_cudnn_deterministic=1) -endif() - set_tests_properties( test_dataloader_keep_order_deprecated test_dataloader_unkeep_order_deprecated PROPERTIES LABELS "RUN_TYPE=DIST") -if(NOT WIN32) - set_tests_properties(test_multiprocess_reader_exception_deprecated - PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") - set_tests_properties(test_layers_deprecated PROPERTIES TIMEOUT 120) -endif() - # setting timeout value as 15S set_tests_properties(test_imperative_lod_tensor_to_selected_rows_deprecated PROPERTIES TIMEOUT 200) diff --git a/test/deprecated/rnn/CMakeLists.txt b/test/deprecated/rnn/CMakeLists.txt index c1fcaeccc5dd46..da63dccaef87a8 100644 --- a/test/deprecated/rnn/CMakeLists.txt +++ b/test/deprecated/rnn/CMakeLists.txt @@ -8,6 +8,5 @@ foreach(TEST_OP ${TEST_OPS}) py_test_modules(${TEST_OP} MODULES ${TEST_OP}) endforeach() if(NOT WIN32) - set_tests_properties(test_rnn_nets_static_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_rnn_nets_deprecated PROPERTIES TIMEOUT 120) endif() diff --git a/tools/parallel_UT_rule.py b/tools/parallel_UT_rule.py index cf76c82a31b598..8d6df3cec6c68f 100755 --- a/tools/parallel_UT_rule.py +++ b/tools/parallel_UT_rule.py @@ -1050,7 +1050,6 @@ 'test_grid_sampler_op', 'test_initializer_nn', 'test_eager_tensor', - 'test_fuse_elewise_add_act_pass_deprecated', 'test_select_input_output_op', 'test_lstm_op', 'test_break_continue', @@ -2148,7 +2147,6 @@ 'test_trt_conv3d_op', 'test_tensorrt_engine', 'test_load_state_dict_from_old_format', - 'test_fuse_elewise_add_act_pass_deprecated', 'test_randint_op', 'test_standalone_controlflow', 'test_standalone_multiply_write', diff --git a/tools/static_mode_white_list.py b/tools/static_mode_white_list.py index 7b23b6cff60a90..6280c8f6b99768 100755 --- a/tools/static_mode_white_list.py +++ b/tools/static_mode_white_list.py @@ -502,7 +502,6 @@ 'test_squared_mat_sub_fuse_pass', 'test_transpose_flatten_concat_fuse_pass', 'test_detection_map_op', - 'test_fuse_elewise_add_act_pass_deprecated', 'test_fusion_seqexpand_concat_fc_op', 'test_match_matrix_tensor_op', 'test_matmul_op_with_head', From 4ef6baff9444d7702c643d2405efebb800a1f535 Mon Sep 17 00:00:00 2001 From: zhengshengning Date: Mon, 25 Aug 2025 20:51:57 +0800 Subject: [PATCH 22/27] fix --- test/sot/test_sot_dynamic_shape.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/sot/test_sot_dynamic_shape.py b/test/sot/test_sot_dynamic_shape.py index 0f5d80523f8e90..24c3f2a2962644 100644 --- a/test/sot/test_sot_dynamic_shape.py +++ b/test/sot/test_sot_dynamic_shape.py @@ -25,6 +25,7 @@ import paddle from paddle.jit.sot.psdb import check_no_breakgraph from paddle.jit.sot.utils import ( + ConditionalFallbackError, allow_dynamic_shape_guard, enable_0_size_fallback_guard, specialized_dim_numbers_guard, @@ -342,6 +343,11 @@ def test_dynamic_shape_constraint(self): 5, # hit 2 * (s0 + s1 - 2) <= 30 ) + with self.assertRaises(ConditionalFallbackError): + self.assert_results( + dynamic_shape_constraint, paddle.randn([0, 1, const_dim]) + ) + def test_mixed_dynamic_and_static(self): with ( allow_dynamic_shape_guard(True), From a20de6c267b30a43931ce065cc51e397d6523a2e Mon Sep 17 00:00:00 2001 From: zhengshengning Date: Mon, 25 Aug 2025 22:19:44 +0800 Subject: [PATCH 23/27] fix --- test/sot/test_sot_dynamic_shape.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/sot/test_sot_dynamic_shape.py b/test/sot/test_sot_dynamic_shape.py index 24c3f2a2962644..50a40c9b389fc6 100644 --- a/test/sot/test_sot_dynamic_shape.py +++ b/test/sot/test_sot_dynamic_shape.py @@ -249,6 +249,7 @@ def test_pad_dynamic_shape_fallback(self): ) for i in range(1, 5): self.assert_results(pad_func, paddle.randn([1, 3, 224, 224]), i) + self.assertEqual(ctx.translate_count, 1 if i == 1 else 2) def test_dynamic_shape_int_mul_float(self): with ( From c1996b5a94a85c2da5d2ead34cc7b35ddd1069fb Mon Sep 17 00:00:00 2001 From: zhengshengning Date: Tue, 26 Aug 2025 15:03:41 +0800 Subject: [PATCH 24/27] remove sum --- paddle/fluid/pybind/arg_pre_process.cc | 7 +------ paddle/fluid/pybind/arg_pre_process.h | 10 +--------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/paddle/fluid/pybind/arg_pre_process.cc b/paddle/fluid/pybind/arg_pre_process.cc index 2695ac82c7c83d..e0fdcf1fab21c3 100644 --- a/paddle/fluid/pybind/arg_pre_process.cc +++ b/paddle/fluid/pybind/arg_pre_process.cc @@ -26,11 +26,6 @@ #include "paddle/phi/common/data_type.h" #include "paddle/phi/core/enforce.h" namespace paddle { -namespace pybind { -void SumPreProcess(Tensor* x, IntArray* axis) {} -void SumPreProcess(Value* x, Value* axis) { - paddle::dialect::SetStopGradient(x); -} -} // namespace pybind +namespace pybind {} // namespace pybind } // namespace paddle diff --git a/paddle/fluid/pybind/arg_pre_process.h b/paddle/fluid/pybind/arg_pre_process.h index 0ae2e6496d15a0..bc034e394a61dc 100644 --- a/paddle/fluid/pybind/arg_pre_process.h +++ b/paddle/fluid/pybind/arg_pre_process.h @@ -20,14 +20,6 @@ namespace paddle { -namespace pybind { -using Tensor = paddle::Tensor; -using Value = pir::Value; -using IntArray = paddle::experimental::IntArray; -using IntVector = std::vector; - -void SumPreProcess(Tensor* x, IntArray* axis); -void SumPreProcess(Value* x, Value* axis); -} // namespace pybind +namespace pybind {} // namespace pybind } // namespace paddle From 30630e30e6acbade462ba86e663a144203f2063e Mon Sep 17 00:00:00 2001 From: zhengshengning Date: Tue, 26 Aug 2025 15:09:24 +0800 Subject: [PATCH 25/27] fix --- python/paddle/tensor/math.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 6f608cad669f5c..ce5c4f93ce8049 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -1654,6 +1654,7 @@ def sum( ) -> Tensor: """ Computes the sum of tensor elements over the given dimension. + Args: x (Tensor): An N-D Tensor, the data type is bool, bfloat16, float16, float32, float64, uint8, int8, int16, int32, int64, complex64, complex128. @@ -1669,13 +1670,17 @@ def sum( than the :attr:`x` unless :attr:`keepdim` is true, default value is False. name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + Returns: Tensor: Results of summation operation on the specified axis of input Tensor `x`, if `x.dtype='bool'`, `x.dtype='int32'`, it's data type is `'int64'`, otherwise it's data type is the same as `x`. + Examples: .. code-block:: python + >>> import paddle + >>> # x is a Tensor with following elements: >>> # [[0.2, 0.3, 0.5, 0.9] >>> # [0.1, 0.2, 0.6, 0.7]] @@ -1699,6 +1704,7 @@ def sum( Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True, [[1.89999998], [1.60000002]]) + >>> # y is a Tensor with shape [2, 2, 2] and elements as below: >>> # [[[1, 2], [3, 4]], >>> # [[5, 6], [7, 8]]] @@ -1713,6 +1719,7 @@ def sum( >>> out6 Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, [16, 20]) + >>> # x is a Tensor with following elements: >>> # [[True, True, True, True] >>> # [False, False, False, False]] From 1a11f229b1820e6bad64b495ba5ff57bb7ef3430 Mon Sep 17 00:00:00 2001 From: zhengshengning Date: Tue, 26 Aug 2025 16:32:21 +0800 Subject: [PATCH 26/27] add blank line --- python/paddle/_paddle_docs.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index e8baf762a39584..add728ca82fa96 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -88,6 +88,7 @@ def add_doc_and_signature(func_name: str, docstr: str, func_def: str) -> None: Examples: .. code-block:: python + >>> # type: ignore >>> import paddle >>> # data_x is a Tensor with shape [2, 4] with multiple minimum elements @@ -223,6 +224,7 @@ def amin( Examples: .. code-block:: python + >>> # type: ignore >>> import paddle >>> # data_x is a Tensor with shape [2, 4] with multiple maximum elements @@ -353,6 +355,7 @@ def amax( Examples: .. code-block:: python + >>> # type: ignore >>> import paddle >>> # x is a bool Tensor with following elements: @@ -451,6 +454,7 @@ def isfinite( Examples: .. code-block:: python + >>> # type: ignore >>> import paddle >>> x = paddle.to_tensor([float('-inf'), -2, 3.6, float('inf'), 0, float('-nan'), float('nan')]) @@ -486,6 +490,7 @@ def isinf( Examples: .. code-block:: python + >>> # type: ignore >>> import paddle @@ -536,6 +541,7 @@ def isnan( .. code-block:: python >>> import paddle + >>> # type: ignore >>> x = paddle.to_tensor([[1, 0], [1, 1]], dtype='int32') From 59c7cf5d631a3efe54a633e2ef925bf3ee1b6f7d Mon Sep 17 00:00:00 2001 From: cangtianhuang Date: Tue, 26 Aug 2025 17:52:37 +0800 Subject: [PATCH 27/27] fix docs --- python/paddle/_paddle_docs.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/python/paddle/_paddle_docs.py b/python/paddle/_paddle_docs.py index dccf0507f783de..0f68d67b4cc3b4 100644 --- a/python/paddle/_paddle_docs.py +++ b/python/paddle/_paddle_docs.py @@ -1027,9 +1027,16 @@ def bmm( .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor + .. note:: + Alias Support: + 1. The parameter name ``input`` can be used as an alias for ``x``. + 2. The parameter name ``other`` can be used as an alias for ``y``. + Args: x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, bfloat16, float16, float32, float64, complex64, complex128. + alias: ``input``. y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, bfloat16, float16, float32, float64, complex64, complex128. + alias: ``other``. out(Tensor|None, optional): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output. name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. @@ -1070,9 +1077,16 @@ def logical_and( .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor + .. note:: + Alias Support: + 1. The parameter name ``input`` can be used as an alias for ``x``. + 2. The parameter name ``other`` can be used as an alias for ``y``. + Args: x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, bfloat16, float16, float32, float64, complex64, complex128. + alias: ``input``. y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, bfloat16, float16, float32, float64, complex64, complex128. + alias: ``other``. out(Tensor|None, optional): The ``Variable`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output. name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. @@ -1114,8 +1128,13 @@ def logical_or( .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor + .. note:: + Alias Support: + 1. The parameter name ``input`` can be used as an alias for ``x``. + Args: x(Tensor): Operand of logical_not operator. Must be a Tensor of type bool, int8, int16, in32, in64, bfloat16, float16, float32, or float64, complex64, complex128. + alias: ``input``. out(Tensor|None): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor` will be created to save the output. name(str|None, optional): The default value is None. Normally there is no need for users to set this property. For more information, please refer to :ref:`api_guide_Name`. @@ -1155,9 +1174,16 @@ def logical_not( .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor + .. note:: + Alias Support: + 1. The parameter name ``input`` can be used as an alias for ``x``. + 2. The parameter name ``other`` can be used as an alias for ``y``. + Args: x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, bfloat16, float16, float32, float64, complex64, complex128. + alias: ``input``. y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, bfloat16, float16, float32, float64, complex64, complex128. + alias: ``other``. out(Tensor|None, optional): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output. name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.