pool_op_xpu.cc 7.2 KB
Newer Older
D
Double_V 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/* 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. */
#include "paddle/fluid/operators/pool_op.h"
#include <unordered_map>

#ifdef PADDLE_WITH_XPU
namespace paddle {
namespace operators {

xpu::Pooling_t XPUPoolingType(const std::string& pooltype, bool exclusive,
                              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."));
  }
}
33

D
Double_V 已提交
34 35 36 37 38 39 40 41 42 43 44 45
template <typename DeviceContext, typename T>
class PoolXPUKernel : public framework::OpKernel<T> {
 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");
46 47 48 49
    PADDLE_ENFORCE_EQ(
        ksize.size(), 2,
        platform::errors::InvalidArgument(
            "The Pool2d XPU OP only support 2 dimension pooling!"));
50 51 52 53
    PADDLE_ENFORCE_EQ(!adaptive || (ksize[0] * ksize[1] == 1), true,
                      platform::errors::InvalidArgument(
                          "The Pool2d XPU OP does not support (adaptive == "
                          "true && output_size != 1)"));
D
Double_V 已提交
54
    int* index_data = nullptr;
55 56 57
    bool global_pooling = context.Attr<bool>("global_pooling") ||
                          (adaptive && (ksize[0] * ksize[1] == 1));
    if (global_pooling) {
D
Double_V 已提交
58 59 60 61 62
      for (size_t i = 0; i < ksize.size(); ++i) {
        paddings[i] = 0;
        ksize[i] = static_cast<int>(in_x->dims()[i + 2]);
      }
    }
63 64
    const int n = in_x->dims()[0];
    const int c = in_x->dims()[1];
D
Double_V 已提交
65 66 67 68 69 70
    const int in_h = in_x->dims()[2];
    const int in_w = in_x->dims()[3];
    const float* input = in_x->data<float>();
    out->mutable_data<T>(context.GetPlace());
    float* output = out->data<float>();
    auto& dev_ctx = context.template device_context<DeviceContext>();
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    int r = xpu::Error_t::SUCCESS;
    if (pooling_type == "max") {
      r = xpu::max_pool2d(dev_ctx.x_context(), input, output, index_data, n, c,
                          in_h, in_w, ksize, strides, paddings, true);
    } else if (pooling_type == "avg") {
      r = xpu::avg_pool2d(dev_ctx.x_context(), input, output, n, c, in_h, in_w,
                          ksize, strides, paddings, !exclusive, true);
    } else {
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Unsupported pooling type for kunlun ", pooling_type));
    }
    PADDLE_ENFORCE_EQ(r, xpu::Error_t::SUCCESS,
                      platform::errors::External(
                          "The pool2d XPU API return wrong value[%d %s]", r,
                          XPUAPIErrorMsg[r]));
D
Double_V 已提交
86 87
  }
};
88

D
Double_V 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
template <typename DeviceContext, typename T>
class PoolGradXPUKernel : public framework::OpKernel<T> {
 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");
    const int* index_data = nullptr;
105 106 107 108 109
    PADDLE_ENFORCE_EQ(ksize.size(), 2, platform::errors::InvalidArgument(
                                           "The Pool2d XPU OP only support 2 "
                                           "dimension pooling!, but received "
                                           "%d-dimension pool kernel size",
                                           ksize.size()));
110 111 112 113 114 115 116
    PADDLE_ENFORCE_EQ(!adaptive || (ksize[0] * ksize[1] == 1), true,
                      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 已提交
117 118 119 120 121 122 123 124
      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;
    }
125 126
    const int n = in_x->dims()[0];
    const int c = in_x->dims()[1];
D
Double_V 已提交
127 128 129 130 131 132 133 134
    const int in_h = in_x->dims()[2];
    const int in_w = in_x->dims()[3];
    const float* input = in_x->data<float>();
    const float* output = out->data<float>();
    const float* output_grad = out_grad->data<float>();
    in_x_grad->mutable_data<T>(context.GetPlace());
    float* input_grad = in_x_grad->data<float>();
    auto& dev_ctx = context.template device_context<DeviceContext>();
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    int r = xpu::Error_t::SUCCESS;
    if (pooling_type == "max") {
      r = xpu::max_pool2d_grad(dev_ctx.x_context(), input, output, index_data,
                               output_grad, input_grad, n, c, in_h, in_w, ksize,
                               strides, paddings, true);
    } else if (pooling_type == "avg") {
      r = xpu::avg_pool2d_grad(dev_ctx.x_context(), input, output, output_grad,
                               input_grad, n, c, in_h, in_w, ksize, strides,
                               paddings, !exclusive, true);
    } else {
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Unsupported pooling type for kunlun ", pooling_type));
    }
    PADDLE_ENFORCE_EQ(r, xpu::Error_t::SUCCESS,
                      platform::errors::External(
                          "The Pool2dGrad XPU OP return wrong value[%d %s]", r,
                          XPUAPIErrorMsg[r]));
D
Double_V 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_XPU_KERNEL(
    pool2d, ops::PoolXPUKernel<paddle::platform::XPUDeviceContext, float>);
REGISTER_OP_XPU_KERNEL(
    pool2d_grad,
    ops::PoolGradXPUKernel<paddle::platform::XPUDeviceContext, float>);

#endif