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][unittest] add repeat_interleave unittest for zero_dim #49596

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
23 changes: 23 additions & 0 deletions python/paddle/fluid/tests/unittests/test_repeat_interleave_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def test_check_grad_normal(self):

class TestIndexSelectAPI(unittest.TestCase):
def input_data(self):
self.data_zero_dim_x = np.array(0.5)
self.data_x = np.array(
[
[1.0, 2.0, 3.0, 4.0],
Expand Down Expand Up @@ -170,6 +171,19 @@ def test_repeat_interleave_api(self):
expect_out = np.repeat(self.data_x, repeats, axis=0)
np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05)

# case 3 zero_dim:
with program_guard(Program(), Program()):
x = fluid.layers.data(name='x', shape=[])
z = paddle.repeat_interleave(x, repeats)
exe = fluid.Executor(fluid.CPUPlace())
(res,) = exe.run(
feed={'x': self.data_zero_dim_x},
fetch_list=[z.name],
return_numpy=False,
)
expect_out = np.repeat(self.data_zero_dim_x, repeats)
np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05)

def test_dygraph_api(self):
self.input_data()
# case axis none
Expand Down Expand Up @@ -220,6 +234,15 @@ def test_dygraph_api(self):
expect_out = np.repeat(self.data_x, index, axis=0)
np.testing.assert_allclose(expect_out, np_z, rtol=1e-05)

# case 3 zero_dim:
with fluid.dygraph.guard():
x = fluid.dygraph.to_variable(self.data_zero_dim_x)
index = 2
z = paddle.repeat_interleave(x, index, None)
np_z = z.numpy()
expect_out = np.repeat(self.data_zero_dim_x, index, axis=None)
np.testing.assert_allclose(expect_out, np_z, rtol=1e-05)


if __name__ == '__main__':
unittest.main()
46 changes: 46 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 @@ -966,6 +966,34 @@ def test_argsort(self):
self.assertEqual(x1.grad.numpy(), 0)
self.assertEqual(x2.grad.numpy(), 0)

def test_repeat_interleave(self):
places = ['cpu']
if paddle.is_compiled_with_cuda():
places.append('gpu')
for place in places:
paddle.set_device(place)

x = paddle.randn(())
x.stop_gradient = False

out = paddle.repeat_interleave(x, 2, None)
out.backward()

# check shape of output
self.assertEqual(out.shape, [2])

# check grad shape
self.assertEqual(x.grad.shape, [])

repeats = paddle.to_tensor([3], dtype='int32')
out = paddle.repeat_interleave(x, repeats, None)

# check shape of output with 1D repeats
self.assertEqual(out.shape, [3])

# check grad shape with 1D repeats
self.assertEqual(x.grad.shape, [])


class TestSundryAPIStatic(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -1380,6 +1408,24 @@ def test_argsort(self):
self.assertEqual(res[0].shape, ())
self.assertEqual(res[1].shape, ())

@prog_scope()
def test_repeat_interleave(self):
x = paddle.full([], 1.0, 'float32')
out = paddle.repeat_interleave(x, 2, None)
paddle.static.append_backward(out)

prog = paddle.static.default_main_program()
res = self.exe.run(prog, fetch_list=[out])
self.assertEqual(res[0].shape, (2,))

repeats = paddle.to_tensor([3], dtype='int32')
out = paddle.repeat_interleave(x, repeats, None)
paddle.static.append_backward(out)

prog = paddle.static.default_main_program()
res = self.exe.run(prog, fetch_list=[out])
self.assertEqual(res[0].shape, (3,))


# Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest.
class TestNoBackwardAPI(unittest.TestCase):
Expand Down