diff --git a/python/paddle/fluid/executor.py b/python/paddle/fluid/executor.py index ee7c4020225a72a26977f8b269156334038ecea9..da1521044c94d50c3fecbaf16f3d9739f0adbd25 100755 --- a/python/paddle/fluid/executor.py +++ b/python/paddle/fluid/executor.py @@ -580,7 +580,7 @@ def _as_lodtensor(data, place, dtype=None): else dtype ) if np.isscalar(data): - data = np.array([data]).astype(dtype) + data = np.array(data).astype(dtype) elif isinstance(data, (list, tuple)): data = np.array(data) if data.dtype == np.object_: diff --git a/python/paddle/fluid/tests/unittests/test_compare_op.py b/python/paddle/fluid/tests/unittests/test_compare_op.py index 9765ebda405b80d391460b6b6b0169ea3a01626d..0b8c4aa8eae4215545a3953fe96a05040e2f557a 100755 --- a/python/paddle/fluid/tests/unittests/test_compare_op.py +++ b/python/paddle/fluid/tests/unittests/test_compare_op.py @@ -96,7 +96,7 @@ def create_paddle_case(op_type, callback): paddle.enable_static() with program_guard(Program(), Program()): x = paddle.static.data(name='x', shape=[4], dtype='int64') - y = paddle.static.data(name='y', shape=[1], dtype='int64') + y = paddle.static.data(name='y', shape=[], dtype='int64') op = eval("paddle.%s" % (self.op_type)) out = op(x, y) exe = fluid.Executor(self.place) diff --git a/python/paddle/fluid/tests/unittests/test_data.py b/python/paddle/fluid/tests/unittests/test_data.py index 51f01ed8bef3303e5d397afe9222345a9b7f656e..763637fd0edb7d1c0df5cfb87fc0c494e2864a59 100644 --- a/python/paddle/fluid/tests/unittests/test_data.py +++ b/python/paddle/fluid/tests/unittests/test_data.py @@ -31,6 +31,13 @@ class TestApiStaticDataError(unittest.TestCase): x3 = paddle.static.data(name="x3", shape=[2, 25]) self.assertEqual(x3.dtype, core.VarDesc.VarType.FP64) + def test_0D(self): + with program_guard(Program(), Program()): + x1 = paddle.static.data(name="x1_0D", shape=[]) + self.assertEqual(x1.dtype, core.VarDesc.VarType.FP32) + x2 = paddle.static.data(name="x2_0D", shape=(), dtype="bool") + self.assertEqual(x2.dtype, core.VarDesc.VarType.BOOL) + def test_error(self): with program_guard(Program(), Program()): diff --git a/python/paddle/fluid/tests/unittests/test_deg2rad.py b/python/paddle/fluid/tests/unittests/test_deg2rad.py index 25800356abcac6f34e65a84a8ab041ed9c18d1af..77dce311a939d6b4b03b180cd874553f693c7004 100644 --- a/python/paddle/fluid/tests/unittests/test_deg2rad.py +++ b/python/paddle/fluid/tests/unittests/test_deg2rad.py @@ -66,7 +66,7 @@ class TestDeg2radAPI(unittest.TestCase): class TestDeg2radAPI2(TestDeg2radAPI): # Test input data type is int def setUp(self): - self.x_np = 180 + self.x_np = [180] self.x_shape = [1] self.out_np = np.pi self.x_dtype = 'int64' diff --git a/python/paddle/fluid/tests/unittests/test_executor_feed_non_tensor.py b/python/paddle/fluid/tests/unittests/test_executor_feed_non_tensor.py index eaf8857f6acf48ccfb87c4a0be6ac6648e309c32..c1389ac2e1a67438c5e439ebc9666baf5d6cbb72 100644 --- a/python/paddle/fluid/tests/unittests/test_executor_feed_non_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_executor_feed_non_tensor.py @@ -22,7 +22,7 @@ from paddle import fluid class TestExecutor(unittest.TestCase): def net(self): - lr = paddle.static.data(name="lr", shape=[1], dtype='float32') + lr = paddle.static.data(name="lr", shape=[], dtype='float32') x = paddle.static.data(name="x", shape=[None, 1], dtype='float32') y = paddle.static.data(name="y", shape=[None, 1], dtype='float32') y_predict = paddle.static.nn.fc(x, size=1) diff --git a/python/paddle/fluid/tests/unittests/test_gcd.py b/python/paddle/fluid/tests/unittests/test_gcd.py index 2ed8438ee56835d035afc24d65fda1e2e7a0e97d..738c040ea98908e79304682a51581490d10efa81 100644 --- a/python/paddle/fluid/tests/unittests/test_gcd.py +++ b/python/paddle/fluid/tests/unittests/test_gcd.py @@ -25,8 +25,8 @@ paddle.enable_static() class TestGcdAPI(unittest.TestCase): def setUp(self): - self.x_np = 12 - self.y_np = 20 + self.x_np = [12] + self.y_np = [20] self.x_shape = [1] self.y_shape = [1] @@ -81,14 +81,14 @@ class TestGcdAPI3(TestGcdAPI): def setUp(self): self.x_np = 0 self.y_np = 20 - self.x_shape = [1] - self.y_shape = [1] + self.x_shape = [] + self.y_shape = [] class TestGcdAPI4(TestGcdAPI): def setUp(self): - self.x_np = 0 - self.y_np = 0 + self.x_np = [0] + self.y_np = [0] self.x_shape = [1] self.y_shape = [1] @@ -97,5 +97,5 @@ class TestGcdAPI5(TestGcdAPI): def setUp(self): self.x_np = 12 self.y_np = -20 - self.x_shape = [1] - self.y_shape = [1] + self.x_shape = [] + self.y_shape = [] diff --git a/python/paddle/fluid/tests/unittests/test_lcm.py b/python/paddle/fluid/tests/unittests/test_lcm.py index bb846a80d6ab27a2751321f37e9502308d0b7217..478853d8bab8ff4262513e72225777477b237bc7 100644 --- a/python/paddle/fluid/tests/unittests/test_lcm.py +++ b/python/paddle/fluid/tests/unittests/test_lcm.py @@ -27,8 +27,8 @@ class TestLcmAPI(unittest.TestCase): def setUp(self): self.x_np = 12 self.y_np = 20 - self.x_shape = [1] - self.y_shape = [1] + self.x_shape = [] + self.y_shape = [] def test_static_graph(self): startup_program = fluid.Program() @@ -81,14 +81,14 @@ class TestLcmAPI3(TestLcmAPI): def setUp(self): self.x_np = 0 self.y_np = 20 - self.x_shape = [1] - self.y_shape = [1] + self.x_shape = [] + self.y_shape = [] class TestLcmAPI4(TestLcmAPI): def setUp(self): - self.x_np = 0 - self.y_np = 0 + self.x_np = [0] + self.y_np = [0] self.x_shape = [1] self.y_shape = [1] @@ -97,5 +97,5 @@ class TestLcmAPI5(TestLcmAPI): def setUp(self): self.x_np = 12 self.y_np = -20 - self.x_shape = [1] - self.y_shape = [1] + self.x_shape = [] + self.y_shape = [] diff --git a/python/paddle/fluid/tests/unittests/test_put_along_axis_op.py b/python/paddle/fluid/tests/unittests/test_put_along_axis_op.py index a2085cc416a1953319c8a0b9c6bfeda2249e825b..2834cda0be6966a4ee18587c76835988078572a8 100644 --- a/python/paddle/fluid/tests/unittests/test_put_along_axis_op.py +++ b/python/paddle/fluid/tests/unittests/test_put_along_axis_op.py @@ -141,7 +141,7 @@ class TestPutAlongAxisAPI(unittest.TestCase): self.place = [paddle.CPUPlace()] self.axis = 0 self.value_np = 99.0 - self.value_shape = [1] + self.value_shape = [] self.x_feed = copy.deepcopy(self.x_np) if core.is_compiled_with_cuda(): self.place.append(paddle.CUDAPlace(0)) @@ -240,7 +240,7 @@ class TestPutAlongAxisAPICase2(TestPutAlongAxisAPI): self.place = [paddle.CPUPlace()] self.axis = 0 self.value_np = 99.0 - self.value_shape = [1] + self.value_shape = [] self.x_feed = copy.deepcopy(self.x_np) if core.is_compiled_with_cuda(): self.place.append(paddle.CUDAPlace(0)) @@ -258,7 +258,7 @@ class TestPutAlongAxisAPICase3(TestPutAlongAxisAPI): self.place = [paddle.CPUPlace()] self.axis = 0 self.value_np = 99.0 - self.value_shape = [1] + self.value_shape = [] self.x_feed = copy.deepcopy(self.x_np) if core.is_compiled_with_cuda(): self.place.append(paddle.CUDAPlace(0)) diff --git a/python/paddle/fluid/tests/unittests/test_rad2deg.py b/python/paddle/fluid/tests/unittests/test_rad2deg.py index 4013a6568e8226a4137f7a34ca9c1487879ff2fd..8629d7dcd37f287656acb48eecf8a4f259ffe358 100644 --- a/python/paddle/fluid/tests/unittests/test_rad2deg.py +++ b/python/paddle/fluid/tests/unittests/test_rad2deg.py @@ -65,7 +65,7 @@ class TestRad2degAPI(unittest.TestCase): class TestRad2degAPI2(TestRad2degAPI): def setUp(self): - self.x_np = np.pi / 2 + self.x_np = [np.pi / 2] self.x_shape = [1] self.out_np = 90 self.x_dtype = 'float32' @@ -83,7 +83,7 @@ class TestRad2degAPI2(TestRad2degAPI): class TestRad2degAPI3(TestRad2degAPI): # Test input data type is int def setUp(self): - self.x_np = 1 + self.x_np = [1] self.x_shape = [1] self.out_np = 180 / np.pi self.x_dtype = 'int64' diff --git a/python/paddle/fluid/tests/unittests/test_trapezoid.py b/python/paddle/fluid/tests/unittests/test_trapezoid.py index 226f40db91ab3d67cc72ad70dc7f16b138d8723a..de18e75512717c328d17ad32baf160041ba625c7 100644 --- a/python/paddle/fluid/tests/unittests/test_trapezoid.py +++ b/python/paddle/fluid/tests/unittests/test_trapezoid.py @@ -83,7 +83,7 @@ class TestTrapezoidAPI(unittest.TestCase): ) if self.dx is not None: dx = paddle.static.data( - name="dx", shape=[1], dtype='float32' + name="dx", shape=[], dtype='float32' ) exe = paddle.static.Executor(place) diff --git a/python/paddle/fluid/tests/unittests/test_unbind_op.py b/python/paddle/fluid/tests/unittests/test_unbind_op.py index 4d5c4f9beefae323b8db9fa01614fac208e37496..989eb43b0504d0e0ee58531dbd20816a404883f3 100644 --- a/python/paddle/fluid/tests/unittests/test_unbind_op.py +++ b/python/paddle/fluid/tests/unittests/test_unbind_op.py @@ -29,7 +29,7 @@ class TestUnbind(unittest.TestCase): x_1 = paddle.static.data(shape=[2, 3], dtype='float32', name='x_1') [out_0, out_1] = tensor.unbind(input=x_1, axis=0) input_1 = np.random.random([2, 3]).astype("float32") - axis = paddle.static.data(shape=[1], dtype='int32', name='axis') + axis = paddle.static.data(shape=[], dtype='int32', name='axis') exe = fluid.Executor(place=fluid.CPUPlace()) [res_1, res_2] = exe.run( @@ -87,7 +87,7 @@ class TestLayersUnbind(unittest.TestCase): x_1 = paddle.static.data(shape=[2, 3], dtype='float32', name='x_1') [out_0, out_1] = paddle.unbind(input=x_1, axis=0) input_1 = np.random.random([2, 3]).astype("float32") - axis = paddle.static.data(shape=[1], dtype='int32', name='axis') + axis = paddle.static.data(shape=[], dtype='int32', name='axis') exe = fluid.Executor(place=fluid.CPUPlace()) [res_1, res_2] = exe.run( diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 69ff0acbb3a00044023845289d48a57f34213b6f..ee905a911bb70127e8e43400346d5cfa3e1e8f8b 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -3528,6 +3528,39 @@ class TestSundryAPIStatic(unittest.TestCase): res = self.exe.run(prog, feed={"x": x_tensor}, fetch_list=[out]) self.assertEqual(res[0].shape, (3, 4, 2)) + @prog_scope() + def test_static_data(self): + x1 = paddle.static.data(name="x1", shape=[]) + prog = paddle.static.default_main_program() + res = self.exe.run( + prog, + feed={ + "x1": np.array(1.0, dtype='float32'), + }, + fetch_list=[ + x1.name, + ], + ) + self.assertEqual(res[0].shape, ()) + self.assertEqual(res[0], np.array(1.0)) + + x2 = paddle.static.data(name="x2", shape=[]) + x3 = paddle.static.data(name="x3", shape=[]) + y = x2 + x3 + prog = paddle.static.default_main_program() + res = self.exe.run( + prog, + feed={ + "x2": 100.5, + "x3": 200.5, + }, + fetch_list=[ + y.name, + ], + ) + self.assertEqual(res[0].shape, ()) + self.assertEqual(res[0], 301.0) + @prog_scope() def test_prelu(self): x1 = paddle.full([], 1.0, 'float32')