未验证 提交 50c43dd3 编写于 作者: R RedContritio 提交者: GitHub

Fix div 0 error of case20: paddle.min (#50013)

上级 97411214
......@@ -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>(
......@@ -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
......
......@@ -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>(
......
......@@ -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
......
......@@ -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);
}
......
......@@ -20,6 +20,7 @@ from test_sum_op import TestReduceOPTensorAxisBase
import paddle
import paddle.fluid.core as core
from paddle import fluid
class ApiMinTest(unittest.TestCase):
......@@ -117,5 +118,18 @@ class TestMinWithTensorAxis2(TestReduceOPTensorAxisBase):
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()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册