crop_op.h 3.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
W
wanghaoshuang 已提交
2

L
Luo Tao 已提交
3 4 5
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
W
wanghaoshuang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
W
wanghaoshuang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
W
wanghaoshuang 已提交
14 15

#pragma once
S
Siddharth Goyal 已提交
16 17
#include <utility>
#include <vector>
Y
Yi Wang 已提交
18 19 20
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/strided_memcpy.h"
W
wanghaoshuang 已提交
21 22

namespace paddle {
23
namespace operators {  // Internal
W
wanghaoshuang 已提交
24 25 26 27

template <typename T, size_t D, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenTensor = framework::EigenTensor<T, D, MajorType, IndexType>;
28 29 30
using framework::Tensor;

template <typename T>
Y
Yu Yang 已提交
31
class CropKernel : public framework::OpKernel<T> {
32 33 34 35
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* x = context.Input<Tensor>("X");
    auto* out = context.Output<Tensor>("Out");
W
wanghaoshuang 已提交
36
    const T* x_data = x->data<T>();
37
    T* out_data = out->mutable_data<T>(context.GetPlace());
W
wanghaoshuang 已提交
38 39
    auto x_stride = framework::stride(x->dims());
    auto out_stride = framework::stride(out->dims());
40 41
    auto offsets = context.Attr<std::vector<int>>("offsets");
    PADDLE_ENFORCE_EQ(
Q
qiaolongfei 已提交
42
        x->dims().size(), static_cast<int64_t>(offsets.size()),
43 44
        "Offsets size should be equal to dimension size of input tensor.");
    int64_t offset = 0;
Q
qiaolongfei 已提交
45
    for (size_t i = 0; i < offsets.size(); ++i) {
46 47 48 49 50 51
      offset += (x_stride[i] * offsets[i]);
    }
    StridedMemcpy<T>(context.device_context(), x_data + offset, x_stride,
                     out->dims(), out_stride, out_data);
  }
};
W
wanghaoshuang 已提交
52

Q
QI JUN 已提交
53
template <typename DeviceContext, typename T, size_t D>
W
wanghaoshuang 已提交
54
void CropGradFunction(const framework::ExecutionContext& context) {
55
  auto* d_x = context.Output<Tensor>(framework::GradVarName("X"));
56
  if (d_x != nullptr) {
57
    auto* d_out = context.Input<Tensor>(framework::GradVarName("Out"));
58
    d_x->mutable_data<T>(context.GetPlace());
59
    auto offsets = context.Attr<std::vector<int>>("offsets");
60
    Eigen::array<std::pair<int, int>, D> paddings;
Q
qiaolongfei 已提交
61
    for (size_t i = 0; i < D; ++i) {
62
      paddings[i].first = offsets[i];
W
wanghaoshuang 已提交
63
      paddings[i].second = d_x->dims()[i] - d_out->dims()[i] - offsets[i];
64 65 66
    }
    auto d_x_tensor = EigenTensor<T, D>::From(*d_x);
    auto d_out_tensor = EigenTensor<T, D>::From(*d_out);
Q
QI JUN 已提交
67 68
    d_x_tensor.device(
        *context.template device_context<DeviceContext>().eigen_device()) =
69
        d_out_tensor.pad(paddings, 0);
W
wanghaoshuang 已提交
70 71 72
  }
}

Q
QI JUN 已提交
73
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
74
class CropGradKernel : public framework::OpKernel<T> {
W
wanghaoshuang 已提交
75 76
 public:
  void Compute(const framework::ExecutionContext& context) const override {
77
    size_t rank =
78
        context.Input<Tensor>(framework::GradVarName("Out"))->dims().size();
79
    switch (rank) {
W
wanghaoshuang 已提交
80
      case 1:
Q
QI JUN 已提交
81
        CropGradFunction<DeviceContext, T, 1>(context);
W
wanghaoshuang 已提交
82 83
        break;
      case 2:
Q
QI JUN 已提交
84
        CropGradFunction<DeviceContext, T, 2>(context);
W
wanghaoshuang 已提交
85 86
        break;
      case 3:
Q
QI JUN 已提交
87
        CropGradFunction<DeviceContext, T, 3>(context);
W
wanghaoshuang 已提交
88 89
        break;
      case 4:
Q
QI JUN 已提交
90
        CropGradFunction<DeviceContext, T, 4>(context);
W
wanghaoshuang 已提交
91 92
        break;
      case 5:
Q
QI JUN 已提交
93
        CropGradFunction<DeviceContext, T, 5>(context);
W
wanghaoshuang 已提交
94 95
        break;
      case 6:
Q
QI JUN 已提交
96
        CropGradFunction<DeviceContext, T, 6>(context);
W
wanghaoshuang 已提交
97 98
        break;
      default:
99 100
        PADDLE_THROW(
            "CropOp only support tensors with no more than 6 dimensions.");
W
wanghaoshuang 已提交
101 102 103 104 105 106
    }
  }
};

}  // namespace operators
}  // namespace paddle