pool_op_xpu.cc 10.8 KB
Newer Older
D
Double_V 已提交
1 2 3 4 5 6 7 8 9 10
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
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
    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. */
F
From00 已提交
11

D
Double_V 已提交
12
#include <unordered_map>
13

F
From00 已提交
14 15
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/tensor.h"
16
#include "paddle/phi/kernels/funcs/pooling.h"
D
Double_V 已提交
17 18 19 20 21

#ifdef PADDLE_WITH_XPU
namespace paddle {
namespace operators {

F
From00 已提交
22 23
using framework::Tensor;

24 25
xpu::Pooling_t XPUPoolingType(const std::string& pooltype,
                              bool exclusive,
D
Double_V 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39
                              bool is_test) {
  if (pooltype == "max") {
    return xpu::Pooling_t::MAX_WITHOUT_INDEX;
  } else if (pooltype == "avg") {
    if (exclusive) {
      return xpu::Pooling_t::AVG_WITHOUT_PAD;
    } else {
      return xpu::Pooling_t::AVG_WITH_PAD;
    }
  } else {
    PADDLE_THROW(platform::errors::InvalidArgument(
        "Pool op only supports 2D and 3D input."));
  }
}
40

D
Double_V 已提交
41 42
template <typename DeviceContext, typename T>
class PoolXPUKernel : public framework::OpKernel<T> {
43 44
  using XPUType = typename XPUTypeTrait<T>::Type;

D
Double_V 已提交
45 46 47 48 49 50 51 52 53 54
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    const Tensor* in_x = context.Input<Tensor>("X");
    Tensor* out = context.Output<Tensor>("Out");
    std::string pooling_type = context.Attr<std::string>("pooling_type");
    std::vector<int> ksize = context.Attr<std::vector<int>>("ksize");
    std::vector<int> strides = context.Attr<std::vector<int>>("strides");
    std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");
    bool exclusive = context.Attr<bool>("exclusive");
    bool adaptive = context.Attr<bool>("adaptive");
55 56 57
    bool ceil_mode = context.Attr<bool>("ceil_mode");
    std::string padding_algorithm =
        context.Attr<std::string>("padding_algorithm");
58
    PADDLE_ENFORCE_EQ(
59 60
        ksize.size(),
        2,
61 62
        platform::errors::InvalidArgument(
            "The Pool2d XPU OP only support 2 dimension pooling!"));
63 64
    PADDLE_ENFORCE_EQ(!adaptive || (ksize[0] * ksize[1] == 1),
                      true,
65 66 67
                      platform::errors::InvalidArgument(
                          "The Pool2d XPU OP does not support (adaptive == "
                          "true && output_size != 1)"));
D
Double_V 已提交
68
    int* index_data = nullptr;
69 70 71
    bool global_pooling = context.Attr<bool>("global_pooling") ||
                          (adaptive && (ksize[0] * ksize[1] == 1));
    if (global_pooling) {
D
Double_V 已提交
72 73 74 75 76
      for (size_t i = 0; i < ksize.size(); ++i) {
        paddings[i] = 0;
        ksize[i] = static_cast<int>(in_x->dims()[i + 2]);
      }
    }
77

78 79
    const int n = in_x->dims()[0];
    const int c = in_x->dims()[1];
D
Double_V 已提交
80 81
    const int in_h = in_x->dims()[2];
    const int in_w = in_x->dims()[3];
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

    framework::DDim data_dims;

