From d614a2906cf6a3023f49a4afec0c31e3c722c357 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Sat, 21 Jan 2023 17:06:24 +0000 Subject: [PATCH 1/3] add axis check in UniqueRawInferMeta --- paddle/phi/infermeta/unary.cc | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 55e895c6622a62..d0bc2e4956da23 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -4623,9 +4623,7 @@ void UniqueRawInferMeta(const MetaTensor& x, } } else { int axis_value = axis[0]; - if (axis_value < 0) { - axis_value += x.dims().size(); - } + PADDLE_ENFORCE_LT( axis_value, x.dims().size(), @@ -4633,6 +4631,18 @@ void UniqueRawInferMeta(const MetaTensor& x, "the dimension size(%d) of x.", axis_value, x.dims().size())); + PADDLE_ENFORCE_GE(axis_value, + -x.dims().size(), + phi::errors::InvalidArgument( + "The axis(%d) should be greater than or equal to " + "-dimension size(%d) of x.", + axis_value, + -x.dims().size())); + + if (axis_value < 0) { + axis_value += x.dims().size(); + } + auto out_dims = x.dims(); out_dims[axis_value] = -1; out->set_dims(out_dims); From 0c5c59a870e02d23d6a4651bebe315d1ffc714e7 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Sun, 22 Jan 2023 10:27:39 +0000 Subject: [PATCH 2/3] add unittest for negative axis --- .../fluid/tests/unittests/test_unique.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_unique.py b/python/paddle/fluid/tests/unittests/test_unique.py index 9183c1bd676bbf..b3ae10a6c335ed 100644 --- a/python/paddle/fluid/tests/unittests/test_unique.py +++ b/python/paddle/fluid/tests/unittests/test_unique.py @@ -190,6 +190,32 @@ def init_config(self): } +class TestUniqueOpAxisNeg(TestUniqueOp): + def init_config(self): + self.inputs = {'X': np.random.random((6, 1, 8)).astype('float64')} + unique, indices, inverse, counts = np.unique( + self.inputs['X'], + return_index=True, + return_inverse=True, + return_counts=True, + axis=-1, + ) + self.attrs = { + 'dtype': int(core.VarDesc.VarType.INT32), + "return_index": True, + "return_inverse": True, + "return_counts": True, + "axis": [-1], + "is_sorted": True, + } + self.outputs = { + 'Out': unique, + 'Indices': indices, + "Index": inverse, + "Counts": counts, + } + + class TestUniqueOpAxis1(TestUniqueOp): def init_config(self): self.inputs = {'X': np.random.random((3, 8, 8)).astype('float64')} From 3b625d837a1a7d393cb45d13e7d3c53b5bf7e62a Mon Sep 17 00:00:00 2001 From: RedContritio Date: Sun, 22 Jan 2023 13:23:58 +0000 Subject: [PATCH 3/3] simplify check for unique --- paddle/phi/infermeta/unary.cc | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index d0bc2e4956da23..bd50777633472d 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -4623,6 +4623,9 @@ void UniqueRawInferMeta(const MetaTensor& x, } } else { int axis_value = axis[0]; + if (axis_value < 0) { + axis_value += x.dims().size(); + } PADDLE_ENFORCE_LT( axis_value, @@ -4631,17 +4634,13 @@ void UniqueRawInferMeta(const MetaTensor& x, "the dimension size(%d) of x.", axis_value, x.dims().size())); - PADDLE_ENFORCE_GE(axis_value, - -x.dims().size(), - phi::errors::InvalidArgument( - "The axis(%d) should be greater than or equal to " - "-dimension size(%d) of x.", - axis_value, - -x.dims().size())); - - if (axis_value < 0) { - axis_value += x.dims().size(); - } + PADDLE_ENFORCE_GE( + axis_value, + 0, + phi::errors::InvalidArgument( + "The axis(%d) + rank(x) (%d) should be greater than or equal to 0.", + axis_value, + -x.dims().size())); auto out_dims = x.dims(); out_dims[axis_value] = -1;