spp_op.h 7.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
S
sweetsky0901 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Indicesou may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

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. */

#pragma once
16 17
#include <string>
#include <vector>
Y
Yi Wang 已提交
18 19 20
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/pooling.h"
#include "paddle/fluid/operators/strided_memcpy.h"
21
#include "paddle/phi/kernels/funcs/math_function.h"
S
sweetsky0901 已提交
22 23 24

namespace paddle {
namespace operators {
S
sweetsky0901 已提交
25
template <typename DeviceContext, typename T>
S
sweetsky0901 已提交
26 27 28 29 30 31
class SppKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    const framework::Tensor* in_x = context.Input<framework::Tensor>("X");
    auto* out = context.Output<framework::Tensor>("Out");
    int pyramid_height = context.template Attr<int>("pyramid_height");
S
sweetsky0901 已提交
32 33
    std::string pooling_type =
        context.template Attr<std::string>("pooling_type");
S
sweetsky0901 已提交
34
    out->mutable_data<T>(context.GetPlace());
35
    auto out_stride = phi::stride(out->dims());
S
sweetsky0901 已提交
36 37 38 39 40
    int input_h = in_x->dims()[2];
    int input_w = in_x->dims()[3];
    size_t output_offset = 0;
    for (int p = 0; p < pyramid_height; ++p) {
      int bins = std::pow(2, p);
S
sweetsky0901 已提交
41 42 43 44 45 46
      int kernel_size_h = std::ceil(input_h / static_cast<double>(bins));
      int kernel_size_w = std::ceil(input_w / static_cast<double>(bins));
      int padding_h = (kernel_size_h * bins - input_h + 1) / 2;
      int padding_w = (kernel_size_w * bins - input_w + 1) / 2;
      std::vector<int> kernel_size({kernel_size_h, kernel_size_w});
      std::vector<int> strides({kernel_size_h, kernel_size_w});
S
sweetsky0901 已提交
47 48
      std::vector<int> paddings({padding_h, padding_w});
      // pooling output shape
S
sweetsky0901 已提交
49
      framework::Tensor out_level;
S
sweetsky0901 已提交
50 51
      std::vector<int64_t> output_shape_vec(
          {in_x->dims()[0], in_x->dims()[1], bins, bins});
52
      framework::DDim output_shape(phi::make_ddim(output_shape_vec));
S
sweetsky0901 已提交
53 54
      out_level.mutable_data<T>(output_shape, context.GetPlace());
      // pooling
S
sweetsky0901 已提交
55 56 57 58
      if (pooling_type == "max") {
        math::Pool2dFunctor<DeviceContext, math::MaxPool<T>, T> pool_forward;
        math::MaxPool<T> max_process;
        pool_forward(context.template device_context<DeviceContext>(), *in_x,
59 60
                     kernel_size, strides, paddings, true, false, &out_level,
                     max_process);
S
sweetsky0901 已提交
61 62 63 64
      } else if (pooling_type == "avg") {
        math::Pool2dFunctor<DeviceContext, math::AvgPool<T>, T> pool_forward;
        math::AvgPool<T> avg_process;
        pool_forward(context.template device_context<DeviceContext>(), *in_x,
65 66
                     kernel_size, strides, paddings, true, false, &out_level,
                     avg_process);
S
sweetsky0901 已提交
67
      }
S
sweetsky0901 已提交
68 69 70 71 72
      // flatten pooling output shape
      int output_flatten_w = in_x->dims()[1] * bins * bins;
      std::vector<int64_t> output_flatten_shape_vec(
          {in_x->dims()[0], output_flatten_w});
      framework::DDim output_flatten_shape(
73
          phi::make_ddim(output_flatten_shape_vec));
S
sweetsky0901 已提交
74
      out_level.Resize(output_flatten_shape);
S
sweetsky0901 已提交
75
      // concat
76
      auto out_level_stride = phi::stride(out_level.dims());
S
sweetsky0901 已提交
77 78
      StridedMemcpy<T>(context.template device_context<DeviceContext>(),
                       out_level.data<T>(), out_level_stride, out_level.dims(),
S
sweetsky0901 已提交
79
                       out_stride, out->data<T>() + output_offset);
S
sweetsky0901 已提交
80
      output_offset += out_level.dims()[1] * out_level_stride[1];
S
sweetsky0901 已提交
81 82 83
    }
  }
};
S
sweetsky0901 已提交
84
template <typename DeviceContext, typename T>
S
sweetsky0901 已提交
85 86 87 88 89 90 91 92 93
class SppGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    const framework::Tensor* in_x = context.Input<framework::Tensor>("X");
    const framework::Tensor* out = context.Input<framework::Tensor>("Out");
    const framework::Tensor* out_grad =
        context.Input<framework::Tensor>(framework::GradVarName("Out"));
    framework::Tensor* in_x_grad =
        context.Output<framework::Tensor>(framework::GradVarName("X"));
S
sweetsky0901 已提交
94
    int pyramid_height = context.template Attr<int>("pyramid_height");
