Skip to content

Commit

Permalink
Fix 堆栈溢出 (stack overflow) of case8: paddle.unique_consecutive (#49983)
Browse files Browse the repository at this point in the history
* support negative index in unique_consecutive

* add unittest

* add unittest
  • Loading branch information
RedContritio authored Feb 3, 2023
1 parent 5cfe164 commit 83077f6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions paddle/phi/kernels/cpu/unique_consecutive_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ void UniqueConsecutiveKernel(const Context& dev_ctx,
dev_ctx, x, out, return_inverse, return_counts, index, counts));
} else {
int valid_axis = axis[0];
if (valid_axis < 0) valid_axis += x.dims().size();
phi::VisitDataTypeTiny(
data_type,
UniqueConsecutiveDimFunctor<Context, T>(dev_ctx,
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/kernels/gpu/unique_consecutive_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void UniqueConsecutiveKernel(const Context& dev_ctx,
} else {
// 'axis' is required.
int valid_axis = axis[0];
if (valid_axis < 0) valid_axis += x.dims().size();
phi::VisitDataTypeTiny(
data_type,
UniqueConsecutiveDimsCUDAFunctor<Context, T>(dev_ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import paddle.fluid.core as core


def reference_unique_consecutive(X, return_inverse=False, return_counts=False):
def reference_unique_consecutive(
X, return_inverse=False, return_counts=False, axis=None
):
"""
Reference unique_consecutive implementation using python.
Args:
Expand Down Expand Up @@ -273,6 +275,47 @@ def test_dygraph(self):
)


class TestUniqueConsecutiveCase3API(unittest.TestCase):
def setUp(self):
self.places = [fluid.CPUPlace()]
if core.is_compiled_with_cuda():
self.places.append(fluid.CUDAPlace(0))

def check_static_result(self, place):
with fluid.program_guard(fluid.Program(), fluid.Program()):
paddle.enable_static()
input_x = fluid.data(
name="input_x",
shape=[
100,
],
dtype="float32",
)
result, inverse, counts = paddle.unique_consecutive(
input_x, return_inverse=True, return_counts=True, axis=-1
)
x_np = np.random.randint(20, size=100).astype("float32")
exe = fluid.Executor(place)
fetches = exe.run(
fluid.default_main_program(),
feed={"input_x": x_np},
fetch_list=[result],
)

def test_static(self):
for place in self.places:
self.check_static_result(place=place)

def test_dygraph(self):
for place in self.places:
with fluid.dygraph.guard(place):
input_x = np.random.randint(20, size=100).astype("float64")
x = paddle.to_tensor(input_x)
result, inverse, counts = paddle.unique_consecutive(
x, return_inverse=True, return_counts=True, axis=-1
)


class TestUniqueConsecutiveEmptyInput(OpTest):
"""empty input"""

Expand Down

0 comments on commit 83077f6

Please sign in to comment.