From 377b3465116df91e02e12a649036ec5bea70b7a3 Mon Sep 17 00:00:00 2001 From: Aurelius84 Date: Wed, 3 Aug 2022 17:16:38 +0800 Subject: [PATCH] [Yaml] Add ones and zeros for C++ API (#44839) --- paddle/phi/api/yaml/legacy_api.yaml | 12 ++++++++++- .../fluid/tests/unittests/test_ones_like.py | 19 ++++++++++++++++++ .../tests/unittests/test_zeros_like_op.py | 20 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/paddle/phi/api/yaml/legacy_api.yaml b/paddle/phi/api/yaml/legacy_api.yaml index 4857603c08..018f20a608 100755 --- a/paddle/phi/api/yaml/legacy_api.yaml +++ b/paddle/phi/api/yaml/legacy_api.yaml @@ -319,7 +319,7 @@ func : AucInferMeta kernel : func : auc - optional : ins_tag_weight + optional : ins_tag_weight #average_accumulates - api : average_accumulates_ @@ -1785,6 +1785,11 @@ kernel : func : one_hot +- api : ones + args : (IntArray shape, DataType dtype=DataType::FLOAT32, Place place=CPUPlace()) + output : Tensor + invoke : full(shape, 1, dtype, place) + - api : ones_like args : (Tensor x, DataType dtype=DataType::UNDEFINED, Place place={}) output : Tensor @@ -2728,6 +2733,11 @@ optional : gt_score backward : yolov3_loss_grad +- api : zeros + args : (IntArray shape, DataType dtype=DataType::FLOAT32, Place place=CPUPlace()) + output : Tensor + invoke : full(shape, 0, dtype, place) + - api : zeros_like args : (Tensor x, DataType dtype=DataType::UNDEFINED, Place place = {}) output : Tensor diff --git a/python/paddle/fluid/tests/unittests/test_ones_like.py b/python/paddle/fluid/tests/unittests/test_ones_like.py index 31e28fe478..4c1394caae 100644 --- a/python/paddle/fluid/tests/unittests/test_ones_like.py +++ b/python/paddle/fluid/tests/unittests/test_ones_like.py @@ -18,8 +18,10 @@ import unittest import numpy as np import paddle import paddle.fluid as fluid +from paddle import _C_ops from paddle import ones_like from paddle.fluid import core, Program, program_guard +from paddle.fluid.framework import convert_np_dtype_to_dtype_ class TestOnesLikeAPIError(unittest.TestCase): @@ -79,5 +81,22 @@ class TestOnesLikeImpeartive(unittest.TestCase): paddle.enable_static() +class TestOnesAPI(unittest.TestCase): + + def test_api(self): + shape = [3, 4] + place = fluid.CUDAPlace( + 0) if core.is_compiled_with_cuda() else fluid.CPUPlace() + paddle.disable_static(place) + + for dtype in [np.float32, np.float64, np.int32, np.int64]: + out = _C_ops.final_state_ones(shape, + convert_np_dtype_to_dtype_(dtype), + place) + self.assertEqual((out.numpy() == np.ones(shape, dtype)).all(), True) + + paddle.enable_static() + + if __name__ == "__main__": unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_zeros_like_op.py b/python/paddle/fluid/tests/unittests/test_zeros_like_op.py index 13911dff01..fcd6bba051 100644 --- a/python/paddle/fluid/tests/unittests/test_zeros_like_op.py +++ b/python/paddle/fluid/tests/unittests/test_zeros_like_op.py @@ -18,8 +18,10 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle import zeros_like +from paddle import _C_ops from paddle.fluid import core, Program, program_guard from paddle.fluid.framework import _test_eager_guard +from paddle.fluid.framework import convert_np_dtype_to_dtype_ class TestZerosLikeAPIError(unittest.TestCase): @@ -86,5 +88,23 @@ class TestZerosLikeImpeartive(unittest.TestCase): self.test_out() +class TestZerosAPI(unittest.TestCase): + + def test_api(self): + shape = [3, 4] + place = fluid.CUDAPlace( + 0) if core.is_compiled_with_cuda() else fluid.CPUPlace() + paddle.disable_static(place) + + for dtype in [np.float32, np.float64, np.int32, np.int64]: + out = _C_ops.final_state_zeros(shape, + convert_np_dtype_to_dtype_(dtype), + place) + self.assertEqual((out.numpy() == np.zeros(shape, dtype)).all(), + True) + + paddle.enable_static() + + if (__name__ == '__main__'): unittest.main() -- GitLab