S
sweetsky0901 已提交
95 96
    std::string pooling_type =
        context.template Attr<std::string>("pooling_type");
S
sweetsky0901 已提交
97
    auto& device_ctx = context.template device_context<DeviceContext>();
98
    phi::funcs::SetConstant<DeviceContext, T> zero;
S
sweetsky0901 已提交
99 100
    in_x_grad->mutable_data<T>(context.GetPlace());
    zero(device_ctx, in_x_grad, static_cast<T>(0));
101
    auto out_stride = phi::stride(out->dims());
S
sweetsky0901 已提交
102 103 104 105 106
    int input_h = in_x->dims()[2];
    int input_w = in_x->dims()[3];
    size_t out_offset = 0;
    for (int p = 0; p < pyramid_height; ++p) {
      int bins = std::pow(2, p);
S
sweetsky0901 已提交
107 108 109 110 111 112
      int kernel_size_h = std::ceil(input_h / static_cast<double>(bins));
      int kernel_size_w = std::ceil(input_w / static_cast<double>(bins));
      int padding_h = (kernel_size_h * bins - input_h + 1) / 2;
      int padding_w = (kernel_size_w * bins - input_w + 1) / 2;
      std::vector<int> kernel_size({kernel_size_h, kernel_size_w});
      std::vector<int> strides({kernel_size_h, kernel_size_w});
S
sweetsky0901 已提交
113
      std::vector<int> paddings({padding_h, padding_w});
S
sweetsky0901 已提交
114
      // split out and outgrad  ...  to flatten
S
sweetsky0901 已提交
115 116
      framework::Tensor out_level;
      framework::Tensor outgrad_level;
S
sweetsky0901 已提交
117 118 119
      int out_flatten_w = in_x->dims()[1] * bins * bins;
      std::vector<int64_t> out_flatten_shape_vec(
          {in_x->dims()[0], out_flatten_w});
120
      framework::DDim out_flatten_shape(phi::make_ddim(out_flatten_shape_vec));
S
sweetsky0901 已提交
121 122
      out_level.mutable_data<T>(out_flatten_shape, context.GetPlace());
      outgrad_level.mutable_data<T>(out_flatten_shape, context.GetPlace());
123
      auto flatten_stride = phi::stride(out_level.dims());
S
sweetsky0901 已提交
124
      // memcpy
S
sweetsky0901 已提交
125 126 127
      StridedMemcpy<T>(context.template device_context<DeviceContext>(),
                       out->data<T>() + out_offset, out_stride,
                       out_level.dims(), flatten_stride, out_level.data<T>());
S
sweetsky0901 已提交
128

S
sweetsky0901 已提交
129
      StridedMemcpy<T>(context.template device_context<DeviceContext>(),
S
sweetsky0901 已提交
130
                       out_grad->data<T>() + out_offset, out_stride,
S
sweetsky0901 已提交
131 132 133
                       outgrad_level.dims(), flatten_stride,
                       outgrad_level.data<T>());
      out_offset += out_level.dims()[1] * out_stride[1];
S
sweetsky0901 已提交
134
      // flatten backward to nchw
S
sweetsky0901 已提交
135

S
sweetsky0901 已提交
136
      std::vector<int64_t> out_shape_vec({in_x->dims()[0], in_x->dims()[1]});
S
sweetsky0901 已提交
137 138 139 140
      out_shape_vec.push_back(
          (input_h - kernel_size_h + 2 * padding_h) / kernel_size_h + 1);
      out_shape_vec.push_back(
          (input_w - kernel_size_w + 2 * padding_w) / kernel_size_w + 1);
141
      framework::DDim out_shape(phi::make_ddim(out_shape_vec));
S
sweetsky0901 已提交
142
      out_level.ShareDataWith(out_level);
S
sweetsky0901 已提交
143
      out_level.Resize(out_shape);
S
sweetsky0901 已提交
144
      outgrad_level.ShareDataWith(outgrad_level);
S
sweetsky0901 已提交
145
      outgrad_level.Resize(out_shape);
S
sweetsky0901 已提交
146
      // pooling backward
S
sweetsky0901 已提交
147 148 149 150 151 152 153 154 155 156
      if (pooling_type == "max") {
        math::MaxPool2dGradFunctor<DeviceContext, T> pool2d_backward;
        pool2d_backward(context.template device_context<DeviceContext>(), *in_x,
                        *&out_level, *&outgrad_level, kernel_size, strides,
                        paddings, in_x_grad);
      } else if (pooling_type == "avg") {
        math::Pool2dGradFunctor<DeviceContext, math::AvgPoolGrad<T>, T>
            pool_backward;
        math::AvgPoolGrad<T> avg_process;
        pool_backward(context.template device_context<DeviceContext>(), *in_x,
S
sweetsky0901 已提交
157
                      *&out_level, *&outgrad_level, kernel_size, strides,
158
                      paddings, true, false, in_x_grad, avg_process);
S
sweetsky0901 已提交
159
      }
S
sweetsky0901 已提交
160 161 162 163 164
    }
  }
};
}  // namespace operators
}  // namespace paddle