elementwise_add_compute.cu 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2019 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. */

#pragma once
#include <vector>
Z
Zhaolong Xing 已提交
14
#include "lite/backends/cuda/math/elementwise.h"
15 16 17 18 19 20 21 22
#include "lite/core/op_registry.h"
#include "lite/kernels/cuda/elementwise_add_compute.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace cuda {

Z
Zhaolong Xing 已提交
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
void ElementwiseAddCompute::Run() {
  auto& param = this->Param<param_t>();
  auto& ctx = this->ctx_->template As<CUDAContext>();
  auto stream = ctx.exec_stream();

  const lite::Tensor* x = param.X;
  const lite::Tensor* y = param.Y;
  lite::Tensor* out = param.Out;

  CHECK(x->dims() == y->dims());

  const int n = x->dims()[0];
  const int c = x->dims()[1];
  const int h = x->dims()[2];
  const int w = x->dims()[3];

  auto* x_data = x->data<float>();
  auto* y_data = y->data<float>();
  auto out_data = out->mutable_data<float>(TARGET(kCUDA));

  int pixel_num = x->numel();
  lite::cuda::math::elementwise_add(
      pixel_num, x_data, y_data, out_data, stream);

  cudaError_t error = cudaGetLastError();
  if (error != cudaSuccess) LOG(INFO) << cudaGetErrorString(error);
49 50
}

Z
Zhaolong Xing 已提交
51
void ElementwiseAddComputeNHWC::Run() {
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  auto& param = this->Param<param_t>();
  auto& ctx = this->ctx_->template As<CUDAContext>();
  auto stream = ctx.exec_stream();

  const lite::Tensor* x = param.X;
  const lite::Tensor* y = param.Y;
  lite::Tensor* out = param.Out;

  CHECK(x->dims() == y->dims());

  const int n = x->dims()[0];
  const int c = x->dims()[1];
  const int h = x->dims()[2];
  const int w = x->dims()[3];

  auto* x_data = x->data<float>();
  auto* y_data = y->data<float>();
  auto out_data = out->mutable_data<float>(TARGET(kCUDA));

  int pixel_num = x->numel();
Z
Zhaolong Xing 已提交
72 73
  lite::cuda::math::elementwise_add(
      pixel_num, x_data, y_data, out_data, stream);
74

Z
Zhaolong Xing 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  cudaError_t error = cudaGetLastError();
  if (error != cudaSuccess) LOG(INFO) << cudaGetErrorString(error);
}

void ElementwiseAddComputeInt8::Run() {
  auto& param = this->Param<param_t>();
  auto& ctx = this->ctx_->template As<CUDAContext>();
  auto stream = ctx.exec_stream();

  const lite::Tensor* x = param.X;
  const lite::Tensor* y = param.Y;
  lite::Tensor* out = param.Out;

  CHECK(x->dims() == y->dims());

  const int c = x->dims()[3];

  auto* x_data = x->data<float>();
  auto* y_data = y->data<float>();
  auto out_data = out->mutable_data<int8_t>(TARGET(kCUDA));

  int pixel_num = x->numel();
  float output_scale = param.output_scale;
  if (c % 4 == 0) {
    lite::cuda::math::elementwise_add_nhwc4_int8(
        pixel_num / 4,
        static_cast<const void*>(x_data),
        static_cast<const void*>(y_data),
        1. / output_scale,
        static_cast<void*>(out_data),
        stream);
  } else {
    lite::cuda::math::elementwise_add_int8(
        pixel_num, x_data, y_data, 1. / output_scale, out_data, stream);
  }
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

  cudaError_t error = cudaGetLastError();
  if (error != cudaSuccess) LOG(INFO) << cudaGetErrorString(error);
}

}  // namespace cuda
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_KERNEL(elementwise_add,
                     kCUDA,
                     kFloat,
                     kNCHW,
                     paddle::lite::kernels::cuda::ElementwiseAddCompute,
                     def)
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kCUDA))})
    .BindInput("Y", {LiteType::GetTensorTy(TARGET(kCUDA))})
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kCUDA))})
    .Finalize();
Z
Zhaolong Xing 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

REGISTER_LITE_KERNEL(elementwise_add,
                     kCUDA,
                     kFloat,
                     kNHWC,
                     paddle::lite::kernels::cuda::ElementwiseAddComputeNHWC,
                     nhwc_format)
    .BindInput("X",
               {LiteType::GetTensorTy(TARGET(kCUDA),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNHWC))})
    .BindInput("Y",
               {LiteType::GetTensorTy(TARGET(kCUDA),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNHWC))})
    .BindOutput("Out",
                {LiteType::GetTensorTy(TARGET(kCUDA),
                                       PRECISION(kFloat),
                                       DATALAYOUT(kNHWC))})
    .Finalize();