未验证 提交 377b3465 编写于 作者: A Aurelius84 提交者: GitHub

[Yaml] Add ones and zeros for C++ API (#44839)

上级 71cbbdac
...@@ -1785,6 +1785,11 @@ ...@@ -1785,6 +1785,11 @@
kernel : kernel :
func : one_hot 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 - api : ones_like
args : (Tensor x, DataType dtype=DataType::UNDEFINED, Place place={}) args : (Tensor x, DataType dtype=DataType::UNDEFINED, Place place={})
output : Tensor output : Tensor
...@@ -2728,6 +2733,11 @@ ...@@ -2728,6 +2733,11 @@
optional : gt_score optional : gt_score
backward : yolov3_loss_grad 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 - api : zeros_like
args : (Tensor x, DataType dtype=DataType::UNDEFINED, Place place = {}) args : (Tensor x, DataType dtype=DataType::UNDEFINED, Place place = {})
output : Tensor output : Tensor
......
...@@ -18,8 +18,10 @@ import unittest ...@@ -18,8 +18,10 @@ import unittest
import numpy as np import numpy as np
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle import _C_ops
from paddle import ones_like from paddle import ones_like
from paddle.fluid import core, Program, program_guard from paddle.fluid import core, Program, program_guard
from paddle.fluid.framework import convert_np_dtype_to_dtype_
class TestOnesLikeAPIError(unittest.TestCase): class TestOnesLikeAPIError(unittest.TestCase):
...@@ -79,5 +81,22 @@ class TestOnesLikeImpeartive(unittest.TestCase): ...@@ -79,5 +81,22 @@ class TestOnesLikeImpeartive(unittest.TestCase):
paddle.enable_static() 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__": if __name__ == "__main__":
unittest.main() unittest.main()
...@@ -18,8 +18,10 @@ import numpy as np ...@@ -18,8 +18,10 @@ import numpy as np
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle import zeros_like from paddle import zeros_like
from paddle import _C_ops
from paddle.fluid import core, Program, program_guard from paddle.fluid import core, Program, program_guard
from paddle.fluid.framework import _test_eager_guard from paddle.fluid.framework import _test_eager_guard
from paddle.fluid.framework import convert_np_dtype_to_dtype_
class TestZerosLikeAPIError(unittest.TestCase): class TestZerosLikeAPIError(unittest.TestCase):
...@@ -86,5 +88,23 @@ class TestZerosLikeImpeartive(unittest.TestCase): ...@@ -86,5 +88,23 @@ class TestZerosLikeImpeartive(unittest.TestCase):
self.test_out() 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__'): if (__name__ == '__main__'):
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册