Skip to content

Commit 2092660

Browse files
Ones op for API 2.0: remove the device and out parameters (#25497)
1 parent 4a44ffd commit 2092660

File tree

5 files changed

+116
-88
lines changed

5 files changed

+116
-88
lines changed

python/paddle/fluid/layers/tensor.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,10 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None):
650650
Returns:
651651
Variable: Tensor which is created according to shape and dtype.
652652
653-
Raise:
653+
Raises:
654654
TypeError: The dtype must be one of bool, float16, float32, float64, int32 and int64
655655
and the data type of out Tensor must be the same as the dtype.
656+
TypeError: The shape must be one of list, tuple and Variable.
656657
657658
Examples:
658659
.. code-block:: python
@@ -665,7 +666,7 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None):
665666
666667
# attr shape is a list which contains Variable Tensor.
667668
positive_2 = fluid.layers.fill_constant([1], "int32", 2)
668-
data3 = fluid.layers.fill_constant(shape=[1, positive_2], dtype='float32', value=1.5) # data3=[1.5, 1.5]
669+
data3 = fluid.layers.fill_constant(shape=[1, positive_2], dtype='float32', value=1.5) # data3=[[1.5, 1.5]]
669670
670671
# attr shape is an Variable Tensor.
671672
shape = fluid.layers.fill_constant([2], "int32", 2) # shape=[2,2]
@@ -1424,6 +1425,12 @@ def linspace(start, stop, num, dtype=None, name=None):
14241425
the data shape of this tensor is :math:`[num]` . If the :attr:`num` is set 1, the output tensor just has \
14251426
the value with input :attr:`start`.
14261427
1428+
Raises:
1429+
TypeError: The dtype must be one of float32 and float64.
1430+
TypeError: The dtype of `start` and `stop` must be one of float32 and float64.
1431+
TypeError: The dtype of `num` must be one of int32 and int64.
1432+
1433+
14271434
Examples:
14281435
.. code-block:: python
14291436

python/paddle/fluid/tests/unittests/test_fill_constant_op.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,6 @@ def test_check_output(self):
8383
self.check_output()
8484

8585

86-
class TestFillConstantOp5(unittest.TestCase):
87-
def test_errors(self):
88-
with program_guard(Program()):
89-
out_np = np.zeros(shape=(1), dtype='float32')
90-
out = paddle.zeros(shape=[1], dtype="float32")
91-
place = fluid.CPUPlace()
92-
exe = fluid.Executor(place)
93-
result = exe.run(fetch_list=[out])
94-
self.assertEqual((result == out_np).all(), True)
95-
with program_guard(Program()):
96-
data = fluid.data(name="X", shape=[1], dtype="float32")
97-
out = paddle.ones(shape=[1], out=data, dtype="float32")
98-
place = fluid.CPUPlace()
99-
exe = fluid.Executor(place)
100-
result = exe.run(feed={"X": np.array(
101-
[0.1], dtype="float32")},
102-
fetch_list=[data, out])
103-
self.assertEqual(result[0], result[1])
104-
105-
10686
class TestFillConstantOpWithSelectedRows(unittest.TestCase):
10787
def check_with_place(self, place):
10888
scope = core.Scope()

python/paddle/fluid/tests/unittests/test_ones_op.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,36 @@
2626

2727

2828
class ApiOnesTest(unittest.TestCase):
29-
def test_out(self):
30-
with fluid.program_guard(fluid.Program()):
29+
def test_paddle_ones(self):
30+
with paddle.program_guard(paddle.Program()):
3131
ones = paddle.ones(shape=[10], dtype="float64")
32-
place = fluid.CPUPlace()
33-
exe = fluid.Executor(place)
32+
place = paddle.CPUPlace()
33+
exe = paddle.Executor(place)
3434
result, = exe.run(fetch_list=[ones])
3535
expected_result = np.ones(10, dtype="float64")
3636
self.assertEqual((result == expected_result).all(), True)
3737

