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

fix zero bug of case21: paddle.mode #51091

Merged
merged 6 commits into from
Mar 2, 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
6 changes: 6 additions & 0 deletions paddle/phi/kernels/cpu/mode_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ void ModeKernel(const Context& dev_ctx,
DenseTensor* out,
DenseTensor* indices) {
const auto& in_dims = x.dims();
for (int i = 0; i < in_dims.size(); i++) {
PADDLE_ENFORCE_LT(0,
in_dims[i],
errors::InvalidArgument(
"The dims of Input(X) should be greater than 0."));
}
auto out_dims = out->dims();
// axis < 0, cacluate the real axis
if (axis < 0) axis += in_dims.size();
Expand Down
6 changes: 6 additions & 0 deletions paddle/phi/kernels/gpu/mode_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ void ModeKernel(const Context& dev_ctx,
DenseTensor* indices) {
// get the input dims
const auto& in_dims = x.dims();
for (int i = 0; i < in_dims.size(); i++) {
PADDLE_ENFORCE_LT(0,
in_dims[i],
errors::InvalidArgument(
"The dims of Input(X) should be greater than 0."));
}
// calcluate the real axis
if (axis < 0) axis += in_dims.size();

Expand Down
12 changes: 12 additions & 0 deletions python/paddle/fluid/tests/unittests/test_mode_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,17 @@ def test_run_static(self):
np.testing.assert_allclose(paddle_result, expect_value, rtol=1e-05)


class TestModeZeroError(unittest.TestCase):
def test_errors(self):
with paddle.fluid.dygraph.guard():

def test_0_size():
array = np.array([], dtype=np.float32)
x = paddle.to_tensor(np.reshape(array, [0, 0]), dtype='float32')
paddle.mode(x, axis=0)

self.assertRaises(ValueError, test_0_size)


if __name__ == '__main__':
unittest.main()