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 12 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
2 changes: 1 addition & 1 deletion python/paddle/fluid/layers/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None):

# attr shape is a list which contains Variable Tensor.
positive_2 = fluid.layers.fill_constant([1], "int32", 2)
data3 = fluid.layers.fill_constant(shape=[1, positive_2], dtype='float32', value=1.5) # data3=[1.5, 1.5]
data3 = fluid.layers.fill_constant(shape=[1, positive_2], dtype='float32', value=1.5) # data3=[[1.5, 1.5]]

# attr shape is an Variable Tensor.
shape = fluid.layers.fill_constant([2], "int32", 2) # shape=[2,2]
Expand Down
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
71 changes: 38 additions & 33 deletions python/paddle/tensor/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def full_like(x, fill_value, dtype=None, name=None):
paddle.enable_imperative() # Now we are in imperative mode
input = paddle.full(shape=[2, 3], fill_value=0.0, dtype='float32', name='input')
output = paddle.full_like(input, 2.0)
#output result : [array([[2., 2., 2.], [2., 2., 2.]], dtype=float32)]
# [[2. 2. 2.]
# [2. 2. 2.]]
"""

if dtype is None:
Expand Down Expand Up @@ -111,46 +112,43 @@ 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

The OP creates a tensor of specified :attr:`shape` and :attr:`dtype`, and fills it with 1.

Args:
shape(tuple|list): Shape of output tensor.
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.

shape(tuple|list|Variable): Shape of output tensor, the data type of shape is int32 or int64.
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 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`

Returns:
Variable: A tensor of data type :attr:`dtype` with shape :attr:`shape` and all elements set to 1.

Examples:
.. code-block:: python

import paddle
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.]]

paddle.enable_imperative()
data1 = paddle.ones(shape=[3, 2])
# [[1. 1.]
# [1. 1.]
# [1. 1.]]
data2 = paddle.ones(shape=[2, 2], dtype='int32')
# [[1 1]
# [1 1]]
shape = paddle.fill_constant(shape=[2], dtype='int32', value=2)
data3 = paddle.ones(shape=shape, dtype='int32')
# [[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 Expand Up @@ -390,19 +388,26 @@ def full(shape, fill_value, dtype=None, name=None):
import paddle

paddle.enable_imperative() # Now we are in imperative mode
data1 = paddle.full(shape=[2,1], fill_value=0, dtype='int64') # data1=[[0],[0]]
data1 = paddle.full(shape=[2,1], fill_value=0, dtype='int64')
#[[0]
# [0]]

# attr shape is a list which contains Variable Tensor.
positive_2 = paddle.fill_constant([1], "int32", 2)
data3 = paddle.full(shape=[1, positive_2], dtype='float32', fill_value=1.5) # data3=[1.5, 1.5]
positive_3 = paddle.fill_constant([1], "int32", 2)
data3 = paddle.full(shape=[1, positive_2], dtype='float32', fill_value=1.5)
# [[1.5 1.5]]

# attr shape is an Variable Tensor.
shape = paddle.fill_constant([2], "int32", 2) # shape=[2,2]
data4 = paddle.full(shape=shape, dtype='bool', fill_value=True) # data4=[[True,True],[True,True]]
shape = paddle.fill_constant([2], "int32", 2)
data4 = paddle.full(shape=shape, dtype='bool', fill_value=True)
# [[True True]
# [True True]]

# attr value is an Variable Tensor.
val = paddle.fill_constant([1], "float32", 2.0) # val=[2.0]
data5 = paddle.full(shape=[2,1], fill_value=val, dtype='float32') #data5=[[2.0],[2.0]]
val = paddle.fill_constant([1], "float32", 2.0)
data5 = paddle.full(shape=[2,1], fill_value=val, dtype='float32') i
# [[2.0]
# [2.0]]
"""

helper = LayerHelper("full", **locals())
Expand Down