38-
with fluid.program_guard(fluid.Program()):
38+
with paddle.program_guard(paddle.Program()):
39+
ones = paddle.ones(shape=[10], dtype="float64")
40+
place = paddle.CPUPlace()
41+
exe = paddle.Executor(place)
42+
result, = exe.run(fetch_list=[ones])
43+
expected_result = np.ones(10, dtype="float64")
44+
self.assertEqual((result == expected_result).all(), True)
45+
46+
with paddle.program_guard(paddle.Program()):
3947
ones = paddle.ones(shape=[10], dtype="int64")
40-
place = fluid.CPUPlace()
41-
exe = fluid.Executor(place)
48+
place = paddle.CPUPlace()
49+
exe = paddle.Executor(place)
4250
result, = exe.run(fetch_list=[ones])
4351
expected_result = np.ones(10, dtype="int64")
4452
self.assertEqual((result == expected_result).all(), True)
4553

46-
with fluid.program_guard(fluid.Program()):
47-
ones = paddle.ones(shape=[10], dtype="int64", device="cpu")
48-
place = fluid.CPUPlace()
49-
exe = fluid.Executor(place)
54+
def test_fluid_ones(self):
55+
with paddle.program_guard(paddle.Program()):
56+
ones = fluid.layers.ones(shape=[10], dtype="int64")
57+
place = paddle.CPUPlace()
58+
exe = paddle.Executor(place)
5059
result, = exe.run(fetch_list=[ones])
5160
expected_result = np.ones(10, dtype="int64")
5261
self.assertEqual((result == expected_result).all(), True)
@@ -55,25 +64,25 @@ def test_out(self):
5564
class ApiOnesZerosError(unittest.TestCase):
5665
def test_errors(self):
5766
def test_error1():
58-
with fluid.program_guard(fluid.Program()):
59-
ones = paddle.ones(shape=10, dtype="int64", device="opu")
67+
with paddle.program_guard(paddle.Program()):
68+
ones = paddle.ones(shape=10, dtype="int64")
6069

61-
self.assertRaises(ValueError, test_error1)
70+
self.assertRaises(TypeError, test_error1)
6271

6372
def test_error2():
64-
with fluid.program_guard(fluid.Program()):
65-
ones = paddle.ones(shape=10, dtype="int64", device="opu")
73+
with paddle.program_guard(paddle.Program()):
74+
ones = paddle.ones(shape=10)
6675

67-
self.assertRaises(ValueError, test_error2)
76+
self.assertRaises(TypeError, test_error2)
6877

6978
def test_error3():
70-
with fluid.program_guard(fluid.Program()):
79+
with paddle.program_guard(paddle.Program()):
7180
ones = fluid.layers.ones(shape=10, dtype="int64")
7281

7382
self.assertRaises(TypeError, test_error3)
7483

7584
def test_error4():
76-
with fluid.program_guard(fluid.Program()):
85+
with paddle.program_guard(paddle.Program()):
7786
ones = fluid.layers.ones(shape=[10], dtype="int8")
7887

7988
self.assertRaises(TypeError, test_error4)

python/paddle/fluid/tests/unittests/test_zeros_op.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,43 @@ def test_errors(self):
3636

3737
class ApiZerosTest(unittest.TestCase):
3838
def test_out(self):
39-
with paddle.program_guard(fluid.Program()):
39+
with program_guard(Program()):
4040
zeros = paddle.zeros(shape=[10], dtype="float64")
41-
place = fluid.CPUPlace()
42-
exe = fluid.Executor(place)
41+
place = paddle.CPUPlace()
42+
exe = paddle.Executor(place)
4343
result, = exe.run(fetch_list=[zeros])
4444
expected_result = np.zeros(10, dtype="float64")
4545
self.assertEqual((result == expected_result).all(), True)
4646

47-
with paddle.program_guard(fluid.Program()):
47+
with paddle.program_guard(Program()):
4848
zeros = paddle.zeros(shape=[10], dtype="int64")
49-
place = fluid.CPUPlace()
50-
exe = fluid.Executor(place)
49+
place = paddle.CPUPlace()
50+
exe = paddle.Executor(place)
5151
result, = exe.run(fetch_list=[zeros])
5252
expected_result = np.zeros(10, dtype="int64")
5353
self.assertEqual((result == expected_result).all(), True)
5454

