From 7e940b844aaf5302a742d7bbf68775c44e952927 Mon Sep 17 00:00:00 2001 From: zyfncg Date: Wed, 15 Jun 2022 16:10:25 +0800 Subject: [PATCH] [cherry-pick] Fix bug of strided_slice and slice (#43388, #43443) (#43432) * fix bug of strided_slice (#43388) * fix stride_slice bug * fix bug * fix bug of infer shape for slice (#43443) --- paddle/fluid/operators/slice_op.cc | 6 +++++- paddle/phi/kernels/funcs/strided_slice.h | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/operators/slice_op.cc b/paddle/fluid/operators/slice_op.cc index c6432d00e9d..7014bf923c3 100644 --- a/paddle/fluid/operators/slice_op.cc +++ b/paddle/fluid/operators/slice_op.cc @@ -101,7 +101,11 @@ class SliceOp : public framework::OperatorWithKernel { platform::errors::InvalidArgument( "The size of ends must be equal to the size of axes.")); } - + for (auto &axis : axes) { + if (axis < 0) { + axis = std::max(0, axis + in_dims.size()); + } + } phi::funcs::CheckAndUpdateSliceAttrs(in_dims, axes, &starts, &ends, nullptr, &infer_flags); diff --git a/paddle/phi/kernels/funcs/strided_slice.h b/paddle/phi/kernels/funcs/strided_slice.h index 8eebfc7caa7..c39a9694e18 100644 --- a/paddle/phi/kernels/funcs/strided_slice.h +++ b/paddle/phi/kernels/funcs/strided_slice.h @@ -74,10 +74,14 @@ static void StridedSliceOutDims(const std::vector& starts, if (start_index < 0) { start_index = start_index + axis_size; + start_index = std::max(start_index, 0); } if (end_index < 0) { if (!(end_index == -1 && stride_index < 0)) { // skip None stop condition end_index = end_index + axis_size; + if (end_index < 0) { + end_index = 0; + } } } -- GitLab