diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index f06fb612ee4838462a1698b65c97a9b37c4d5ddc..b92b26f64368d3f2601cafc586cd8e92a1b2fdc6 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -2257,8 +2257,8 @@ void OneHotRawInferMeta(const MetaTensor& x, auto x_dims = x.dims(); PADDLE_ENFORCE_GE( x_dims.size(), - 1, - phi::errors::InvalidArgument("Rank of Input(X) should be at least 1.")); + 0, + phi::errors::InvalidArgument("Rank of Input(X) should be at least 0.")); auto out_dims_vec = phi::vectorize(x_dims); out_dims_vec.push_back(depth.to()); auto out_dims = phi::make_ddim(out_dims_vec); @@ -2273,8 +2273,8 @@ void OneHotInferMeta(const MetaTensor& x, auto x_dims = x.dims(); PADDLE_ENFORCE_GE( x_dims.size(), - 1, - phi::errors::InvalidArgument("Rank of Input(X) should be at least 1.")); + 0, + phi::errors::InvalidArgument("Rank of Input(X) should be at least 0.")); int depth = depth_t.to(); auto out_dims_vec = phi::vectorize(x_dims); 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 710480bfd9443c3ad1f296c193156715e4b98ad4..d4ee3e5019489d541af58f23f459822df764537a 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -1421,6 +1421,24 @@ class TestNoBackwardAPI(unittest.TestCase): out = paddle.zeros(self.shape) self.assertEqual(out.shape, [2, 3, 4]) + def test_embedding(self): + ids = paddle.full(shape=[], fill_value=1, dtype='int64') + w0 = paddle.arange(3, 9).reshape((3, 2)).astype(paddle.float32) + w = paddle.to_tensor(w0, stop_gradient=False) + emb = paddle.nn.functional.embedding( + x=ids, weight=w, sparse=True, name="embedding" + ) + self.assertEqual(emb.shape, [2]) + res = [5.0, 6.0] + for i in range(len(res)): + self.assertEqual(emb.numpy()[i], res[i]) + + def test_one_hot_label(self): + label = paddle.full(shape=[], fill_value=2, dtype='int64') + one_hot_label = paddle.nn.functional.one_hot(label, num_classes=4) + self.assertEqual(one_hot_label.shape, [4]) + self.assertEqual(one_hot_label.numpy()[2], 1) + class TestNoBackwardAPIStatic(unittest.TestCase): def setUp(self): @@ -1590,6 +1608,39 @@ class TestNoBackwardAPIStatic(unittest.TestCase): self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, (2, 3, 4)) + def test_embedding(self): + ids = paddle.full(shape=[], fill_value=1, dtype='int64') + w0 = paddle.arange(3, 9).reshape((3, 2)).astype(paddle.float32) + w = paddle.to_tensor(w0, stop_gradient=False) + emb = paddle.nn.functional.embedding( + x=ids, weight=w, sparse=True, name="embedding" + ) + + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[emb]) + self.assertEqual(res[0].shape, (2,)) + result = [5.0, 6.0] + for i in range(len(res)): + self.assertEqual(res[0][i], result[i]) + + def test_static_embedding(self): + ids = paddle.full(shape=[], fill_value=1, dtype='int64') + emb = paddle.static.nn.embedding(ids, (20, 3)) + prog = paddle.static.default_main_program() + self.exe.run(paddle.fluid.default_startup_program()) + res = self.exe.run(prog, fetch_list=[emb]) + self.assertEqual(res[0].shape, (3,)) + + def test_one_hot_label(self): + label = paddle.full(shape=[], fill_value=2, dtype='int64') + one_hot_label = paddle.nn.functional.one_hot(label, num_classes=4) + prog = paddle.static.default_main_program() + self.exe.run(paddle.fluid.default_startup_program()) + res = self.exe.run(prog, fetch_list=[one_hot_label]) + + self.assertEqual(res[0].shape, (4,)) + self.assertEqual(res[0][2], 1) + if __name__ == "__main__": unittest.main() diff --git a/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py index 95d56bb902716214c7c1f9612a3a7deaa89417c2..e7ed57cad49a07e050e774e62b088714daf54b74 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py @@ -833,6 +833,24 @@ class TestNoBackwardAPI(unittest.TestCase): out = paddle.zeros(self.shape) self.assertEqual(out.shape, [2, 3, 4]) + def test_embedding(self): + ids = paddle.full(shape=[], fill_value=1, dtype='int64') + w0 = paddle.arange(3, 9).reshape((3, 2)).astype(paddle.float32) + w = paddle.to_tensor(w0, stop_gradient=False) + emb = paddle.nn.functional.embedding( + x=ids, weight=w, sparse=True, name="embedding" + ) + self.assertEqual(emb.shape, [2]) + res = [5.0, 6.0] + for i in range(len(res)): + self.assertEqual(emb.numpy()[i], res[i]) + + def test_one_hot_label(self): + label = paddle.full(shape=[], fill_value=2, dtype='int64') + one_hot_label = paddle.nn.functional.one_hot(label, num_classes=4) + self.assertEqual(one_hot_label.shape, [4]) + self.assertEqual(one_hot_label.numpy()[2], 1) + if __name__ == "__main__": unittest.main()