55-
with paddle.program_guard(fluid.Program()):
55+
with program_guard(Program()):
5656
zeros = paddle.zeros(shape=[10], dtype="int64")
57-
place = fluid.CPUPlace()
58-
exe = fluid.Executor(place)
57+
place = paddle.CPUPlace()
58+
exe = paddle.Executor(place)
59+
result, = exe.run(fetch_list=[zeros])
60+
expected_result = np.zeros(10, dtype="int64")
61+
self.assertEqual((result == expected_result).all(), True)
62+
63+
with program_guard(Program()):
64+
out_np = np.zeros(shape=(1), dtype='float32')
65+
out = paddle.zeros(shape=[1], dtype="float32")
66+
place = paddle.CPUPlace()
67+
exe = paddle.Executor(place)
68+
result = exe.run(fetch_list=[out])
69+
self.assertEqual((result == out_np).all(), True)
70+
71+
def test_fluid_out(self):
72+
with program_guard(Program()):
73+
zeros = fluid.layers.zeros(shape=[10], dtype="int64")
74+
place = paddle.CPUPlace()
75+
exe = paddle.Executor(place)
5976
result, = exe.run(fetch_list=[zeros])
6077
expected_result = np.zeros(10, dtype="int64")
6178
self.assertEqual((result == expected_result).all(), True)

