|
|
@ -19,9 +19,62 @@ limitations under the License. */
|
|
|
|
#include <vector>
|
|
|
|
#include <vector>
|
|
|
|
#include "paddle/fluid/framework/op_registry.h"
|
|
|
|
#include "paddle/fluid/framework/op_registry.h"
|
|
|
|
#include "paddle/fluid/operators/math/math_function.h"
|
|
|
|
#include "paddle/fluid/operators/math/math_function.h"
|
|
|
|
|
|
|
|
#include "paddle/fluid/operators/slice_op.h"
|
|
|
|
namespace paddle {
|
|
|
|
namespace paddle {
|
|
|
|
namespace operators {
|
|
|
|
namespace operators {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void StridedSliceOutDims(
|
|
|
|
|
|
|
|
const std::vector<int>& starts, const std::vector<int>& ends,
|
|
|
|
|
|
|
|
const std::vector<int>& strides, const std::vector<int>& axes,
|
|
|
|
|
|
|
|
const std::vector<int>& infer_flags, const framework::DDim in_dims,
|
|
|
|
|
|
|
|
int* out_dims_vector, const size_t size, bool infer_shape) {
|
|
|
|
|
|
|
|
for (int i = 0; i < in_dims.size(); i++) {
|
|
|
|
|
|
|
|
out_dims_vector[i] = in_dims[i];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int stride_index, start_index, end_index;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
|
|
|
|
int axes_index = axes[i];
|
|
|
|
|
|
|
|
if (infer_shape && infer_flags[i] == -1) {
|
|
|
|
|
|
|
|
out_dims_vector[axes_index] = -1;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_NE(strides[i], 0, "stride must not to be zero");
|
|
|
|
|
|
|
|
start_index = starts[i];
|
|
|
|
|
|
|
|
end_index = ends[i];
|
|
|
|
|
|
|
|
stride_index = strides[i];
|
|
|
|
|
|
|
|
int axis_size = in_dims[axes_index];
|
|
|
|
|
|
|
|
if (axis_size < 0) {
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (start_index < 0) {
|
|
|
|
|
|
|
|
start_index = start_index + axis_size;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end_index < 0) {
|
|
|
|
|
|
|
|
end_index = end_index + axis_size;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (stride_index < 0) {
|
|
|
|
|
|
|
|
start_index = start_index + 1;
|
|
|
|
|
|
|
|
end_index = end_index + 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool zero_dim_condition =
|
|
|
|
|
|
|
|
((stride_index < 0 && (start_index <= end_index)) ||
|
|
|
|
|
|
|
|
(stride_index > 0 && (start_index >= end_index)));
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_EQ(zero_dim_condition, false,
|
|
|
|
|
|
|
|
"starts and end must meet requirement in different "
|
|
|
|
|
|
|
|
"stride conditiont");
|
|
|
|
|
|
|
|
int left = std::max(0, std::min(start_index, end_index));
|
|
|
|
|
|
|
|
int right = std::min(axis_size, std::max(start_index, end_index));
|
|
|
|
|
|
|
|
int step = std::abs(stride_index);
|
|
|
|
|
|
|
|
auto out_dims_index = (std::abs(right - left) + step - 1) / step;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out_dims_vector[axes_index] = out_dims_index;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void StridedSliceFunctor(int* starts, int* ends, int* strides, int* axes,
|
|
|
|
static void StridedSliceFunctor(int* starts, int* ends, int* strides, int* axes,
|
|
|
|
int* reverse_axis, const framework::DDim dims,
|
|
|
|
int* reverse_axis, const framework::DDim dims,
|
|
|
|
const size_t size) {
|
|
|
|
const size_t size) {
|
|
|
@ -91,19 +144,52 @@ class StridedSliceKernel : public framework::OpKernel<T> {
|
|
|
|
*context.template device_context<DeviceContext>().eigen_device();
|
|
|
|
*context.template device_context<DeviceContext>().eigen_device();
|
|
|
|
auto in = context.Input<framework::Tensor>("Input");
|
|
|
|
auto in = context.Input<framework::Tensor>("Input");
|
|
|
|
auto out = context.Output<framework::Tensor>("Out");
|
|
|
|
auto out = context.Output<framework::Tensor>("Out");
|
|
|
|
auto out_dims = out->dims();
|
|
|
|
|
|
|
|
auto in_dims = in->dims();
|
|
|
|
auto in_dims = in->dims();
|
|
|
|
|
|
|
|
|
|
|
|
auto starts = context.Attr<std::vector<int>>("starts");
|
|
|
|
auto starts = context.Attr<std::vector<int>>("starts");
|
|
|
|
auto ends = context.Attr<std::vector<int>>("ends");
|
|
|
|
auto ends = context.Attr<std::vector<int>>("ends");
|
|
|
|
auto strides = context.Attr<std::vector<int>>("strides");
|
|
|
|
auto strides = context.Attr<std::vector<int>>("strides");
|
|
|
|
auto axes = context.Attr<std::vector<int>>("axes");
|
|
|
|
auto axes = context.Attr<std::vector<int>>("axes");
|
|
|
|
|
|
|
|
auto infer_flags = context.Attr<std::vector<int>>("infer_flags");
|
|
|
|
|
|
|
|
|
|
|
|
auto starts_indices = Eigen::DSizes<Eigen::DenseIndex, D>();
|
|
|
|
auto starts_indices = Eigen::DSizes<Eigen::DenseIndex, D>();
|
|
|
|
auto ends_indices = Eigen::DSizes<Eigen::DenseIndex, D>();
|
|
|
|
auto ends_indices = Eigen::DSizes<Eigen::DenseIndex, D>();
|
|
|
|
auto strides_indices = Eigen::DSizes<Eigen::DenseIndex, D>();
|
|
|
|
auto strides_indices = Eigen::DSizes<Eigen::DenseIndex, D>();
|
|
|
|
auto reverse_axis = Eigen::array<bool, D>();
|
|
|
|
auto reverse_axis = Eigen::array<bool, D>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto list_new_ends_tensor =
|
|
|
|
|
|
|
|
context.MultiInput<framework::Tensor>("EndsTensorList");
|
|
|
|
|
|
|
|
auto list_new_starts_tensor =
|
|
|
|
|
|
|
|
context.MultiInput<framework::Tensor>("StartsTensorList");
|
|
|
|
|
|
|
|
auto list_new_strides_tensor =
|
|
|
|
|
|
|
|
context.MultiInput<framework::Tensor>("StridesTensorList");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (list_new_starts_tensor.size() > 0) {
|
|
|
|
|
|
|
|
starts = get_new_data_from_tensorlist(list_new_starts_tensor);
|
|
|
|
|
|
|
|
} else if (context.HasInput("StartsTensor")) {
|
|
|
|
|
|
|
|
auto* starts_tensor = context.Input<framework::Tensor>("StartsTensor");
|
|
|
|
|
|
|
|
starts = get_new_data_from_tensor(starts_tensor);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (list_new_ends_tensor.size() > 0) {
|
|
|
|
|
|
|
|
ends = get_new_data_from_tensorlist(list_new_ends_tensor);
|
|
|
|
|
|
|
|
} else if (context.HasInput("EndsTensor")) {
|
|
|
|
|
|
|
|
auto* ends_tensor = context.Input<framework::Tensor>("EndsTensor");
|
|
|
|
|
|
|
|
ends = get_new_data_from_tensor(ends_tensor);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (list_new_strides_tensor.size() > 0) {
|
|
|
|
|
|
|
|
strides = get_new_data_from_tensorlist(list_new_strides_tensor);
|
|
|
|
|
|
|
|
} else if (context.HasInput("StridesTensor")) {
|
|
|
|
|
|
|
|
auto* strides_tensor = context.Input<framework::Tensor>("StridesTensor");
|
|
|
|
|
|
|
|
strides = get_new_data_from_tensor(strides_tensor);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<int> out_dims_vector(in_dims.size(), -1);
|
|
|
|
|
|
|
|
StridedSliceOutDims(starts, ends, strides, axes, infer_flags, in_dims,
|
|
|
|
|
|
|
|
out_dims_vector.data(), axes.size(), false);
|
|
|
|
|
|
|
|
framework::DDim out_dims(framework::make_ddim(out_dims_vector));
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<int> reverse_vector(starts.size(), 0);
|
|
|
|
std::vector<int> reverse_vector(starts.size(), 0);
|
|
|
|
StridedSliceFunctor(starts.data(), ends.data(), strides.data(), axes.data(),
|
|
|
|
StridedSliceFunctor(starts.data(), ends.data(), strides.data(), axes.data(),
|
|
|
|
reverse_vector.data(), in_dims, starts.size());
|
|
|
|
reverse_vector.data(), in_dims, starts.size());
|
|
|
@ -112,6 +198,7 @@ class StridedSliceKernel : public framework::OpKernel<T> {
|
|
|
|
starts_indices[axis] = 0;
|
|
|
|
starts_indices[axis] = 0;
|
|
|
|
ends_indices[axis] = out_dims[axis];
|
|
|
|
ends_indices[axis] = out_dims[axis];
|
|
|
|
strides_indices[axis] = 1;
|
|
|
|
strides_indices[axis] = 1;
|
|
|
|
|
|
|
|
reverse_axis[axis] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (size_t axis = 0; axis < axes.size(); axis++) {
|
|
|
|
for (size_t axis = 0; axis < axes.size(); axis++) {
|
|
|
|
int axis_index = axes[axis];
|
|
|
|
int axis_index = axes[axis];
|
|
|
@ -124,6 +211,7 @@ class StridedSliceKernel : public framework::OpKernel<T> {
|
|
|
|
framework::Tensor tmp;
|
|
|
|
framework::Tensor tmp;
|
|
|
|
tmp.mutable_data<T>(out_dims, context.GetPlace());
|
|
|
|
tmp.mutable_data<T>(out_dims, context.GetPlace());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out->Resize(out_dims);
|
|
|
|
out->mutable_data<T>(context.GetPlace());
|
|
|
|
out->mutable_data<T>(context.GetPlace());
|
|
|
|
auto in_t =
|
|
|
|
auto in_t =
|
|
|
|
framework::EigenTensor<T, D, Eigen::RowMajor, Eigen::DenseIndex>::From(
|
|
|
|
framework::EigenTensor<T, D, Eigen::RowMajor, Eigen::DenseIndex>::From(
|
|
|
@ -189,6 +277,34 @@ class StridedSliceGradKernel : public framework::OpKernel<T> {
|
|
|
|
auto strides = context.Attr<std::vector<int>>("strides");
|
|
|
|
auto strides = context.Attr<std::vector<int>>("strides");
|
|
|
|
auto axes = context.Attr<std::vector<int>>("axes");
|
|
|
|
auto axes = context.Attr<std::vector<int>>("axes");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto list_new_ends_tensor =
|
|
|
|
|
|
|
|
context.MultiInput<framework::Tensor>("EndsTensorList");
|
|
|
|
|
|
|
|
auto list_new_starts_tensor =
|
|
|
|
|
|
|
|
context.MultiInput<framework::Tensor>("StartsTensorList");
|
|
|
|
|
|
|
|
auto list_new_strides_tensor =
|
|
|
|
|
|
|
|
context.MultiInput<framework::Tensor>("StridesTensorList");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (list_new_starts_tensor.size() > 0) {
|
|
|
|
|
|
|
|
starts = get_new_data_from_tensorlist(list_new_starts_tensor);
|
|
|
|
|
|
|
|
} else if (context.HasInput("StartsTensor")) {
|
|
|
|
|
|
|
|
auto* starts_tensor = context.Input<framework::Tensor>("StartsTensor");
|
|
|
|
|
|
|
|
starts = get_new_data_from_tensor(starts_tensor);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (list_new_ends_tensor.size() > 0) {
|
|
|
|
|
|
|
|
ends = get_new_data_from_tensorlist(list_new_ends_tensor);
|
|
|
|
|
|
|
|
} else if (context.HasInput("EndsTensor")) {
|
|
|
|
|
|
|
|
auto* ends_tensor = context.Input<framework::Tensor>("EndsTensor");
|
|
|
|
|
|
|
|
ends = get_new_data_from_tensor(ends_tensor);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (list_new_strides_tensor.size() > 0) {
|
|
|
|
|
|
|
|
strides = get_new_data_from_tensorlist(list_new_strides_tensor);
|
|
|
|
|
|
|
|
} else if (context.HasInput("StridesTensor")) {
|
|
|
|
|
|
|
|
auto* strides_tensor = context.Input<framework::Tensor>("StridesTensor");
|
|
|
|
|
|
|
|
strides = get_new_data_from_tensor(strides_tensor);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auto starts_indices = Eigen::DSizes<Eigen::DenseIndex, D>();
|
|
|
|
auto starts_indices = Eigen::DSizes<Eigen::DenseIndex, D>();
|
|
|
|
auto ends_indices = Eigen::DSizes<Eigen::DenseIndex, D>();
|
|
|
|
auto ends_indices = Eigen::DSizes<Eigen::DenseIndex, D>();
|
|
|
|
auto strides_indices = Eigen::DSizes<Eigen::DenseIndex, D>();
|
|
|
|
auto strides_indices = Eigen::DSizes<Eigen::DenseIndex, D>();
|
|
|
|