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] add where, atan2, median 0-Dim ut #49692

Merged
merged 5 commits into from
Jan 13, 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
1 change: 1 addition & 0 deletions paddle/phi/kernels/xpu/where_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void WhereKernel(const Context& ctx,

int ret = xpu::select(
ctx.x_context(), cond_data, x_data, y_data, out_data, cond_dims, x_dims);

PADDLE_ENFORCE_XDNN_SUCCESS(ret, "select");
}

Expand Down
70 changes: 70 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 @@ -86,6 +86,7 @@
paddle.lgamma,
paddle.poisson,
paddle.bernoulli,
paddle.median,
]

inplace_api_list = [
Expand Down Expand Up @@ -1071,6 +1072,36 @@ def test_allclose(self):
y = paddle.full([], 0.6)
self.assertFalse(paddle.allclose(x, y))

def test_where(self):
x1 = paddle.full([], 1)
x2 = paddle.full([], 2)
x1.stop_gradient = False
x2.stop_gradient = False
out = paddle.where(x1 > x2, x1, x2)
out.backward()
self.assertEqual(out.shape, [])
self.assertEqual(out.numpy(), 2)
self.assertEqual(out.grad.shape, [])
self.assertEqual(x1.grad.shape, [])
self.assertEqual(x2.grad.shape, [])
self.assertEqual(x1.grad.numpy(), 0)
self.assertEqual(x2.grad.numpy(), 1)

def test_atan2(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个api是二元支持广播的吗?支持的话可以直接放到binary_api_list里

Copy link
Contributor Author

@ronny1996 ronny1996 Jan 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

atan和where不支持广播

x1 = paddle.full([], 0)
x2 = paddle.full([], 2)
x1.stop_gradient = False
x2.stop_gradient = False
out = paddle.atan2(x1, x2)
out.backward()
self.assertEqual(out.shape, [])
self.assertEqual(out.numpy(), 0)
self.assertEqual(out.grad.shape, [])
self.assertEqual(x1.grad.shape, [])
self.assertEqual(x2.grad.shape, [])
self.assertEqual(x1.grad.numpy(), 0.5)
self.assertEqual(x2.grad.numpy(), 0)


class TestSundryAPIStatic(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -1532,6 +1563,45 @@ def test_repeat_interleave(self):
res = self.exe.run(prog, fetch_list=[out])
self.assertEqual(res[0].shape, (3,))

@prog_scope()
def test_where(self):
x1 = paddle.full([], 1, 'float32')
x2 = paddle.full([], 2, 'float32')
x1.stop_gradient = False
x2.stop_gradient = False
out = paddle.where(x1 > x2, x1, x2)
loss = paddle.mean(out)
paddle.static.append_backward(loss)

prog = paddle.static.default_main_program()
res = self.exe.run(
prog,
feed={},
fetch_list=[out, out.grad_name, x1.grad_name, x2.grad_name],
)

self.assertEqual(res[0].shape, ())
self.assertEqual(res[0], 2)
self.assertEqual(res[1].shape, ())
self.assertEqual(res[2].shape, ())
self.assertEqual(res[2], 0)
self.assertEqual(res[3].shape, ())
self.assertEqual(res[3], 1)

@prog_scope()
def test_atan2(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同广播问题

x1 = paddle.full([], 0, 'float32')
x2 = paddle.full([], 2, 'float32')
x1.stop_gradient = False
x2.stop_gradient = False
out = paddle.atan2(x1, x2)
paddle.static.append_backward(out)

prog = paddle.static.default_main_program()
res = self.exe.run(prog, feed={}, fetch_list=[out])

self.assertEqual(res[0].shape, ())


# 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
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
paddle.lgamma,
paddle.poisson,
paddle.bernoulli,
paddle.median,
]

inplace_api_list = [
Expand Down Expand Up @@ -780,6 +781,8 @@ def test_allclose(self):


# 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):
def setUp(self):
paddle.disable_static()
Expand Down Expand Up @@ -926,6 +929,13 @@ def test_one_hot_label(self):
self.assertEqual(one_hot_label.shape, [4])
self.assertEqual(one_hot_label.numpy()[2], 1)

def test_where(self):
x1 = paddle.full([], 1)
x2 = paddle.full([], 2)
out = paddle.where(x1 > x2, x1, x2)
self.assertEqual(out.shape, [])
self.assertEqual(out.numpy(), 2)


if __name__ == "__main__":
unittest.main()
4 changes: 4 additions & 0 deletions python/paddle/tensor/stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ def median(x, axis=None, keepdim=False, name=None):
"""
if not isinstance(x, Variable):
raise TypeError("In median, the input x should be a Tensor.")

if len(x.shape) == 0:
return x.clone()

is_flatten = axis is None
dims = len(x.shape)
if is_flatten:
Expand Down