python/paddle/tensor/creation.py

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ def full_like(x, fill_value, dtype=None, name=None):
7575
Returns:
7676
out(Variable): The Tensor variable storing the output.
7777
78+
Raises:
79+
TypeError: The dtype must be one of bool, float16, float32, float64, int32, int64 and None.
80+
7881
Examples:
7982
.. code-block:: python
8083
@@ -84,7 +87,8 @@ def full_like(x, fill_value, dtype=None, name=None):
8487
paddle.enable_imperative() # Now we are in imperative mode
8588
input = paddle.full(shape=[2, 3], fill_value=0.0, dtype='float32', name='input')
8689
output = paddle.full_like(input, 2.0)
87-
#output result : [array([[2., 2., 2.], [2., 2., 2.]], dtype=float32)]
90+
# [[2. 2. 2.]
91+
# [2. 2. 2.]]
8892
"""
8993

9094
if dtype is None:
@@ -99,7 +103,7 @@ def full_like(x, fill_value, dtype=None, name=None):
99103
helper = LayerHelper("full_like", **locals())
100104
check_dtype(dtype, 'dtype',
101105
['bool', 'float16', 'float32', 'float64', 'int32', 'int64'],
102-
'full_like/zeros_like')
106+
'full_like')
103107
out = helper.create_variable_for_type_inference(dtype=dtype)
104108

105109
helper.append_op(
@@ -112,46 +116,52 @@ def full_like(x, fill_value, dtype=None, name=None):
112116
return out
113117

114118

115-
def ones(shape, dtype=None, out=None, device=None):
119+
def ones(shape, dtype=None, name=None):
116120
"""
117121
:alias_main: paddle.ones
118122
:alias: paddle.ones,paddle.tensor.ones,paddle.tensor.creation.ones
119123
120124
The OP creates a tensor of specified :attr:`shape` and :attr:`dtype`, and fills it with 1.
121125
122126
Args:
123-
shape(tuple|list): Shape of output tensor.
124-
dtype(np.dtype|core.VarDesc.VarType|str): Data type of output tensor, it supports
125-
bool, float16, float32, float64, int32 and int64.
126-
out(Variable, optional): Optional output which can be any created
127-
Variable that meets the requirements to store the result of operation.
128-
if out is None, a new Varibale will be create to store the result.
129-
device(str, optional): Which device to run the operator. The :attr:`device` must be
130-
None,'cpu', 'gpu'. If :attr:`device` is None, it will be choose the device that the user set in
131-
the paddle program. Default value is False.
132-
127+
shape(tuple|list|Variable): Shape of output tensor, the data type of shape is int32 or int64.
128+
dtype(np.dtype|core.VarDesc.VarType|str, optional): Data type of output tensor, it supports
129+
bool, float16, float32, float64, int32 and int64. Default: if None, the data type is 'float32'.
130+
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`
131+
133132
Returns:
134133
Variable: A tensor of data type :attr:`dtype` with shape :attr:`shape` and all elements set to 1.
135134
135+
Raises:
136+
TypeError: The dtype must be one of bool, float16, float32, float64, int32, int64 and None
137+
and the data type of out Tensor must be the same as the dtype.
138+
TypeError: The `shape` must be one of list, tuple and Variable.
139+
136140
Examples:
137141
.. code-block:: python
138142
139-
import paddle
140-
data = paddle.ones(shape=[3, 2], dtype='float32') # [[1., 1.], [1., 1.], [1., 1.]]
141-
data = paddle.ones(shape=[2, 2], dtype='float32', device='cpu') # [[1., 1.], [1., 1.]]
143+
import paddle
144+
paddle.enable_imperative()
145+
146+
#default dtype for ones OP
147+
data1 = paddle.ones(shape=[3, 2])
148+
# [[1. 1.]
149+
# [1. 1.]
150+
# [1. 1.]]
151+
152+
data2 = paddle.ones(shape=[2, 2], dtype='int32')
153+
# [[1 1]
154+
# [1 1]]
155+
156+
#shape is a Variable
157+
shape = paddle.fill_constant(shape=[2], dtype='int32', value=2)
158+
data3 = paddle.ones(shape=shape, dtype='int32')
159+
# [[1 1]
160+
# [1 1]]
142161
"""
143-
check_dtype(dtype, 'create data type',
144-
['bool', 'float16', 'float32', 'float64', 'int32', 'int64'],
145-
'zeros')
146-
147-
if device is not None:
148-
if device not in ['cpu', 'gpu']:
149-
raise ValueError(
150-
"The value of 'device' in zeros_op must be cpu or gpu, but received %s."
151-
% (device))
152-
with fluid.device_guard(device):
153-
return fill_constant(value=1.0, shape=shape, dtype=dtype, out=out)
154-
return fill_constant(value=1.0, shape=shape, dtype=dtype, out=out)
162+
if dtype is None:
163+
dtype = 'float32'
164+
return fill_constant(value=1.0, shape=shape, dtype=dtype, name=name)
155165

156166

157167
def ones_like(input, dtype=None, device=None, name=None):
@@ -366,31 +376,36 @@ def full(shape, fill_value, dtype=None, name=None):
366376
367377
Raises:
368378
TypeError: The `dtype` must be one of None, bool, float16, float32, float64, int32 and int64.
369-
TypeError: The `shape` must be one of Variable, list tuple.
379+
TypeError: The `shape` must be one of Variable, list and tuple.
370380
371381
Examples:
372382
.. code-block:: python
373383
374384
import paddle
375385
376386
paddle.enable_imperative() # Now we are in imperative mode
377-
data1 = paddle.full(shape=[2,1], fill_value=0, dtype='int64') # data1=[[0],[0]]
387+
data1 = paddle.full(shape=[2,1], fill_value=0, dtype='int64')
388+
#[[0]
389+
# [0]]
378390
379391
# attr shape is a list which contains Variable Tensor.
380392
positive_2 = paddle.fill_constant([1], "int32", 2)
381-
data3 = paddle.full(shape=[1, positive_2], dtype='float32', fill_value=1.5) # data3=[1.5, 1.5]
393+
data3 = paddle.full(shape=[1, positive_2], dtype='float32', fill_value=1.5)
394+
# [[1.5 1.5]]
382395
383396
# attr shape is an Variable Tensor.
384-
shape = paddle.fill_constant([2], "int32", 2) # shape=[2,2]
385-
data4 = paddle.full(shape=shape, dtype='bool', fill_value=True) # data4=[[True,True],[True,True]]
397+
shape = paddle.fill_constant([2], "int32", 2)
398+
data4 = paddle.full(shape=shape, dtype='bool', fill_value=True)
399+
# [[True True]
400+
# [True True]]
386401
387-
# attr value is an Variable Tensor.
388-
val = paddle.fill_constant([1], "float32", 2.0) # val=[2.0]
389-
data5 = paddle.full(shape=[2,1], fill_value=val, dtype='float32') #data5=[[2.0],[2.0]]
402+
# attr fill_value is an Variable Tensor.
403+
val = paddle.fill_constant([1], "float32", 2.0)
404+
data5 = paddle.full(shape=[2,1], fill_value=val, dtype='float32')
405+
# [[2.0]
406+
# [2.0]]
390407
"""
391408

392-
helper = LayerHelper("full", **locals())
393-
394409
if dtype is None:
395410
dtype = 'float32'
396411

0 commit comments

Comments
 (0)