sequence_pool_kernel.cpp 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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 SEQUENCE_POOL_OP

#include <cmath>
H
hjchen2 已提交
18
#include <limits>
19 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
#include <string>
#include <vector>
#include "common/types.h"
#include "operators/kernel/sequence_kernels.h"
#include "operators/math/pooling.h"
#ifdef __ARM_NEON__
#include <arm_neon.h>
#endif  // __ARM_NEON__

namespace paddle_mobile {
namespace operators {

template <PoolingType P = MAX, typename T = float>
void SequencePoolImpl(const framework::LoDTensor &input,
                      framework::LoDTensor *output) {
  const float *input_ptr = input.data<float>();
  float *output_ptr = output->mutable_data<float>();
  const auto &lod = input.lod()[0];
  int64_t width = input.numel() / input.dims()[0];

  #pragma omp parallel for
  for (int i = 0; i < static_cast<int>(lod.size()) - 1; ++i) {
    const float *in_ptr = input_ptr + lod[i] * width;
    float *out_ptr = output_ptr + i * width;
    int64_t height = static_cast<int64_t>(lod[i + 1] - lod[i]);
    if (width == 1) {
H
hjchen2 已提交
45
      float max = -std::numeric_limits<float>::max();
46 47 48 49 50 51 52 53 54 55 56
      int remain_h = height;
#ifdef __ARM_NEON__
      int loop = remain_h >> 2;
      remain_h = remain_h & 0x3;
      float32x4_t __max4 = math::vPoolInitq_f32<MAX>();
      for (int h = 0; h < loop; ++h) {
        float32x4_t r0 = vld1q_f32(in_ptr);
        __max4 = vmaxq_f32(__max4, r0);
        in_ptr += 4;
      }
      float32x2_t __max2 =
H
hjchen2 已提交
57 58 59
          vpmax_f32(vget_low_f32(__max4), vget_high_f32(__max4));
      __max2 = vpmax_f32(__max2, __max2);
      max = std::max(max, vget_lane_f32(__max2, 0));
60 61
#endif  // __ARM_NEON__
      for (int h = 0; h < remain_h; ++h) {
H
hjchen2 已提交
62
        max = std::max(max, in_ptr[h]);
63
      }
H
hjchen2 已提交
64
      *out_ptr = max;
65 66
    } else {
      memcpy(out_ptr, in_ptr, width * sizeof(float));
H
hjchen2 已提交
67
      in_ptr += width;
68
      int remain_h = height - 1;
69
      int remain_w_start = 0;
70
#ifdef __ARM_NEON__
71
      remain_w_start = width & 0xfffc;
72 73 74 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
#endif  // __ARM_NEON__
      for (int h = 0; h < remain_h; ++h) {
#ifdef __ARM_NEON__
        for (int w = 0; w < width; w += 4) {
          float32x4_t __in = vld1q_f32(in_ptr + w);
          float32x4_t __out = vld1q_f32(out_ptr + w);
          __out = vmaxq_f32(__out, __in);
          vst1q_f32(out_ptr + w, __out);
        }
#endif  // __ARM_NEON__
        for (int w = remain_w_start; w < width; ++w) {
          out_ptr[w] = std::max(out_ptr[w], in_ptr[w]);
        }
        in_ptr += width;
      }
    }
  }
}

template <>
void SequencePoolImpl<SUM, float>(const framework::LoDTensor &input,
                                  framework::LoDTensor *output) {
  const float *input_ptr = input.data<float>();
  float *output_ptr = output->mutable_data<float>();
  const auto &lod = input.lod()[0];
  int64_t width = input.numel() / input.dims()[0];

  #pragma omp parallel for
  for (int i = 0; i < static_cast<int>(lod.size()) - 1; ++i) {
    const float *in_ptr = input_ptr + lod[i] * width;
    float *out_ptr = output_ptr + i * width;
    int64_t height = static_cast<int64_t>(lod[i + 1] - lod[i]);
    if (width == 1) {
      float sum = 0.f;
      int remain_h = height;
#ifdef __ARM_NEON__
      int loop = remain_h >> 2;
      remain_h = remain_h & 0x3;
      float32x4_t __sum4 = vdupq_n_f32(0.f);
      for (int h = 0; h < loop; ++h) {
        float32x4_t r0 = vld1q_f32(in_ptr);
        __sum4 = vaddq_f32(__sum4, r0);
        in_ptr += 4;
      }
      float32x2_t __sum2 =
          vpadd_f32(vget_low_f32(__sum4), vget_high_f32(__sum4));
      sum += vget_lane_f32(__sum2, 0) + vget_lane_f32(__sum2, 1);
#endif  // __ARM_NEON__
      for (int h = 0; h < remain_h; ++h) {
        sum += in_ptr[h];
      }
      *out_ptr = sum;
    } else {
      memcpy(out_ptr, in_ptr, width * sizeof(float));
H
hjchen2 已提交
126
      in_ptr += width;
127
      int remain_h = height - 1;
128
      int remain_w_start = 0;
129 130
#ifdef __ARM_NEON__
      int loop_w = width >> 2;
131
      remain_w_start = width & 0xfffc;
132 133 134
#endif  // __ARM_NEON__
      for (int h = 0; h < remain_h; ++h) {
#ifdef __ARM_NEON__
H
hjchen2 已提交
135
        for (int w = 0; w < width - 3; w += 4) {
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
          float32x4_t __in = vld1q_f32(in_ptr + w);
          float32x4_t __out = vld1q_f32(out_ptr + w);
          __out = vaddq_f32(__out, __in);
          vst1q_f32(out_ptr + w, __out);
        }
#endif  // __ARM_NEON__
        for (int w = remain_w_start; w < width; ++w) {
          out_ptr[w] += in_ptr[w];
        }
        in_ptr += width;
      }
    }
  }
}

template <>
void SequencePoolImpl<FIRST, float>(const framework::LoDTensor &input,
                                    framework::LoDTensor *output) {
  const float *input_ptr = input.data<float>();
  float *output_ptr = output->mutable_data<float>();
  const auto &lod = input.lod()[0];
  int64_t width = input.numel() / input.dims()[0];

  for (int i = 0; i < static_cast<int>(lod.size()) - 1; ++i) {
    const float *in_ptr = input_ptr + lod[i] * width;
    float *out_ptr = output_ptr + i * width;
    memcpy(out_ptr, in_ptr, width * sizeof(float));
  }
}

template <typename T>
class SequencePoolKernel<CPU, T>
    : public framework::OpKernelBase<CPU, SequencePoolParam<CPU>> {
 public:
  bool Init(SequencePoolParam<CPU> *param) { return true; }

  void Compute(const SequencePoolParam<CPU> &param) {
    const framework::LoDTensor *input = param.input_;
    framework::LoDTensor *output = param.output_;
    output->mutable_data<T>();
H
hjchen2 已提交
176
    const std::string pooling_type = param.pool_type_;
177 178 179 180 181 182 183

    if (param.pool_type_ == "MAX") {
      SequencePoolImpl<MAX, T>(*input, output);
    } else if (param.pool_type_ == "FIRST") {
      SequencePoolImpl<FIRST, T>(*input, output);
    } else if (param.pool_type_ == "SUM") {
      SequencePoolImpl<SUM, T>(*input, output);
H
hjchen2 已提交
184 185 186 187
    } else {
      PADDLE_MOBILE_THROW_EXCEPTION(
          "pooling type `%s` has not been implemented.",
          param.pool_type_.c_str());
188 189 190 191 192 193 194 195 196 197
    }
  }
};

template class SequencePoolKernel<CPU, float>;

}  // namespace operators
}  // namespace paddle_mobile

#endif  // SEQUENCE_POOL_OP