From e0e76c88c907cca698af83f60dda8a29de3709b7 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Wed, 25 Jan 2023 19:09:26 +0000 Subject: [PATCH 1/3] add elements count check in atan2 --- paddle/phi/kernels/impl/atan2_kernel_impl.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/paddle/phi/kernels/impl/atan2_kernel_impl.h b/paddle/phi/kernels/impl/atan2_kernel_impl.h index 2cae914e2f615..b7799a777046f 100644 --- a/paddle/phi/kernels/impl/atan2_kernel_impl.h +++ b/paddle/phi/kernels/impl/atan2_kernel_impl.h @@ -77,6 +77,14 @@ void Atan2Kernel(const Context& ctx, auto x_data = x.data(); auto y_data = y.data(); + PADDLE_ENFORCE_LE( + numel, + y.numel(), + phi::errors::InvalidArgument("The count (%d) of elements of X shall not " + "greater than count (%d) of elements of Y.", + numel, + y.numel())); + auto* out_data = ctx.template Alloc::type>( out, size_t(x.numel() * sizeof(typename Atan2Out::type))); From 357ca730dba6d20154ad9d41b1c5645b8bd0621b Mon Sep 17 00:00:00 2001 From: RedContritio Date: Wed, 25 Jan 2023 20:35:22 +0000 Subject: [PATCH 2/3] add unittest and pre-check in inferMeta --- paddle/phi/infermeta/binary.cc | 11 +++++++++++ python/paddle/fluid/tests/unittests/test_atan2_op.py | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index 561938adca80a..56df8c83b9af6 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -142,6 +142,17 @@ void KLDivInferMeta(const MetaTensor& x, } void Atan2InferMeta(const MetaTensor& x, const MetaTensor& y, MetaTensor* out) { + auto x_dims = x.dims(); + auto y_dims = y.dims(); + + PADDLE_ENFORCE_LE( + x_dims[0], + y_dims[0], + phi::errors::InvalidArgument("The count (%d) of elements of X shall not " + "greater than count (%d) of elements of Y.", + x_dims[0], + y_dims[0])); + out->share_meta(x); if (x.dtype() == DataType::INT32 || x.dtype() == DataType::INT64 || y.dtype() == DataType::INT32 || y.dtype() == DataType::INT64) { diff --git a/python/paddle/fluid/tests/unittests/test_atan2_op.py b/python/paddle/fluid/tests/unittests/test_atan2_op.py index 77ad77e3252b8..6b62b25ac5d8a 100644 --- a/python/paddle/fluid/tests/unittests/test_atan2_op.py +++ b/python/paddle/fluid/tests/unittests/test_atan2_op.py @@ -130,6 +130,18 @@ def run(place): run(place) +class TestAtan2Error(unittest.TestCase): + def test_mismatch(self): + paddle.enable_static() + + def test_mismatch_numel(): + X = paddle.fluid.data('X', (1,), dtype=np.float64) + Y = paddle.fluid.data('Y', (0,), dtype=np.float64) + out = paddle.atan2(X, Y) + + self.assertRaises(ValueError, test_mismatch_numel) + + if __name__ == '__main__': paddle.enable_static() unittest.main() From 97e3812e7d2f15f7cf4639753adc498b8f639a52 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Thu, 26 Jan 2023 23:38:30 +0000 Subject: [PATCH 3/3] add dimension check --- paddle/phi/infermeta/binary.cc | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index 56df8c83b9af6..3ca56e0602c1d 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -145,13 +145,22 @@ void Atan2InferMeta(const MetaTensor& x, const MetaTensor& y, MetaTensor* out) { auto x_dims = x.dims(); auto y_dims = y.dims(); - PADDLE_ENFORCE_LE( - x_dims[0], - y_dims[0], - phi::errors::InvalidArgument("The count (%d) of elements of X shall not " - "greater than count (%d) of elements of Y.", - x_dims[0], - y_dims[0])); + PADDLE_ENFORCE_EQ( + x_dims.size(), + y_dims.size(), + phi::errors::InvalidArgument("The rank (%d) of X shall be same as " + "rank (%d) of Y.", + x_dims.size(), + y_dims.size())); + + if (x_dims.size() > 0) + PADDLE_ENFORCE_LE(x_dims[0], + y_dims[0], + phi::errors::InvalidArgument( + "The count (%d) of elements of X shall not " + "greater than count (%d) of elements of Y.", + x_dims[0], + y_dims[0])); out->share_meta(x); if (x.dtype() == DataType::INT32 || x.dtype() == DataType::INT64 ||