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]Unsqueeze support 0d tensor #49862

Merged
merged 10 commits into from
Jan 18, 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
45 changes: 45 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 @@ -1345,6 +1345,23 @@ def test_maseked_select(self):
self.assertEqual(x.grad.shape, [])
self.assertEqual(x.grad.numpy(), 1)

def test_unsqueeze(self):
x1 = paddle.full([], 2)
x1.stop_gradient = False
x1.retain_grads()
out1 = paddle.unsqueeze(x1, axis=0)
out1.retain_grads()
out1.backward()
self.assertEqual(out1.shape, [1])
self.assertEqual(x1.grad.shape, [])

x2 = paddle.full([], 0, dtype='int32')
out2 = paddle.unsqueeze(x1, axis=x2)
out2.retain_grads()
out2.backward()
self.assertEqual(out2.shape, [1])
heliqi marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(x1.grad.shape, [])

def test_t(self):
x = paddle.full([], 2.0)
x.stop_gradient = False
Expand Down Expand Up @@ -2091,6 +2108,34 @@ def test_maseked_select(self):
self.assertEqual(res[3].shape, ())
self.assertEqual(res[3], 1)

@prog_scope()
def test_unsqueeze(self):
x1 = paddle.full([], 2)
out1 = paddle.unsqueeze(x1, axis=0)
x1.stop_gradient = False
paddle.static.append_backward(out1.sum())

x2 = paddle.full([], 3)
x3 = paddle.full([], 0, dtype='int32')
x2.stop_gradient = False
out2 = paddle.unsqueeze(x2, axis=x3)
paddle.static.append_backward(out2.sum())

prog = paddle.static.default_main_program()
res = self.exe.run(
prog,
fetch_list=[
out1,
out2,
x1.grad_name,
x2.grad_name,
],
)
self.assertEqual(res[0].shape, (1,))
self.assertEqual(res[1].shape, (1,))
self.assertEqual(res[2].shape, ())
self.assertEqual(res[3].shape, ())

@prog_scope()
def test_t(self):
x = paddle.full([], 2.0)
Expand Down
13 changes: 13 additions & 0 deletions python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,19 @@ def test_maseked_select(self):
self.assertEqual(x.grad.shape, [])
self.assertEqual(x.grad.numpy(), 1)

def test_unsqueeze(self):
x1 = paddle.full([], 2)
x1.stop_gradient = False
out1 = paddle.unsqueeze(x1, axis=0)
out1.backward()
self.assertEqual(out1.shape, [1])
self.assertEqual(x1.grad.shape, [])

x2 = paddle.full([], 0, dtype='int32')
out2 = paddle.unsqueeze(x1, axis=x2)
out2.backward()
self.assertEqual(out2.shape, [1])


# Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest.

Expand Down