From a2e0865723219d008f1d0d231bff6b733944297c Mon Sep 17 00:00:00 2001 From: Yiqun Liu Date: Thu, 19 Aug 2021 16:57:50 +0800 Subject: [PATCH] Add dimension check for inverse to avoid dividing by 0 error when input's shape is [0, 0, 0]. (#34996) --- paddle/fluid/operators/inverse_op.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/paddle/fluid/operators/inverse_op.cc b/paddle/fluid/operators/inverse_op.cc index e73e74ab856..8c1fd34ae87 100644 --- a/paddle/fluid/operators/inverse_op.cc +++ b/paddle/fluid/operators/inverse_op.cc @@ -35,6 +35,14 @@ class InverseOp : public framework::OperatorWithKernel { "The dimension of Input(Input) is expected to be no less than 2. " "But recieved: Input(Input)'s dimension = %d, shape = [%s].", input_rank, input_dims)); + for (int64_t i = 0; i < input_rank; ++i) { + PADDLE_ENFORCE_EQ( + (input_dims[i] == -1) || (input_dims[i] > 0), true, + platform::errors::InvalidArgument( + "Each dimension of input tensor is expected to be -1 or a " + "positive number, but recieved %d. Input's shape is [%s].", + input_dims[i], input_dims)); + } if (input_dims[input_rank - 2] > 0 && input_dims[input_rank - 1] > 0) { PADDLE_ENFORCE_EQ(input_dims[input_rank - 2], input_dims[input_rank - 1], platform::errors::InvalidArgument( -- GitLab