diff --git a/paddle/phi/kernels/stride/strided_slice_kernel.cc b/paddle/phi/kernels/stride/strided_slice_kernel.cc index 181232264f594591b7e21a33e09bec9ea4095cae..648251f416172d6c680503f3d1cda2b169a4595c 100644 --- a/paddle/phi/kernels/stride/strided_slice_kernel.cc +++ b/paddle/phi/kernels/stride/strided_slice_kernel.cc @@ -57,14 +57,17 @@ void StridedSliceRawStridedKernel(const Context& dev_ctx, } } } + + int64_t left = 0; + int64_t right = 0; + if (strides[i] < 0) { - starts[i] = starts[i] + 1; - ends[i] = ends[i] + 1; + left = std::max(static_cast(-1), ends[i]); + right = std::min(axis_size - 1, starts[i]); + } else { + left = std::max(static_cast(0), starts[i]); + right = std::min(axis_size, ends[i]); } - - int64_t left = - std::max(static_cast(0), std::min(starts[i], ends[i])); - int64_t right = std::min(axis_size, std::max(starts[i], ends[i])); int64_t step = std::abs(strides[i]); auto dim = (std::abs(right - left) + step - 1) / step;