|
|
|
@ -24,19 +24,7 @@ namespace operators { // Internal
|
|
|
|
|
template <typename T, size_t D, int MajorType = Eigen::RowMajor,
|
|
|
|
|
typename IndexType = Eigen::DenseIndex>
|
|
|
|
|
using EigenTensor = framework::EigenTensor<T, D, MajorType, IndexType>;
|
|
|
|
|
|
|
|
|
|
using framework::Tensor;
|
|
|
|
|
using framework::DDim;
|
|
|
|
|
|
|
|
|
|
// TODO(wanghaoshuang): move this function to other place
|
|
|
|
|
DDim stride(const DDim& ddim) {
|
|
|
|
|
std::vector<int64_t> strides(ddim.size());
|
|
|
|
|
strides[ddim.size() - 1] = 1;
|
|
|
|
|
for (int i = ddim.size() - 2; i >= 0; --i) {
|
|
|
|
|
strides[i] = strides[i + 1] * ddim[i + 1];
|
|
|
|
|
}
|
|
|
|
|
return make_ddim(strides);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
class CropKernel : public framework::OpKernel {
|
|
|
|
@ -44,13 +32,13 @@ class CropKernel : public framework::OpKernel {
|
|
|
|
|
void Compute(const framework::ExecutionContext& context) const override {
|
|
|
|
|
auto* x = context.Input<Tensor>("X");
|
|
|
|
|
auto* out = context.Output<Tensor>("Out");
|
|
|
|
|
T* x_data = x->data<T>();
|
|
|
|
|
const T* x_data = x->data<T>();
|
|
|
|
|
T* out_data = out->mutable_data<T>(context.GetPlace());
|
|
|
|
|
auto x_stride = stride(x->dims());
|
|
|
|
|
auto out_stride = stride(out->dims());
|
|
|
|
|
auto x_stride = framework::stride(x->dims());
|
|
|
|
|
auto out_stride = framework::stride(out->dims());
|
|
|
|
|
auto offsets = context.Attr<std::vector<int>>("offsets");
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
x_dims.size(), offsets.size(),
|
|
|
|
|
x->dims().size(), offsets.size(),
|
|
|
|
|
"Offsets size should be equal to dimension size of input tensor.");
|
|
|
|
|
int64_t offset = 0;
|
|
|
|
|
for (int i = 0; i < offsets.size(); ++i) {
|
|
|
|
@ -71,7 +59,7 @@ void CropGradFunction(const framework::ExecutionContext& context) {
|
|
|
|
|
Eigen::array<std::pair<int, int>, D> paddings;
|
|
|
|
|
for (int i = 0; i < D; ++i) {
|
|
|
|
|
paddings[i].first = offsets[i];
|
|
|
|
|
paddings[i].second = d_x_dims[i] - d_out_dims[i] - offsets[i];
|
|
|
|
|
paddings[i].second = d_x->dims()[i] - d_out->dims()[i] - offsets[i];
|
|
|
|
|
}
|
|
|
|
|
auto d_x_tensor = EigenTensor<T, D>::From(*d_x);
|
|
|
|
|
auto d_out_tensor = EigenTensor<T, D>::From(*d_out);
|
|
|
|
|