    data_dims = phi::slice_ddim(in_x->dims(), 2, in_x->dims().size());
    phi::funcs::UpdatePadding(&paddings,
                              global_pooling,
                              adaptive,
                              padding_algorithm,
                              data_dims,
                              strides,
                              ksize);
    if (ceil_mode) {
      paddings[1] += (strides[0] - 1);
      paddings[3] += (strides[1] - 1);
    }

98
    auto input = reinterpret_cast<const XPUType*>(in_x->data<T>());
D
Double_V 已提交
99
    out->mutable_data<T>(context.GetPlace());
100
    auto output = reinterpret_cast<XPUType*>(out->data<T>());
D
Double_V 已提交
101
    auto& dev_ctx = context.template device_context<DeviceContext>();
102 103
    int r = xpu::Error_t::SUCCESS;
    if (pooling_type == "max") {
104 105 106 107 108 109 110 111 112 113 114 115
      r = xpu::max_pool2d<XPUType>(dev_ctx.x_context(),
                                   input,
                                   output,
                                   index_data,
                                   n,
                                   c,
                                   in_h,
                                   in_w,
                                   ksize,
                                   strides,
                                   paddings,
                                   true);
116
    } else if (pooling_type == "avg") {
117 118 119 120 121 122 123 124 125 126 127 128
      r = xpu::avg_pool2d<XPUType>(dev_ctx.x_context(),
                                   input,
                                   output,
                                   n,
                                   c,
                                   in_h,
                                   in_w,
                                   ksize,
                                   strides,
                                   paddings,
                                   !exclusive,
                                   true);
129 130 131 132
    } else {
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Unsupported pooling type for kunlun ", pooling_type));
    }
133 134
    PADDLE_ENFORCE_EQ(r,
                      xpu::Error_t::SUCCESS,
135
                      platform::errors::External(
136 137
                          "The pool2d XPU API return wrong value[%d %s]",
                          r,
138
                          XPUAPIErrorMsg[r]));
D
Double_V 已提交
139 140
  }
};
141

D
Double_V 已提交
142 143
template <typename DeviceContext, typename T>
class PoolGradXPUKernel : public framework::OpKernel<T> {
144 145
  using XPUType = typename XPUTypeTrait<T>::Type;

D
Double_V 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    const Tensor* in_x = context.Input<Tensor>("X");
    const Tensor* out = context.Input<Tensor>("Out");
    const Tensor* out_grad =
        context.Input<Tensor>(framework::GradVarName("Out"));
    Tensor* in_x_grad = context.Output<Tensor>(framework::GradVarName("X"));
    std::string pooling_type = context.Attr<std::string>("pooling_type");
    std::vector<int> ksize = context.Attr<std::vector<int>>("ksize");
    std::vector<int> strides = context.Attr<std::vector<int>>("strides");
    std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");
    bool exclusive = context.Attr<bool>("exclusive");
    bool adaptive = context.Attr<bool>("adaptive");
159 160 161
    bool ceil_mode = context.Attr<bool>("ceil_mode");
    std::string padding_algorithm =
        context.Attr<std::string>("padding_algorithm");
D
Double_V 已提交
162
    const int* index_data = nullptr;
163
    PADDLE_ENFORCE_EQ(
164 165
        ksize.size(),
        2,
166 167 168 169
        platform::errors::InvalidArgument("The Pool2d XPU OP only support 2 "
                                          "dimension pooling!, but received "
                                          "%d-dimension pool kernel size",
                                          ksize.size()));
170 171
    PADDLE_ENFORCE_EQ(!adaptive || (ksize[0] * ksize[1] == 1),
                      true,
172 173 174 175 176 177
                      platform::errors::InvalidArgument(
                          "The Pool2d XPU OP does not support (adaptive == "
                          "true && output_size != 1)"));
    bool global_pooling = context.Attr<bool>("global_pooling") ||
                          (adaptive && (ksize[0] * ksize[1] == 1));
    if (global_pooling) {
D
Double_V 已提交
178 179 180 181 182 183 184 185
      for (size_t i = 0; i < ksize.size(); ++i) {
        paddings[i] = 0;
        ksize[i] = static_cast<int>(in_x->dims()[i + 2]);
      }
    }
    if (!in_x_grad) {
      return;
    }
186 187
    const int n = in_x->dims()[0];
    const int c = in_x->dims()[1];
D
Double_V 已提交
188 189
    const int in_h = in_x->dims()[2];
    const int in_w = in_x->dims()[3];
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

    framework::DDim data_dims;

    data_dims = phi::slice_ddim(in_x->dims(), 2, in_x->dims().size());
    phi::funcs::UpdatePadding(&paddings,
                              global_pooling,
                              adaptive,
                              padding_algorithm,
                              data_dims,
                              strides,
                              ksize);
    if (ceil_mode) {
      paddings[1] += (strides[0] - 1);
      paddings[3] += (strides[1] - 1);
    }

206 207 208
    auto input = reinterpret_cast<const XPUType*>(in_x->data<T>());
    auto output = reinterpret_cast<const XPUType*>(out->data<T>());
    auto output_grad = reinterpret_cast<const XPUType*>(out_grad->data<T>());
D
Double_V 已提交
209
    in_x_grad->mutable_data<T>(context.GetPlace());
210
    auto input_grad = reinterpret_cast<XPUType*>(in_x_grad->data<T>());
D
Double_V 已提交
211
    auto& dev_ctx = context.template device_context<DeviceContext>();
212 213
    int r = xpu::Error_t::SUCCESS;
    if (pooling_type == "max") {
214 215 216 217 218 219 220 221 222 223 224 225 226 227
      r = xpu::max_pool2d_grad<XPUType>(dev_ctx.x_context(),
                                        input,
                                        output,
                                        index_data,
                                        output_grad,
                                        input_grad,
                                        n,
                                        c,
                                        in_h,
                                        in_w,
                                        ksize,
                                        strides,
                                        paddings,
                                        true);
228
    } else if (pooling_type == "avg") {
229 230 231 232 233 234 235 236 237 238 239 240 241 242
      r = xpu::avg_pool2d_grad<XPUType>(dev_ctx.x_context(),
                                        input,
                                        output,
                                        output_grad,
                                        input_grad,
                                        n,
                                        c,
                                        in_h,
                                        in_w,
                                        ksize,
                                        strides,
                                        paddings,
                                        !exclusive,
                                        true);
243 244 245 246
    } else {
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Unsupported pooling type for kunlun ", pooling_type));
    }
247 248
    PADDLE_ENFORCE_EQ(r,
                      xpu::Error_t::SUCCESS,
249
                      platform::errors::External(
250 251
                          "The Pool2dGrad XPU OP return wrong value[%d %s]",
                          r,
252
                          XPUAPIErrorMsg[r]));
D
Double_V 已提交
253 254 255 256 257 258 259 260
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_XPU_KERNEL(
261 262
    pool2d,
    ops::PoolXPUKernel<paddle::platform::XPUDeviceContext, float>,
263 264
    ops::PoolXPUKernel<paddle::platform::XPUDeviceContext,
                       paddle::platform::float16>);
D
Double_V 已提交
265 266
REGISTER_OP_XPU_KERNEL(
    pool2d_grad,
267 268 269
    ops::PoolGradXPUKernel<paddle::platform::XPUDeviceContext, float>,
    ops::PoolGradXPUKernel<paddle::platform::XPUDeviceContext,
                           paddle::platform::float16>);
D
Double_V 已提交
270 271

#endif