Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ones op for API 2.0: remove the device and out parameters #25497

Merged
merged 20 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions python/paddle/fluid/tests/unittests/test_fill_constant_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,6 @@ def test_check_output(self):
self.check_output()


class TestFillConstantOp5(unittest.TestCase):
def test_errors(self):
with program_guard(Program()):
out_np = np.zeros(shape=(1), dtype='float32')
out = paddle.zeros(shape=[1], dtype="float32")
place = fluid.CPUPlace()
exe = fluid.Executor(place)
result = exe.run(fetch_list=[out])
self.assertEqual((result == out_np).all(), True)
with program_guard(Program()):
data = fluid.data(name="X", shape=[1], dtype="float32")
out = paddle.ones(shape=[1], out=data, dtype="float32")
place = fluid.CPUPlace()
exe = fluid.Executor(place)
result = exe.run(feed={"X": np.array(
[0.1], dtype="float32")},
fetch_list=[data, out])
self.assertEqual(result[0], result[1])


class TestFillConstantOpWithSelectedRows(unittest.TestCase):
def check_with_place(self, place):
scope = core.Scope()
Expand Down
47 changes: 28 additions & 19 deletions python/paddle/fluid/tests/unittests/test_ones_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,36 @@


class ApiOnesTest(unittest.TestCase):
def test_out(self):
with fluid.program_guard(fluid.Program()):
def test_paddle_ones(self):
with paddle.program_guard(paddle.Program()):
ones = paddle.ones(shape=[10], dtype="float64")
place = fluid.CPUPlace()
exe = fluid.Executor(place)
place = paddle.CPUPlace()
exe = paddle.Executor(place)
result, = exe.run(fetch_list=[ones])
expected_result = np.ones(10, dtype="float64")
self.assertEqual((result == expected_result).all(), True)

with fluid.program_guard(fluid.Program()):
with paddle.program_guard(paddle.Program()):
ones = paddle.ones(shape=[10], dtype="float64")
place = paddle.CPUPlace()
exe = paddle.Executor(place)
result, = exe.run(fetch_list=[ones])
expected_result = np.ones(10, dtype="float64")
self.assertEqual((result == expected_result).all(), True)

with paddle.program_guard(paddle.Program()):
ones = paddle.ones(shape=[10], dtype="int64")
place = fluid.CPUPlace()
exe = fluid.Executor(place)
place = paddle.CPUPlace()
exe = paddle.Executor(place)
result, = exe.run(fetch_list=[ones])
expected_result = np.ones(10, dtype="int64")
self.assertEqual((result == expected_result).all(), True)

with fluid.program_guard(fluid.Program()):
ones = paddle.ones(shape=[10], dtype="int64", device="cpu")
place = fluid.CPUPlace()
exe = fluid.Executor(place)
def test_fluid_ones(self):
with paddle.program_guard(paddle.Program()):
ones = fluid.layers.ones(shape=[10], dtype="int64")
place = paddle.CPUPlace()
exe = paddle.Executor(place)
result, = exe.run(fetch_list=[ones])
expected_result = np.ones(10, dtype="int64")
self.assertEqual((result == expected_result).all(), True)
Expand All @@ -55,25 +64,25 @@ def test_out(self):
class ApiOnesZerosError(unittest.TestCase):
def test_errors(self):
def test_error1():
with fluid.program_guard(fluid.Program()):
ones = paddle.ones(shape=10, dtype="int64", device="opu")
with paddle.program_guard(paddle.Program()):
ones = paddle.ones(shape=10, dtype="int64")

self.assertRaises(ValueError, test_error1)
self.assertRaises(TypeError, test_error1)

def test_error2():
with fluid.program_guard(fluid.Program()):
ones = paddle.ones(shape=10, dtype="int64", device="opu")
with paddle.program_guard(paddle.Program()):
ones = paddle.ones(shape=10)

self.assertRaises(ValueError, test_error2)
self.assertRaises(TypeError, test_error2)

def test_error3():
with fluid.program_guard(fluid.Program()):
with paddle.program_guard(paddle.Program()):
ones = fluid.layers.ones(shape=10, dtype="int64")

self.assertRaises(TypeError, test_error3)

def test_error4():
with fluid.program_guard(fluid.Program()):
with paddle.program_guard(paddle.Program()):
ones = fluid.layers.ones(shape=[10], dtype="int8")

self.assertRaises(TypeError, test_error4)
Expand Down
35 changes: 26 additions & 9 deletions python/paddle/fluid/tests/unittests/test_zeros_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,43 @@ def test_errors(self):

