conv_op_xpu.cc 11.9 KB
Newer Older
X
xiaoting 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2020 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 <memory>
#include <string>
#include <vector>
14 15

#include "paddle/fluid/operators/conv_op.h"
X
xiaoting 已提交
16
#include "paddle/fluid/platform/cudnn_workspace_helper.h"
17
#include "paddle/fluid/platform/device/device_wrapper.h"
X
xiaoting 已提交
18 19 20 21 22 23
#ifdef PADDLE_WITH_XPU
namespace paddle {
namespace operators {

template <typename DeviceContext, typename T>
class GemmConvXPUKernel : public framework::OpKernel<T> {
24 25
  using XPUT = typename XPUTypeTrait<T>::Type;

X
xiaoting 已提交
26
 public:
27 28
  void Compute(const framework::ExecutionContext &context) const override {
    const Tensor *input = context.Input<Tensor>("Input");
X
xiaoting 已提交
29 30 31 32
    // The filter will be reshaped in the calculations,
    // so here use an assignment operation,
    // that avoids modifying the variable in the Scope.
    Tensor filter = *context.Input<Tensor>("Filter");
33
    Tensor *output = context.Output<Tensor>("Output");
X
xiaoting 已提交
34 35 36 37 38
    output->mutable_data<T>(context.GetPlace());
    int groups = context.Attr<int>("groups");
    std::vector<int> strides = context.Attr<std::vector<int>>("strides");
    std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");
    std::vector<int> dilations = context.Attr<std::vector<int>>("dilations");
39 40 41 42
    const std::string data_format = context.Attr<std::string>("data_format");
    const std::string padding_algorithm =
        context.Attr<std::string>("padding_algorithm");

43
    PADDLE_ENFORCE_EQ(
44 45
        data_format == "NDHWC",
        false,
46 47
        platform::errors::InvalidArgument(
            ("XPU does not support data_format is NDHWC in conv op.")));
48 49

    framework::DDim in_data_dims =
50
        phi::slice_ddim(input->dims(), 2, input->dims().size());
51
    framework::DDim filter_data_dims =
52 53
        phi::slice_ddim(filter.dims(), 2, filter.dims().size());
    std::vector<int> ksize = phi::vectorize<int>(filter_data_dims);
54 55
    UpdatePaddingAndDilation(
        &paddings, &dilations, padding_algorithm, in_data_dims, strides, ksize);
56

57 58 59 60 61 62 63 64 65 66 67 68
    int batch_size = static_cast<int>(input->dims()[0]);
    int img_c = static_cast<int>(input->dims()[1]);
    int img_h = static_cast<int>(input->dims()[2]);
    int img_w = static_cast<int>(input->dims()[3]);
    int f = static_cast<int>(filter.dims()[0]);
    bool is_nchw = true;
    if (data_format == "NHWC") {
      img_c = static_cast<int>(input->dims()[3]);
      img_h = static_cast<int>(input->dims()[1]);
      img_w = static_cast<int>(input->dims()[2]);
      is_nchw = false;
    }
69 70 71 72 73 74

    const XPUT *input_data = reinterpret_cast<const XPUT *>(input->data<T>());
    const XPUT *filter_data = reinterpret_cast<const XPUT *>(filter.data<T>());
    XPUT *output_data = reinterpret_cast<XPUT *>(output->data<T>());

    auto &dev_ctx = context.template device_context<DeviceContext>();
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    xpu::ctx_guard RAII_GUARD(dev_ctx.x_context());

    XPUT *filter_data_tmp;
    const XPUT *filter_data_ptr = filter_data;
    if (data_format == "NHWC") {
      filter_data_tmp = RAII_GUARD.alloc<XPUT>(filter.numel());
      PADDLE_ENFORCE_XDNN_NOT_NULL(filter_data_tmp);
      std::vector<int> filter_shape = phi::vectorize<int>(filter.dims());
      int r = xpu::transpose<XPUT>(dev_ctx.x_context(),
                                   filter_data,
                                   filter_data_tmp,
                                   filter_shape,
                                   {0, 2, 3, 1});
      PADDLE_ENFORCE_XDNN_SUCCESS(r, "transpose");
      filter_data_ptr = reinterpret_cast<const XPUT *>(filter_data_tmp);
    }

92 93
    int r = xpu::conv2d<XPUT, XPUT, XPUT, int16_t>(dev_ctx.x_context(),
                                                   input_data,
94
                                                   filter_data_ptr,
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
                                                   output_data,
                                                   batch_size,
                                                   img_c,
                                                   img_h,
                                                   img_w,
                                                   f,
                                                   ksize,
                                                   strides,
                                                   paddings,
                                                   dilations,
                                                   groups,
                                                   nullptr,
                                                   nullptr,
                                                   nullptr,
                                                   is_nchw);
110
    PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv2d");
X
xiaoting 已提交
111 112
  }
};
113

X
xiaoting 已提交
114 115
template <typename DeviceContext, typename T>
class GemmConvGradXPUKernel : public framework::OpKernel<T> {
116 117
  using XPUT = typename XPUTypeTrait<T>::Type;

X
xiaoting 已提交
118
 public:
119 120 121
  void Compute(const framework::ExecutionContext &context) const override {
    const Tensor *input = context.Input<Tensor>("Input");
    const Tensor *output_grad =
X
xiaoting 已提交
122
        context.Input<Tensor>(framework::GradVarName("Output"));
123
    Tensor *input_grad =
X
xiaoting 已提交
124
        context.Output<Tensor>(framework::GradVarName("Input"));
125
    Tensor *filter_grad =
X
xiaoting 已提交
126 127 128 129 130 131 132 133 134 135
        context.Output<Tensor>(framework::GradVarName("Filter"));
    // The filter and filter_grad will be reshaped in the calculations,
    // so here use an assignment operation,
    // that avoids modifying the variable in the Scope.
    Tensor filter = *context.Input<Tensor>("Filter");
    if (!input_grad && !filter_grad) return;
    int groups = context.Attr<int>("groups");
    std::vector<int> strides = context.Attr<std::vector<int>>("strides");
    std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");
    std::vector<int> dilations = context.Attr<std::vector<int>>("dilations");
136 137 138 139 140
    const std::string data_format = context.Attr<std::string>("data_format");
    const std::string padding_algorithm =
        context.Attr<std::string>("padding_algorithm");

    PADDLE_ENFORCE_EQ(
141 142
        data_format == "NDHWC",
        false,
143
        platform::errors::InvalidArgument(
144
            ("XPU doesn't support data_format is NDHWC in conv grad op.")));
145 146

    framework::DDim in_data_dims =
147
        phi::slice_ddim(input->dims(), 2, input->dims().size());
148
    framework::DDim filter_data_dims =
149 150
        phi::slice_ddim(filter.dims(), 2, filter.dims().size());
    std::vector<int> ksize = phi::vectorize<int>(filter_data_dims);
151
    std::vector<int> filter_shape = phi::vectorize<int>(filter.dims());
152 153
    UpdatePaddingAndDilation(
        &paddings, &dilations, padding_algorithm, in_data_dims, strides, ksize);
154

155 156 157 158 159 160 161 162 163 164 165 166
    int batch_size = static_cast<int>(input->dims()[0]);
    int img_c = static_cast<int>(input->dims()[1]);
    int img_h = static_cast<int>(input->dims()[2]);
    int img_w = static_cast<int>(input->dims()[3]);
    int f = static_cast<int>(filter.dims()[0]);
    bool is_nchw = true;
    if (data_format == "NHWC") {
      img_c = static_cast<int>(input->dims()[3]);
      img_h = static_cast<int>(input->dims()[1]);
      img_w = static_cast<int>(input->dims()[2]);
      is_nchw = false;
    }
167 168 169 170 171 172

    const XPUT *input_data = reinterpret_cast<const XPUT *>(input->data<T>());
    const XPUT *filter_data = reinterpret_cast<const XPUT *>(filter.data<T>());
    const XPUT *output_grad_data =
        reinterpret_cast<const XPUT *>(output_grad->data<T>());
    XPUT *input_grad_data = nullptr;
X
xiaoting 已提交
173 174
    if (input_grad) {
      input_grad->mutable_data<T>(context.GetPlace());
175
      input_grad_data = reinterpret_cast<XPUT *>(input_grad->data<T>());
X
xiaoting 已提交
176
    }
177
    XPUT *filter_grad_data = nullptr;
X
xiaoting 已提交
178 179
    if (filter_grad) {
      filter_grad->mutable_data<T>(context.GetPlace());
180
      filter_grad_data = reinterpret_cast<XPUT *>(filter_grad->data<T>());
X
xiaoting 已提交
181
    }
182
    auto &dev_ctx = context.template device_context<DeviceContext>();
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    xpu::ctx_guard RAII_GUARD(dev_ctx.x_context());

    XPUT *filter_data_tmp;
    XPUT *filter_grad_data_tmp;
    const XPUT *filter_data_ptr = filter_data;
    XPUT *filter_grad_data_ptr = filter_grad_data;
    if (data_format == "NHWC") {
      filter_data_tmp = RAII_GUARD.alloc<XPUT>(filter.numel());
      PADDLE_ENFORCE_XDNN_NOT_NULL(filter_data_tmp);
      int r = xpu::transpose<XPUT>(dev_ctx.x_context(),
                                   filter_data,
                                   filter_data_tmp,
                                   filter_shape,
                                   {0, 2, 3, 1});
      PADDLE_ENFORCE_XDNN_SUCCESS(r, "transpose");
      filter_data_ptr = reinterpret_cast<const XPUT *>(filter_data_tmp);

      if (filter_grad_data != nullptr) {
        filter_grad_data_tmp = RAII_GUARD.alloc<XPUT>(filter.numel());
        PADDLE_ENFORCE_XDNN_NOT_NULL(filter_grad_data_tmp);
        filter_grad_data_ptr = filter_grad_data_tmp;
      }
    }
206 207
    int r = xpu::conv2d_grad<XPUT, XPUT, XPUT, int16_t>(dev_ctx.x_context(),
                                                        input_data,
208
                                                        filter_data_ptr,
209 210
                                                        output_grad_data,
                                                        input_grad_data,
211
                                                        filter_grad_data_ptr,
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
                                                        batch_size,
                                                        img_c,
                                                        img_h,
                                                        img_w,
                                                        f,
                                                        ksize,
                                                        strides,
                                                        paddings,
                                                        dilations,
                                                        groups,
                                                        nullptr,
                                                        nullptr,
                                                        nullptr,
                                                        nullptr,
                                                        nullptr,
                                                        is_nchw);
228 229 230 231 232 233 234 235 236 237 238 239
    PADDLE_ENFORCE_XDNN_SUCCESS(r, "conv2d_grad");

    if ((filter_grad_data_ptr != nullptr) && (data_format == "NHWC")) {
      std::vector<int> filter_shape_fhwc = {
          filter_shape[0], filter_shape[2], filter_shape[3], filter_shape[1]};
      int r = xpu::transpose<XPUT>(dev_ctx.x_context(),
                                   filter_grad_data_ptr,
                                   filter_grad_data,
                                   filter_shape_fhwc,
                                   {0, 3, 1, 2});
      PADDLE_ENFORCE_XDNN_SUCCESS(r, "transpose");
    }
X
xiaoting 已提交
240 241 242 243 244 245
  }
};
}  // namespace operators
}  // namespace paddle
namespace ops = paddle::operators;
REGISTER_OP_XPU_KERNEL(
246 247
    conv2d,
    ops::GemmConvXPUKernel<paddle::platform::XPUDeviceContext, float>,
248 249
    ops::GemmConvXPUKernel<paddle::platform::XPUDeviceContext,
                           paddle::platform::float16>);
X
xiaoting 已提交
250 251
REGISTER_OP_XPU_KERNEL(
    conv2d_grad,
252 253 254 255 256
    ops::GemmConvGradXPUKernel<paddle::platform::XPUDeviceContext, float>,
    ops::GemmConvGradXPUKernel<paddle::platform::XPUDeviceContext,
                               paddle::platform::float16>);
REGISTER_OP_XPU_KERNEL(
    depthwise_conv2d,
257
    ops::GemmConvXPUKernel<paddle::platform::XPUDeviceContext, float>);
258 259
REGISTER_OP_XPU_KERNEL(
    depthwise_conv2d_grad,
260
    ops::GemmConvGradXPUKernel<paddle::platform::XPUDeviceContext, float>);
X
xiaoting 已提交
261
#endif