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

【Zero-Dim】Support Zero dim for embedding and one-hot #49562

Merged
merged 6 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2216,8 +2216,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<int>());
auto out_dims = phi::make_ddim(out_dims_vec);
Expand All @@ -2232,8 +2232,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<int>();
auto out_dims_vec = phi::vectorize(x_dims);
Expand Down
51 changes: 51 additions & 0 deletions python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,24 @@ def test_zeros_and_zeros_like(self):
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):
Expand Down Expand Up @@ -1213,6 +1231,39 @@ def test_zeros_and_zeros_like(self):
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,))
seemingwang marked this conversation as resolved.
Show resolved Hide resolved
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()
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,24 @@ def test_zeros_and_zeros_like(self):
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()