affine_channel_op_xpu.cc 6.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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.
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. */

#ifdef PADDLE_WITH_XPU

#include <string>
#include <unordered_map>
#include <vector>
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#include "paddle/fluid/framework/data_layout.h"
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"

namespace paddle {
namespace operators {

template <typename DeviceContext, typename T>
class AffineChannelXPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<framework::Tensor>("X");
    auto* scale = ctx.Input<framework::Tensor>("Scale");
    auto* bias = ctx.Input<framework::Tensor>("Bias");

    auto* y = ctx.Output<framework::Tensor>("Out");
    y->mutable_data<T>(ctx.GetPlace());

    const framework::DataLayout layout =
        framework::StringToDataLayout(ctx.Attr<std::string>("data_layout"));

    auto dims = x->dims();
    int N = dims[0];
    int C = layout == framework::DataLayout::kNCHW ? dims[1]
                                                   : dims[dims.size() - 1];
    int HxW = x->numel() / N / C;

    auto* scale_d = scale->data<T>();
    auto* bias_d = bias->data<T>();

    auto* x_d = x->data<T>();
    auto* y_d = y->data<T>();
    auto& dev_ctx = ctx.template device_context<DeviceContext>();
    std::vector<int> x_shape;
    std::vector<int> b_shape;
    if (layout == framework::DataLayout::kNCHW) {
      x_shape.push_back(N);
      x_shape.push_back(C);
      x_shape.push_back(HxW);
      b_shape.push_back(1);
      b_shape.push_back(C);
      b_shape.push_back(1);
    } else {
      x_shape.push_back(N * HxW);
      x_shape.push_back(C);
      b_shape.push_back(1);
      b_shape.push_back(C);
    }
    int r = 0;
70 71 72 73
    r = xpu::broadcast_mul(
        dev_ctx.x_context(), x_d, scale_d, y_d, x_shape, b_shape);
    PADDLE_ENFORCE_EQ(r,
                      xpu::Error_t::SUCCESS,
74 75
                      platform::errors::External(
                          "The broadcast_mul XPU OP return wrong value[%d %s]",
76 77 78 79 80 81
                          r,
                          XPUAPIErrorMsg[r]));
    r = xpu::broadcast_add(
        dev_ctx.x_context(), y_d, bias_d, y_d, x_shape, b_shape);
    PADDLE_ENFORCE_EQ(r,
                      xpu::Error_t::SUCCESS,
82 83
                      platform::errors::External(
                          "The broadcast_add XPU OP return wrong value[%d %s]",
84 85
                          r,
                          XPUAPIErrorMsg[r]));
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  }
};

template <typename DeviceContext, typename T>
class AffineChannelGradXPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<framework::Tensor>("X");
    auto* scale = ctx.Input<framework::Tensor>("Scale");
    auto* dy = ctx.Input<framework::Tensor>(framework::GradVarName("Out"));

    auto* dx = ctx.Output<framework::Tensor>(framework::GradVarName("X"));
    auto* dscale =
        ctx.Output<framework::Tensor>(framework::GradVarName("Scale"));
    auto* dbias = ctx.Output<framework::Tensor>(framework::GradVarName("Bias"));

    const framework::DataLayout layout =
        framework::StringToDataLayout(ctx.Attr<std::string>("data_layout"));

    auto dims = x->dims();
    int N = dims[0];
    int C = layout == framework::DataLayout::kNCHW ? dims[1]
                                                   : dims[dims.size() - 1];
    int HxW = x->numel() / N / C;

    auto* dy_d = dy->data<T>();
    auto* scale_d = scale->data<T>();

    T* dx_d = dx ? dx->mutable_data<T>(ctx.GetPlace()) : nullptr;
    T* dscale_d = dscale ? dscale->mutable_data<T>(ctx.GetPlace()) : nullptr;
    T* dbias_d = dbias ? dbias->mutable_data<T>(ctx.GetPlace()) : nullptr;

    auto& dev_ctx = ctx.template device_context<DeviceContext>();
    std::vector<int> x_shape;
    std::vector<int> b_shape;
    std::vector<int> rdims;
    if (layout == framework::DataLayout::kNCHW) {
      x_shape.push_back(N);
      x_shape.push_back(C);
      x_shape.push_back(HxW);
      b_shape.push_back(1);
      b_shape.push_back(C);
      b_shape.push_back(1);
      rdims.push_back(0);
      rdims.push_back(2);
    } else {
      x_shape.push_back(N * HxW);
      x_shape.push_back(C);
      b_shape.push_back(1);
      b_shape.push_back(C);
      rdims.push_back(0);
    }

    int r = 0;
    if (dscale_d && dbias_d) {
141 142 143 144
      r = xpu::reduce_sum<T>(
          dev_ctx.x_context(), dy_d, dbias_d, x_shape, rdims);
      PADDLE_ENFORCE_EQ(r,
                        xpu::Error_t::SUCCESS,
145 146
                        platform::errors::External(
                            "The reduce_sum XPU OP return wrong value[%d %s]",
147 148
                            r,
                            XPUAPIErrorMsg[r]));
149 150
      T* tmp = nullptr;
      r = xpu_malloc(reinterpret_cast<void**>(&tmp), dy->numel() * sizeof(T));
151 152
      PADDLE_ENFORCE_EQ(r,
                        xpu::Error_t::SUCCESS,
153 154
                        platform::errors::External("no enough memory in xpu"));

155 156
      r = xpu::mul<T>(
          dev_ctx.x_context(), dy_d, x->data<T>(), tmp, dy->numel());
157
      PADDLE_ENFORCE_EQ(
158 159
          r,
          xpu::Error_t::SUCCESS,
160
          platform::errors::External("The mul XPU OP return wrong value[%d %s]",
161 162 163 164 165 166
                                     r,
                                     XPUAPIErrorMsg[r]));
      r = xpu::reduce_sum<T>(
          dev_ctx.x_context(), tmp, dscale_d, x_shape, rdims);
      PADDLE_ENFORCE_EQ(r,
                        xpu::Error_t::SUCCESS,
167 168
                        platform::errors::External(
                            "The reduce_sum XPU OP return wrong value[%d %s]",
169 170
                            r,
                            XPUAPIErrorMsg[r]));
171 172 173 174 175 176
      if (dev_ctx.x_context()->xpu_stream) {
        dev_ctx.Wait();
      }
      xpu_free(tmp);
    }
    if (dx_d) {
177 178
      r = xpu::broadcast_mul(
          dev_ctx.x_context(), dy_d, scale_d, dx_d, x_shape, b_shape);
179
      PADDLE_ENFORCE_EQ(
180 181
          r,
          xpu::Error_t::SUCCESS,
182
          platform::errors::External(
183 184
              "The broadcast_mul XPU OP return wrong value[%d %s]",
              r,
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
              XPUAPIErrorMsg[r]));
    }
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
using XPU = paddle::platform::XPUDeviceContext;

REGISTER_OP_XPU_KERNEL(affine_channel, ops::AffineChannelXPUKernel<XPU, float>);
REGISTER_OP_XPU_KERNEL(affine_channel_grad,
                       ops::AffineChannelGradXPUKernel<XPU, float>);

#endif