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 div 0 error of case20: paddle.min #50013

Merged
merged 7 commits into from
Feb 3, 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
31 changes: 29 additions & 2 deletions paddle/fluid/operators/reduce_ops/reduce_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,22 @@ void HandleLargeDim(const framework::ExecutionContext& context,

// transpose to 2D tensor whose shape is {unreduced, reduced}.
const int64_t unreduced = output->numel();
const int64_t reduced = shuffled_input.numel() / unreduced;
const int64_t input_numel = shuffled_input.numel();
// assume: 0 / 0 == 0, which allow process 0 dim tensor
const int64_t reduced = (unreduced != 0) ? (input_numel / unreduced) : 0;

PADDLE_ENFORCE_EQ(
unreduced * reduced,
input_numel,
phi::errors::InvalidArgument(
"Reducing failed in HandleLargeDim, when try to transpose (%d) "
"operands into 2D tensor with shape (%d, %d).",
input_numel,
unreduced,
reduced));

shuffled_input.Resize({unreduced, reduced});

DDim output_dim = output->dims();
output->Resize({unreduced});
paddle::operators::ReduceFunctor<DeviceContext, OutT, 2, 1, Functor>(
Expand All @@ -163,7 +177,20 @@ void HandleLargeDimGrad(const framework::ExecutionContext& context,
Functor functor,
const std::vector<int>& dims) {
const int64_t unreduced = out->numel();
const int64_t reduced = x->numel() / unreduced;
const int64_t x_numel = x->numel();
// assume: 0 / 0 == 0, which allow process 0 dim tensor
const int64_t reduced = (unreduced != 0) ? (x_numel / unreduced) : 0;

PADDLE_ENFORCE_EQ(
unreduced * reduced,
x_numel,
phi::errors::InvalidArgument(
"Reducing failed in HandleLargeDimGrad, when try to transpose (%d) "
"operands into 2D tensor with shape (%d, %d).",
x_numel,
unreduced,
reduced));

DDim out_dim(out->dims());
DDim x_dim(x->dims());
// transpose and reshape X
Expand Down
16 changes: 15 additions & 1 deletion paddle/phi/kernels/funcs/reduce_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -1228,8 +1228,22 @@ void HandleLargeDim(const DeviceContext& dev_ctx,

// transpose to 2D tensor whose shape is {unreduced, reduced}.
const int64_t unreduced = output->numel();
const int64_t reduced = shuffled_input.numel() / unreduced;
const int64_t input_numel = shuffled_input.numel();
// assume: 0 / 0 == 0, which allow process 0 dim tensor
const int64_t reduced = (unreduced != 0) ? (input_numel / unreduced) : 0;

PADDLE_ENFORCE_EQ(
unreduced * reduced,
input_numel,
phi::errors::InvalidArgument(
"Reducing failed in HandleLargeDim, when try to transpose (%d) "
"operands into 2D tensor with shape (%d, %d).",
input_numel,
unreduced,
reduced));

shuffled_input.ResizeAndAllocate({unreduced, reduced});

DDim output_dim = output->dims();
output->ResizeAndAllocate({unreduced});
ReduceFunctor<DeviceContext, OutT, 2, 1, Functor>(
Expand Down
15 changes: 14 additions & 1 deletion paddle/phi/kernels/funcs/reduce_grad_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,20 @@ void HandleLargeDimGrad(const Context& dev_ctx,
Functor functor,
const std::vector<int>& dims) {
const int64_t unreduced = out->numel();
const int64_t reduced = x->numel() / unreduced;
const int64_t x_numel = x->numel();
// assume: 0 / 0 == 0, which allow process 0 dim tensor
const int64_t reduced = (unreduced != 0) ? (x_numel / unreduced) : 0;

PADDLE_ENFORCE_EQ(
unreduced * reduced,
x_numel,
phi::errors::InvalidArgument(
"Reducing failed in HandleLargeDimGrad, when try to transpose (%d) "
"operands into 2D tensor with shape (%d, %d).",
x_numel,
unreduced,
reduced));

DDim out_dim(out->dims());
DDim x_dim(x->dims());
// transpose and reshape X
Expand Down
5 changes: 5 additions & 0 deletions paddle/phi/kernels/reduce_min_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ void MinKernel(const Context& dev_ctx,
bool keep_dim,
DenseTensor* out) {
bool reduce_all = recompute_reduce_all(x, dims);
PADDLE_ENFORCE_GT(
x.numel(),
0,
errors::InvalidArgument("Zero-size tensor to reduction operation minimum "
"which has no identity."));
MinRawKernel<T>(dev_ctx, x, dims, keep_dim, reduce_all, out);
}

Expand Down
14 changes: 14 additions & 0 deletions python/paddle/fluid/tests/unittests/test_min_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import paddle
import paddle.fluid.core as core
from paddle import fluid


class ApiMinTest(unittest.TestCase):
Expand Down Expand Up @@ -117,5 +118,18 @@ def init_data(self):
self.keepdim = True


class TestMinAPIWithEmptyTensor(unittest.TestCase):
def test_empty_tensor(self):
with fluid.dygraph.guard():
with self.assertRaises(ValueError):
data = np.array([], dtype=np.float32)
data = np.reshape(data, [0, 0, 0, 0, 0, 0, 0])
x = paddle.to_tensor(data, dtype='float64')
np_axis = np.array([0], dtype='int64')
tensor_axis = paddle.to_tensor(np_axis, dtype='int64')

out = paddle.min(x, tensor_axis)


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