class ApiZerosTest(unittest.TestCase):
def test_out(self):
with paddle.program_guard(fluid.Program()):
with program_guard(Program()):
zeros = paddle.zeros(shape=[10], dtype="float64")
place = fluid.CPUPlace()
exe = fluid.Executor(place)
place = paddle.CPUPlace()
exe = paddle.Executor(place)
result, = exe.run(fetch_list=[zeros])
expected_result = np.zeros(10, dtype="float64")
self.assertEqual((result == expected_result).all(), True)

with paddle.program_guard(fluid.Program()):
with paddle.program_guard(Program()):
zeros = paddle.zeros(shape=[10], dtype="int64")
place = fluid.CPUPlace()
exe = fluid.Executor(place)
place = paddle.CPUPlace()
exe = paddle.Executor(place)
result, = exe.run(fetch_list=[zeros])
expected_result = np.zeros(10, dtype="int64")
self.assertEqual((result == expected_result).all(), True)

with paddle.program_guard(fluid.Program()):
with program_guard(Program()):
zeros = paddle.zeros(shape=[10], dtype="int64")
place = fluid.CPUPlace()
exe = fluid.Executor(place)
place = paddle.CPUPlace()
exe = paddle.Executor(place)
result, = exe.run(fetch_list=[zeros])
expected_result = np.zeros(10, dtype="int64")
self.assertEqual((result == expected_result).all(), True)

with program_guard(Program()):
out_np = np.zeros(shape=(1), dtype='float32')
out = paddle.zeros(shape=[1], dtype="float32")
place = paddle.CPUPlace()
exe = paddle.Executor(place)
result = exe.run(fetch_list=[out])
self.assertEqual((result == out_np).all(), True)

def test_fluid_out(self):
with program_guard(Program()):
zeros = fluid.layers.zeros(shape=[10], dtype="int64")
place = paddle.CPUPlace()
exe = paddle.Executor(place)
result, = exe.run(fetch_list=[zeros])
expected_result = np.zeros(10, dtype="int64")
self.assertEqual((result == expected_result).all(), True)
Expand Down
33 changes: 11 additions & 22 deletions python/paddle/tensor/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def full_like(x, fill_value, dtype=None, name=None):
return out


def ones(shape, dtype=None, out=None, device=None):
def ones(shape, dtype=None, name=None):
"""
:alias_main: paddle.ones
:alias: paddle.ones,paddle.tensor.ones,paddle.tensor.creation.ones
Expand All @@ -120,14 +120,10 @@ def ones(shape, dtype=None, out=None, device=None):

Args:
shape(tuple|list): Shape of output tensor.
wangchaochaohu marked this conversation as resolved.
Show resolved Hide resolved
dtype(np.dtype|core.VarDesc.VarType|str): Data type of output tensor, it supports
bool, float16, float32, float64, int32 and int64.
out(Variable, optional): Optional output which can be any created
Variable that meets the requirements to store the result of operation.
if out is None, a new Varibale will be create to store the result.
device(str, optional): Which device to run the operator. The :attr:`device` must be
None,'cpu', 'gpu'. If :attr:`device` is None, it will be choose the device that the user set in
the paddle program. Default value is False.
dtype(np.dtype|core.VarDesc.VarType|str, optional): Data type of output tensor, it supports
bool, float16, float32, float64, int32 and int64. Default: if None, the data type is 'float32'.
name(str, optional): The name of output variable, normally there is no need for user to set this this property.
Default value is None, the framework set the name of output variable.
wangchaochaohu marked this conversation as resolved.
Show resolved Hide resolved

Returns:
Variable: A tensor of data type :attr:`dtype` with shape :attr:`shape` and all elements set to 1.
Expand All @@ -136,21 +132,14 @@ def ones(shape, dtype=None, out=None, device=None):
.. code-block:: python

import paddle

paddle.enable_imperative()
data = paddle.ones(shape=[3, 2], dtype='float32') # [[1., 1.], [1., 1.], [1., 1.]]
data = paddle.ones(shape=[2, 2], dtype='float32', device='cpu') # [[1., 1.], [1., 1.]]
data = paddle.ones(shape=[2, 2], dtype='int32', name="ones") # [[1, 1], [1, 1]]
wangchaochaohu marked this conversation as resolved.
Show resolved Hide resolved
"""
check_dtype(dtype, 'create data type',
['bool', 'float16', 'float32', 'float64', 'int32', 'int64'],
'zeros')

if device is not None:
if device not in ['cpu', 'gpu']:
raise ValueError(
"The value of 'device' in zeros_op must be cpu or gpu, but received %s."
% (device))
with fluid.device_guard(device):
return fill_constant(value=1.0, shape=shape, dtype=dtype, out=out)
return fill_constant(value=1.0, shape=shape, dtype=dtype, out=out)
if dtype is None:
dtype = 'float32'
return fill_constant(value=1.0, shape=shape, dtype=dtype, name=name)


def ones_like(input, dtype=None, device=None, name=None):
Expand Down