diff --git a/paddle/fluid/operators/slice_op.cc b/paddle/fluid/operators/slice_op.cc index c6432d00e9de16b5635aa80f6dabb63c2b84f46e..7014bf923c3d6048f06a6129c1d444a10319cee2 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 8eebfc7caa79536f5fe8d74f739da9eb948bece8..c39a9694e18e57ca139d12bd49168f611553ac5e 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; + } } }