prelu_kernel.cpp 3.6 KB
Newer Older
T
Tian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Copyright (c) 2018 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. */

#ifdef PRELU_OP

#include "operators/kernel/prelu_kernel.h"
#include <operators/math/transform.h>

namespace paddle_mobile {
I
itminner 已提交
21
namespace operators {
T
Tian 已提交
22

I
itminner 已提交
23 24 25 26
template <typename T>
struct PReluFunctor {
  explicit PReluFunctor(float slope) { this->slope_ = slope; }
  inline T operator()(T in) const { return in > 0 ? in : in * slope_; }
T
Tian 已提交
27

I
itminner 已提交
28 29
  float slope_ = 0.0f;
};
T
Tian 已提交
30 31 32 33

/*
 * @b 特化到具体平台的实现, param 从 op 层传入
 * */
I
itminner 已提交
34 35 36 37 38 39
template <>
void PReluKernel<CPU, float>::Compute(const PReluParam &param) const {
  const auto *input_x = param.InputX();
  auto *input_x_ptr = input_x->data<float>();
  auto *out = param.Out();
  auto *out_ptr = out->mutable_data<float>();
T
Tian 已提交
40

I
itminner 已提交
41 42 43 44 45 46 47 48 49 50 51 52
  if (param.Slopes().size() == 1) {
    PReluFunctor<float> func_(param.Slopes()[0]);
    math::Transform trans;
    trans(input_x_ptr, input_x_ptr + input_x->numel(), out_ptr, func_);
  } else if (param.Slopes().size() > 1) {
    const int dim_size = input_x->dims().size();
    switch (dim_size) {
      case 0:
        break;
      case 1: {
        const int input_width = input_x->dims()[0];
        math::Transform trans;
T
Tian 已提交
53

I
itminner 已提交
54 55 56 57 58 59 60 61
        #pragma omp parallel for
        for (int w = 0; w < input_width; ++w) {
          out_ptr[w] = input_x_ptr[w] * param.Slopes()[w];
        }
      } break;
      case 2: {
        const int input_height = input_x->dims()[0];
        const int input_width = input_x->dims()[1];
T
Tian 已提交
62

I
itminner 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75
        math::Transform trans;
        #pragma omp parallel for
        for (int h = 0; h < input_height; ++h) {
          PReluFunctor<float> func_(param.Slopes()[h]);
          const float *ptr = input_x_ptr + h * input_width;
          float *optr = out_ptr + +h * input_width;
          trans(ptr, ptr + input_width, optr, func_);
        }
      } break;
      case 3: {
        const int chan_size = input_x->dims()[0];
        const int input_height = input_x->dims()[1];
        const int input_width = input_x->dims()[2];
T
Tian 已提交
76

I
itminner 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        math::Transform trans;
        #pragma omp parallel for
        for (int c = 0; c < chan_size; ++c) {
          PReluFunctor<float> func_(param.Slopes()[c]);
          int size = input_height * input_width;
          const float *ptr = input_x_ptr + c * size;
          float *optr = out_ptr + c * size;
          trans(ptr, ptr + size, optr, func_);
        }
      } break;
      case 4:
      default: {
        const int batch_size = input_x->dims()[0];
        const int chan_size = input_x->dims()[1];
        const int input_height = input_x->dims()[2];
        const int input_width = input_x->dims()[3];
        math::Transform trans;
T
Tian 已提交
94

I
itminner 已提交
95 96 97 98 99 100 101 102 103
        #pragma omp parallel for
        for (int b = 0; b < batch_size; ++b) {
          for (int c = 0; c < chan_size; ++c) {
            PReluFunctor<float> func_(param.Slopes()[c]);
            int size = input_height * input_width;
            const float *ptr = input_x_ptr + b * c * size;
            float *optr = out_ptr + +b * c * size;
            trans(ptr, ptr + size, optr, func_);
          }
T
Tian 已提交
104
        }
I
itminner 已提交
105 106 107 108 109 110
      }  // case 3,default
      break;
    }
  }
}
}  // namespace operators
T
Tian 已提交
111 112
}  // namespace paddle_mobile

I
itminner 已提交
113
#endif