pooling3x3.cpp 45.1 KB
Newer Older
H
hjchen2 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/* 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 POOL_OP

#include "operators/math/pooling.h"
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
#include <arm_neon.h>
#endif  // __ARM_NEON

namespace paddle_mobile {
namespace operators {
namespace math {

#define POOLING3X3_NORMAL_BORDER(start, end)                   \
  for (int w = start; w < end; ++w) {                          \
    const int w_in_start = -padding_w + w * Stride;            \
    const int w_in_end = w_in_start + 3;                       \
    const int w_start = w_in_start > 0 ? w_in_start : 0;       \
    const int w_end = w_in_end < input_w ? w_in_end : input_w; \
    PoolingVal<P> val;                                         \
    for (int h_in = h_start; h_in < h_end; ++h_in) {           \
      for (int w_in = w_start; w_in < w_end; ++w_in) {         \
        val += input[h_in * input_w + w_in];                   \
      }                                                        \
    }                                                          \
    output_ptr[w] = val.Value();                               \
  }

H
hjchen2 已提交
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 70 71 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 126 127 128 129 130 131 132 133 134 135 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
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
template <PoolingType P, int Stride = 1>
struct Pooling3x3ValidColLoadInput {
  inline void operator()(const float *input, const int input_w,
                         const int valid_cols, float32x4x2_t &x0,  // NOLINT
                         float32x4x2_t &x1, float32x4x2_t &x2,     // NOLINT
                         float32x4x2_t &y0) {                      // NOLINT
    float fake_input[3][8];
    if (valid_cols == 1) {
      for (int i = 0; i < 8; ++i, input += input_w) {
        fake_input[0][i] = input[0];
      }
    } else if (valid_cols == 2) {
      for (int i = 0; i < 8; ++i, input += input_w) {
        fake_input[0][i] = input[0];
        fake_input[1][i] = input[1];
      }
    } else {
      for (int i = 0; i < 8; ++i, input += input_w) {
        fake_input[0][i] = input[0];
        fake_input[1][i] = input[1];
        fake_input[2][i] = input[2];
      }
    }
    y0.val[0] = vPoolInitq_f32<P>();
    y0.val[1] = vPoolInitq_f32<P>();
    for (int i = 0; i < valid_cols; ++i) {
      x0.val[0] = vld1q_f32(fake_input[i]);
      x0.val[1] = vld1q_f32(fake_input[i] + 4);
      x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
      x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
      x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
      x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
      y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
      y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);
      y0.val[0] = vPoolPreq_f32<P>(x1.val[0], y0.val[0]);
      y0.val[1] = vPoolPreq_f32<P>(x1.val[1], y0.val[1]);
      y0.val[0] = vPoolPreq_f32<P>(x2.val[0], y0.val[0]);
      y0.val[1] = vPoolPreq_f32<P>(x2.val[1], y0.val[1]);
    }
  }
};

template <PoolingType P>
struct Pooling3x3ValidColLoadInput<P, 2> {
  inline void operator()(const float *input, const int input_w,
                         const int valid_cols, float32x4x2_t &x0,  // NOLINT
                         float32x4x2_t &x1, float32x4x2_t &x2,     // NOLINT
                         float32x4x2_t &y0) {                      // NOLINT
    float fake_input[3][13];
    if (valid_cols == 1) {
      for (int i = 0; i < 13; ++i, input += input_w) {
        fake_input[0][i] = input[0];
      }
    } else if (valid_cols == 2) {
      for (int i = 0; i < 13; ++i, input += input_w) {
        fake_input[0][i] = input[0];
        fake_input[1][i] = input[1];
      }
    } else {
      for (int i = 0; i < 13; ++i, input += input_w) {
        fake_input[0][i] = input[0];
        fake_input[1][i] = input[1];
        fake_input[2][i] = input[2];
      }
    }
    for (int i = 0; i < valid_cols; ++i) {
      x0 = vld2q_f32(fake_input[i]);
      x1 = vld2q_f32(fake_input[i] + 8);
      x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
      x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
      x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
      x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
      x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
      x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
      y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
      y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);
    }
  }
};

template <PoolingType P, int Stride = 1>
struct Pooling3x3NormalRowLoadInput {
  inline void operator()(const float *input, float32x4x2_t &x0,  // NOLINT
                         float32x4x2_t &x1, float32x4x2_t &x2,   // NOLINT
                         float32x4x2_t &y0) {                    // NOLINT
    x0.val[0] = vld1q_f32(input);
    x0.val[1] = vld1q_f32(input + 4);
    x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
    x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
    x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
    x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
    y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
    y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);
    y0.val[0] = vPoolPreq_f32<P>(x1.val[0], y0.val[0]);
    y0.val[1] = vPoolPreq_f32<P>(x1.val[1], y0.val[1]);
    y0.val[0] = vPoolPreq_f32<P>(x2.val[0], y0.val[0]);
    y0.val[1] = vPoolPreq_f32<P>(x2.val[1], y0.val[1]);
  }
};

template <PoolingType P>
struct Pooling3x3NormalRowLoadInput<P, 2> {
  inline void operator()(const float *input, float32x4x2_t &x0,  // NOLINT
                         float32x4x2_t &x1, float32x4x2_t &x2,   // NOLINT
                         float32x4x2_t &y0) {                    // NOLINT
    x0 = vld2q_f32(input);
    x1 = vld2q_f32(input + 8);
    x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
    x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
    x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
    x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
    x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
    x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
    y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
    y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);
  }
};
#endif  // __ARM_NEON__

H
hjchen2 已提交
161 162 163 164 165 166 167 168 169 170
template <PoolingType P, int Stride>
inline void Pooling3x3ValidCol(const float *input, const int h_output,
                               const int h_output_end, const int w_output,
                               const int input_h, const int input_w,
                               const int padding_h, const int padding_w,
                               const int output_w, float *output) {
  const int w_in_start = -padding_w + w_output * Stride;
  const int w_in_end = w_in_start + 3;
  const int w_start = w_in_start > 0 ? w_in_start : 0;
  const int w_end = w_in_end < input_w ? w_in_end : input_w;
H
hjchen2 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
  int remain_start = h_output;
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
  int output_tiles = (h_output_end - h_output) / 6;
  remain_start = h_output + output_tiles * 6;
  int input_h_start = h_output * Stride - padding_h;
  size_t input_offset = input_h_start * input_w + w_start;
  size_t output_offset = h_output * output_w + w_output;
  int valid_cols = w_end - w_start;
  Pooling3x3ValidColLoadInput<P, Stride> PoolingCompute;
  float32x4x2_t x0, x1, x2, y0;
  float32x4_t avg = vdupq_n_f32(1.f / (3 * valid_cols));
  for (int h = 0; h < output_tiles * 6; h += 6) {
    float *output0 = output + output_offset;
    float *output1 = output0 + output_w;
    float *output2 = output1 + output_w;
    float *output3 = output2 + output_w;
    float *output4 = output3 + output_w;
    float *output5 = output4 + output_w;
    y0.val[0] = vPoolInitq_f32<P>();
    y0.val[1] = vPoolInitq_f32<P>();
    PoolingCompute(input + input_offset, input_w, valid_cols, x0, x1, x2, y0);
    y0.val[0] = vPoolPostq_f32<P>(y0.val[0], avg);
    y0.val[1] = vPoolPostq_f32<P>(y0.val[1], avg);
    vst1q_lane_f32(output0, y0.val[0], 0);
    vst1q_lane_f32(output1, y0.val[0], 1);
    vst1q_lane_f32(output2, y0.val[0], 2);
    vst1q_lane_f32(output3, y0.val[0], 3);
    vst1q_lane_f32(output4, y0.val[1], 0);
    vst1q_lane_f32(output5, y0.val[1], 1);
H
hjchen2 已提交
200 201
    input_offset += 6 * Stride * input_w;
    output_offset += 6 * output_w;
H
hjchen2 已提交
202 203 204
  }
#endif
  for (int h = remain_start; h < h_output_end; ++h) {
H
hjchen2 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    PoolingVal<P> val;
    const int h_in_start = -padding_h + h * Stride;
    for (int i = 0; i < 3; ++i) {
      for (int w_in = w_start; w_in < w_end; ++w_in) {
        val += input[(h_in_start + i) * input_w + w_in];
      }
    }
    output[h * output_w + w_output] = val.Value();
  }
}

template <PoolingType P, int Stride>
inline void Pooling3x3NormalRow(const float *input, const int h_output,
                                const int input_h, const int input_w,
                                const int padding_h, const int padding_w,
                                const int output_w, float *output) {
  const int h_in_start = -padding_h + h_output * Stride;
  const int h_in_end = h_in_start + 3;
  const int h_start = h_in_start > 0 ? h_in_start : 0;
  const int h_end = h_in_end < input_h ? h_in_end : input_h;

  int valid_w_start = (padding_w + Stride - 1) / Stride;
H
hjchen2 已提交
227
  int valid_w_end = (input_w - 3) / Stride + 1 + valid_w_start;
H
hjchen2 已提交
228 229 230 231 232

  float *output_ptr = output + h_output * output_w;
  // border left
  POOLING3X3_NORMAL_BORDER(0, valid_w_start)
  // middle
H
hjchen2 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  int remain_start = valid_w_start;
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
  int output_tiles = (valid_w_end - valid_w_start) / 6;
  remain_start = valid_w_start + output_tiles * 6;
  Pooling3x3NormalRowLoadInput<P, Stride> PoolingCompute;
  float32x4x2_t x0, x1, x2, y0;
  float32x4_t post = vdupq_n_f32(1.f / (3 * (h_end - h_start)));
  for (int w = 0; w < output_tiles * 6; w += 6) {
    int output_offset = valid_w_start + w;
    int input_w_offset = output_offset * Stride - padding_w;
    y0.val[0] = vPoolInitq_f32<P>();
    y0.val[1] = vPoolInitq_f32<P>();
    for (int h_in = h_start; h_in < h_end; ++h_in) {
      PoolingCompute(input + h_in * input_w + input_w_offset, x0, x1, x2, y0);
    }
    y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
    y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
    vst1q_f32(output_ptr + output_offset, y0.val[0]);
    vst1_f32(output_ptr + output_offset + 4, vget_low_f32(y0.val[1]));
  }
#endif  // __ARM_NEON__
  for (int w = remain_start; w < valid_w_end; ++w) {
H
hjchen2 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    PoolingVal<P> val;
    int input_start = -padding_w + w * Stride;
    for (int h_in = h_start; h_in < h_end; ++h_in) {
      for (int j = 0; j < 3; ++j) {
        val += input[h_in * input_w + j + input_start];
      }
    }
    output_ptr[w] = val.Value();
  }
  // border right
  POOLING3X3_NORMAL_BORDER(valid_w_end, output_w)
}

template <PoolingType P>
struct Pooling3x3<P, 1> {
  inline void operator()(const framework::Tensor &input,
                         const std::vector<int> &paddings,
                         framework::Tensor *output) {
    const float *input_data = input.data<float>();
    float *output_data = output->mutable_data<float>();
    int input_h = input.dims()[2];
    int input_w = input.dims()[3];
    int output_h = output->dims()[2];
    int output_w = output->dims()[3];
    int padding_h = paddings[0];
    int padding_w = paddings[1];
    int image_size = input_h * input_w;
    int out_image_size = output_h * output_w;
    int valid_h_start = padding_h;
H
hjchen2 已提交
284 285
    int valid_h = input_h - 2;
    int valid_h_end = valid_h_start + valid_h;
H
hjchen2 已提交
286
    int valid_w_start = padding_w;
H
hjchen2 已提交
287 288
    int valid_w = input_w - 2;
    int valid_w_end = valid_w_start + valid_w;
H
hjchen2 已提交
289
    float avg = 1.f / 9;
H
hjchen2 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

    #pragma omp parallel for
    for (int c = 0; c < output->dims()[1]; ++c) {
      const float *input_ptr = input_data + c * image_size;
      float *output_ptr = output_data + c * out_image_size;
      // top
      for (int h = 0; h < valid_h_start; ++h) {
        Pooling3x3NormalRow<P, 1>(input_ptr, h, input_h, input_w, padding_h,
                                  padding_w, output_w, output_ptr);
      }
      // left
      for (int w = 0; w < valid_w_start; ++w) {
        Pooling3x3ValidCol<P, 1>(input_ptr, valid_h_start, valid_h_end, w,
                                 input_h, input_w, padding_h, padding_w,
                                 output_w, output_ptr);
      }
      // right
      for (int w = valid_w_end; w < output_w; ++w) {
        Pooling3x3ValidCol<P, 1>(input_ptr, valid_h_start, valid_h_end, w,
                                 input_h, input_w, padding_h, padding_w,
                                 output_w, output_ptr);
      }
      // bottom
      for (int h = valid_h_end; h < output_h; ++h) {
        Pooling3x3NormalRow<P, 1>(input_ptr, h, input_h, input_w, padding_h,
                                  padding_w, output_w, output_ptr);
      }
      // valid
      int output_w_tiles = valid_w / 6;
      int output_w_remain = valid_w - output_w_tiles * 6;
      for (int h = valid_h_start; h < valid_h_end - 3; h += 4) {
        const float *input_ptr0 = input_ptr + (h - padding_h) * input_w;
        const float *input_ptr1 = input_ptr0 + input_w;
        const float *input_ptr2 = input_ptr1 + input_w;
        const float *input_ptr3 = input_ptr2 + input_w;
        const float *input_ptr4 = input_ptr3 + input_w;
        const float *input_ptr5 = input_ptr4 + input_w;
        float *output_ptr0 = output_ptr + h * output_w + valid_w_start;
        float *output_ptr1 = output_ptr0 + output_w;
        float *output_ptr2 = output_ptr1 + output_w;
        float *output_ptr3 = output_ptr2 + output_w;
        int remain = output_w_remain;
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
        float32x4x2_t x0, x1, x2;
        float32x4x2_t y0, y1, y2;
H
hjchen2 已提交
335
        float32x4_t post = vdupq_n_f32(1.f / 9);
H
hjchen2 已提交
336 337 338 339
        for (int loop = 0; loop < output_w_tiles; ++loop) {
          x0.val[0] = vld1q_f32(input_ptr0);
          x0.val[1] = vld1q_f32(input_ptr0 + 4);
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
H
hjchen2 已提交
340
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
341
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
342
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
343 344 345 346 347 348 349 350
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);

          x0.val[0] = vld1q_f32(input_ptr1);
          x0.val[1] = vld1q_f32(input_ptr1 + 4);
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
H
hjchen2 已提交
351
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
352
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
353
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
354 355 356 357 358 359 360 361 362 363
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
          y1.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y1.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(y1.val[0], y0.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(y1.val[1], y0.val[1]);

          x0.val[0] = vld1q_f32(input_ptr2);
          x0.val[1] = vld1q_f32(input_ptr2 + 4);
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
H
hjchen2 已提交
364
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
365
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
366
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
367 368 369 370 371 372 373 374
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
          y2.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y2.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
          y1.val[0] = vPoolPreq_f32<P>(y2.val[0], y1.val[0]);
          y1.val[1] = vPoolPreq_f32<P>(y2.val[1], y1.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(y2.val[0], y0.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(y2.val[1], y0.val[1]);
H
hjchen2 已提交
375 376
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
          y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
H
hjchen2 已提交
377 378 379 380 381 382
          vst1q_f32(output_ptr0, y0.val[0]);
          vst1_f32(output_ptr0 + 4, vget_low_f32(y0.val[1]));

          x0.val[0] = vld1q_f32(input_ptr3);
          x0.val[1] = vld1q_f32(input_ptr3 + 4);
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
H
hjchen2 已提交
383
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
384
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
385
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
386 387 388 389 390 391 392 393
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
          y1.val[0] = vPoolPreq_f32<P>(y0.val[0], y1.val[0]);
          y1.val[1] = vPoolPreq_f32<P>(y0.val[1], y1.val[1]);
          y2.val[0] = vPoolPreq_f32<P>(y0.val[0], y2.val[0]);
          y2.val[1] = vPoolPreq_f32<P>(y0.val[1], y2.val[1]);
H
hjchen2 已提交
394 395
          y1.val[0] = vPoolPostq_f32<P>(y1.val[0], post);
          y1.val[1] = vPoolPostq_f32<P>(y1.val[1], post);
H
hjchen2 已提交
396 397 398 399 400 401
          vst1q_f32(output_ptr1, y1.val[0]);
          vst1_f32(output_ptr1 + 4, vget_low_f32(y1.val[1]));

          x0.val[0] = vld1q_f32(input_ptr4);
          x0.val[1] = vld1q_f32(input_ptr4 + 4);
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
H
hjchen2 已提交
402
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
403
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
404
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
405 406 407 408 409 410 411 412
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);
          y2.val[0] = vPoolPreq_f32<P>(x0.val[0], y2.val[0]);
          y2.val[1] = vPoolPreq_f32<P>(x0.val[1], y2.val[1]);
H
hjchen2 已提交
413 414
          y2.val[0] = vPoolPostq_f32<P>(y2.val[0], post);
          y2.val[1] = vPoolPostq_f32<P>(y2.val[1], post);
H
hjchen2 已提交
415 416 417 418 419 420
          vst1q_f32(output_ptr2, y2.val[0]);
          vst1_f32(output_ptr2 + 4, vget_low_f32(y2.val[1]));

          x0.val[0] = vld1q_f32(input_ptr5);
          x0.val[1] = vld1q_f32(input_ptr5 + 4);
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
H
hjchen2 已提交
421
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
422
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
423
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
424 425 426 427 428 429
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);
H
hjchen2 已提交
430 431
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
          y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
H
hjchen2 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
          vst1q_f32(output_ptr3, y0.val[0]);
          vst1_f32(output_ptr3 + 4, vget_low_f32(y0.val[1]));

          input_ptr0 += 6;
          input_ptr1 += 6;
          input_ptr2 += 6;
          input_ptr3 += 6;
          input_ptr4 += 6;
          input_ptr5 += 6;
          output_ptr0 += 6;
          output_ptr1 += 6;
          output_ptr2 += 6;
          output_ptr3 += 6;
        }
        // remain w
        if (remain >= 4) {
          x0.val[0] = vld1q_f32(input_ptr0);
H
hjchen2 已提交
449
          x0.val[1] = vld1q_f32(input_ptr0 + 4);
H
hjchen2 已提交
450 451 452 453 454 455
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);

          x0.val[0] = vld1q_f32(input_ptr1);
H
hjchen2 已提交
456
          x0.val[1] = vld1q_f32(input_ptr1 + 4);
H
hjchen2 已提交
457 458 459 460 461 462 463
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          y1.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y0.val[0] = vPoolPreq_f32<P>(y1.val[0], y0.val[0]);

          x0.val[0] = vld1q_f32(input_ptr2);
H
hjchen2 已提交
464
          x0.val[1] = vld1q_f32(input_ptr2 + 4);
H
hjchen2 已提交
465 466 467 468 469 470
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          y2.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y1.val[0] = vPoolPreq_f32<P>(y2.val[0], y1.val[0]);
          y0.val[0] = vPoolPreq_f32<P>(y2.val[0], y0.val[0]);
H
hjchen2 已提交
471
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
H
hjchen2 已提交
472 473 474
          vst1q_f32(output_ptr0, y0.val[0]);

          x0.val[0] = vld1q_f32(input_ptr3);
H
hjchen2 已提交
475
          x0.val[1] = vld1q_f32(input_ptr3 + 4);
H
hjchen2 已提交
476 477 478 479 480 481
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y1.val[0] = vPoolPreq_f32<P>(y0.val[0], y1.val[0]);
          y2.val[0] = vPoolPreq_f32<P>(y0.val[0], y2.val[0]);
H
hjchen2 已提交
482
          y1.val[0] = vPoolPostq_f32<P>(y1.val[0], post);
H
hjchen2 已提交
483 484 485
          vst1q_f32(output_ptr1, y1.val[0]);

          x0.val[0] = vld1q_f32(input_ptr4);
H
hjchen2 已提交
486
          x0.val[1] = vld1q_f32(input_ptr4 + 4);
H
hjchen2 已提交
487 488 489 490 491 492
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
          y2.val[0] = vPoolPreq_f32<P>(x0.val[0], y2.val[0]);
H
hjchen2 已提交
493
          y2.val[0] = vPoolPostq_f32<P>(y2.val[0], post);
H
hjchen2 已提交
494 495 496
          vst1q_f32(output_ptr2, y2.val[0]);

          x0.val[0] = vld1q_f32(input_ptr5);
H
hjchen2 已提交
497
          x0.val[1] = vld1q_f32(input_ptr5 + 4);
H
hjchen2 已提交
498 499 500 501 502
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
H
hjchen2 已提交
503
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
H
hjchen2 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
          vst1q_f32(output_ptr3, y0.val[0]);

          input_ptr0 += 4;
          input_ptr1 += 4;
          input_ptr2 += 4;
          input_ptr3 += 4;
          input_ptr4 += 4;
          input_ptr5 += 4;
          output_ptr0 += 4;
          output_ptr1 += 4;
          output_ptr2 += 4;
          output_ptr3 += 4;
          remain -= 4;
        }
#endif  // __ARM_NEON__
        for (int r = 0; r < remain; ++r) {
          float m0 = PoolPre<P>(input_ptr0[r], input_ptr0[r + 1]);
          m0 = PoolPre<P>(m0, input_ptr0[r + 2]);
          float m1 = PoolPre<P>(input_ptr1[r], input_ptr1[r + 1]);
          m1 = PoolPre<P>(m1, input_ptr1[r + 2]);
          float m2 = PoolPre<P>(input_ptr2[r], input_ptr2[r + 1]);
          m2 = PoolPre<P>(m2, input_ptr2[r + 2]);
          float m3 = PoolPre<P>(input_ptr3[r], input_ptr3[r + 1]);
          m3 = PoolPre<P>(m3, input_ptr3[r + 2]);
          float m4 = PoolPre<P>(input_ptr4[r], input_ptr4[r + 1]);
          m4 = PoolPre<P>(m4, input_ptr4[r + 2]);
          float m5 = PoolPre<P>(input_ptr5[r], input_ptr5[r + 1]);
          m5 = PoolPre<P>(m5, input_ptr5[r + 2]);

          m0 = PoolPre<P>(PoolPre<P>(m0, m1), m2);
          m1 = PoolPre<P>(PoolPre<P>(m1, m2), m3);
          m2 = PoolPre<P>(PoolPre<P>(m2, m3), m4);
          m3 = PoolPre<P>(PoolPre<P>(m3, m4), m5);
H
hjchen2 已提交
537 538 539 540
          output_ptr0[r] = PoolPost<P>(m0, avg);
          output_ptr1[r] = PoolPost<P>(m1, avg);
          output_ptr2[r] = PoolPost<P>(m2, avg);
          output_ptr3[r] = PoolPost<P>(m3, avg);
H
hjchen2 已提交
541 542 543 544 545 546 547 548 549 550 551 552
        }
      }
      // remain h
      int start_h = valid_h_start + (valid_h & 0xFFFC);
      for (int h = start_h; h < valid_h_end; ++h) {
        const float *input_ptr0 = input_ptr + (h - padding_h) * input_w;
        const float *input_ptr1 = input_ptr0 + input_w;
        const float *input_ptr2 = input_ptr1 + input_w;
        float *output_ptr0 = output_ptr + h * output_w + valid_w_start;
        int remain = output_w_remain;
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
        float32x4x2_t x0, x1, x2, y0;
H
hjchen2 已提交
553
        float32x4_t post = vdupq_n_f32(1.f / 9);
H
hjchen2 已提交
554 555 556 557
        for (int loop = 0; loop < output_w_tiles; ++loop) {
          x0.val[0] = vld1q_f32(input_ptr0);
          x0.val[1] = vld1q_f32(input_ptr0 + 4);
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
H
hjchen2 已提交
558
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
559
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
560
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
561 562 563 564 565 566 567 568
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);

          x0.val[0] = vld1q_f32(input_ptr1);
          x0.val[1] = vld1q_f32(input_ptr1 + 4);
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
H
hjchen2 已提交
569
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
570
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
571
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
572 573
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
H
hjchen2 已提交
574 575
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
H
hjchen2 已提交
576 577 578 579 580 581
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);

          x0.val[0] = vld1q_f32(input_ptr2);
          x0.val[1] = vld1q_f32(input_ptr2 + 4);
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
H
hjchen2 已提交
582
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
583
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
584
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
585 586
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
H
hjchen2 已提交
587 588
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
H
hjchen2 已提交
589 590
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);
H
hjchen2 已提交
591 592
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
          y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
H
hjchen2 已提交
593 594 595 596 597 598 599 600 601 602 603
          vst1q_f32(output_ptr0, y0.val[0]);
          vst1_f32(output_ptr0 + 4, vget_low_f32(y0.val[1]));

          input_ptr0 += 6;
          input_ptr1 += 6;
          input_ptr2 += 6;
          output_ptr0 += 6;
        }
        // remain w
        if (remain >= 4) {
          x0.val[0] = vld1q_f32(input_ptr0);
H
hjchen2 已提交
604
          x0.val[1] = vld1q_f32(input_ptr0 + 4);
H
hjchen2 已提交
605 606 607 608 609 610
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);

          x0.val[0] = vld1q_f32(input_ptr1);
H
hjchen2 已提交
611
          x0.val[1] = vld1q_f32(input_ptr1 + 4);
H
hjchen2 已提交
612 613 614
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
H
hjchen2 已提交
615
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
616 617 618
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);

          x0.val[0] = vld1q_f32(input_ptr2);
H
hjchen2 已提交
619
          x0.val[1] = vld1q_f32(input_ptr2 + 4);
H
hjchen2 已提交
620 621 622
          x1.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
H
hjchen2 已提交
623
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
624
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
H
hjchen2 已提交
625
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
H
hjchen2 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
          vst1q_f32(output_ptr0, y0.val[0]);

          input_ptr0 += 4;
          input_ptr1 += 4;
          input_ptr2 += 4;
          output_ptr0 += 4;
          remain -= 4;
        }
#endif  // __ARM_NEON__
        for (int r = 0; r < remain; ++r) {
          float m0 = PoolPre<P>(input_ptr0[r], input_ptr0[r + 1]);
          m0 = PoolPre<P>(m0, input_ptr0[r + 2]);
          float m1 = PoolPre<P>(input_ptr1[r], input_ptr1[r + 1]);
          m1 = PoolPre<P>(m1, input_ptr1[r + 2]);
          float m2 = PoolPre<P>(input_ptr2[r], input_ptr2[r + 1]);
          m2 = PoolPre<P>(m2, input_ptr2[r + 2]);

          m0 = PoolPre<P>(PoolPre<P>(m0, m1), m2);
H
hjchen2 已提交
644
          output_ptr0[r] = PoolPost<P>(m0, avg);
H
hjchen2 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
        }
      }
    }
  }
};

template <PoolingType P>
struct Pooling3x3<P, 2> {
  inline void operator()(const framework::Tensor &input,
                         const std::vector<int> &paddings,
                         framework::Tensor *output) {
    const float *input_data = input.data<float>();
    float *output_data = output->mutable_data<float>();
    int input_h = input.dims()[2];
    int input_w = input.dims()[3];
    int output_h = output->dims()[2];
    int output_w = output->dims()[3];
    int padding_h = paddings[0];
    int padding_w = paddings[1];
    int image_size = input_h * input_w;
    int out_image_size = output_h * output_w;
    int valid_h_start = (padding_h + 1) / 2;
H
hjchen2 已提交
667 668
    int valid_h = (input_h - 3) / 2 + 1;
    int valid_h_end = valid_h_start + valid_h;
H
hjchen2 已提交
669
    int valid_w_start = (padding_w + 1) / 2;
H
hjchen2 已提交
670 671
    int valid_w = (input_w - 3) / 2 + 1;
    int valid_w_end = valid_w_start + valid_w;
H
hjchen2 已提交
672
    float avg = 1.f / 9;
H
hjchen2 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719

    #pragma omp parallel for
    for (int c = 0; c < output->dims()[1]; ++c) {
      const float *input_ptr = input_data + c * image_size;
      float *output_ptr = output_data + c * out_image_size;
      // top
      for (int h = 0; h < valid_h_start; ++h) {
        Pooling3x3NormalRow<P, 2>(input_ptr, h, input_h, input_w, padding_h,
                                  padding_w, output_w, output_ptr);
      }
      // left
      for (int w = 0; w < valid_w_start; ++w) {
        Pooling3x3ValidCol<P, 2>(input_ptr, valid_h_start, valid_h_end, w,
                                 input_h, input_w, padding_h, padding_w,
                                 output_w, output_ptr);
      }
      // right
      for (int w = valid_w_end; w < output_w; ++w) {
        Pooling3x3ValidCol<P, 2>(input_ptr, valid_h_start, valid_h_end, w,
                                 input_h, input_w, padding_h, padding_w,
                                 output_w, output_ptr);
      }
      // bottom
      for (int h = valid_h_end; h < output_h; ++h) {
        Pooling3x3NormalRow<P, 2>(input_ptr, h, input_h, input_w, padding_h,
                                  padding_w, output_w, output_ptr);
      }
      // valid
      int input_w_start = 2 * valid_w_start - padding_w;
      int output_w_tiles = valid_w / 6;
      int output_w_remain = valid_w - output_w_tiles * 6;
      for (int h = valid_h_start; h < valid_h_end - 2; h += 3) {
        size_t offset = (2 * h - padding_h) * input_w + input_w_start;
        const float *input_ptr0 = input_ptr + offset;
        const float *input_ptr1 = input_ptr0 + input_w;
        const float *input_ptr2 = input_ptr1 + input_w;
        const float *input_ptr3 = input_ptr2 + input_w;
        const float *input_ptr4 = input_ptr3 + input_w;
        const float *input_ptr5 = input_ptr4 + input_w;
        const float *input_ptr6 = input_ptr5 + input_w;
        float *output_ptr0 = output_ptr + h * output_w + valid_w_start;
        float *output_ptr1 = output_ptr0 + output_w;
        float *output_ptr2 = output_ptr1 + output_w;
        int remain = output_w_remain;
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
        float32x4x2_t x0, x1, x2;
        float32x4x2_t y0, y1, y2;
H
hjchen2 已提交
720
        float32x4_t post = vdupq_n_f32(1.f / 9);
H
hjchen2 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
        for (int loop = 0; loop < output_w_tiles; ++loop) {
          x0 = vld2q_f32(input_ptr0);
          x1 = vld2q_f32(input_ptr0 + 8);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);

          x0 = vld2q_f32(input_ptr1);
          x1 = vld2q_f32(input_ptr1 + 8);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
H
hjchen2 已提交
737 738
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
H
hjchen2 已提交
739 740 741 742 743 744 745 746 747 748 749 750 751
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);

          x0 = vld2q_f32(input_ptr2);
          x1 = vld2q_f32(input_ptr2 + 8);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
          y1.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y1.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(y1.val[0], y0.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(y1.val[1], y0.val[1]);
H
hjchen2 已提交
752 753
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
          y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
H
hjchen2 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
          vst1q_f32(output_ptr0, y0.val[0]);
          vst1_f32(output_ptr0 + 4, vget_low_f32(y0.val[1]));

          x0 = vld2q_f32(input_ptr3);
          x1 = vld2q_f32(input_ptr3 + 8);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
          y1.val[0] = vPoolPreq_f32<P>(x0.val[0], y1.val[0]);
          y1.val[1] = vPoolPreq_f32<P>(x0.val[1], y1.val[1]);

          x0 = vld2q_f32(input_ptr4);
          x1 = vld2q_f32(input_ptr4 + 8);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
          y1.val[0] = vPoolPreq_f32<P>(y0.val[0], y1.val[0]);
          y1.val[1] = vPoolPreq_f32<P>(y0.val[1], y1.val[1]);
H
hjchen2 已提交
778 779
          y1.val[0] = vPoolPostq_f32<P>(y1.val[0], post);
          y1.val[1] = vPoolPostq_f32<P>(y1.val[1], post);
H
hjchen2 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
          vst1q_f32(output_ptr1, y1.val[0]);
          vst1_f32(output_ptr1 + 4, vget_low_f32(y1.val[1]));

          x0 = vld2q_f32(input_ptr5);
          x1 = vld2q_f32(input_ptr5 + 8);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);

          x0 = vld2q_f32(input_ptr6);
          x1 = vld2q_f32(input_ptr6 + 8);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);
H
hjchen2 已提交
804 805
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
          y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
H
hjchen2 已提交
806 807 808 809 810 811 812 813 814
          vst1q_f32(output_ptr2, y0.val[0]);
          vst1_f32(output_ptr2 + 4, vget_low_f32(y0.val[1]));

          input_ptr0 += 12;
          input_ptr1 += 12;
          input_ptr2 += 12;
          input_ptr3 += 12;
          input_ptr4 += 12;
          input_ptr5 += 12;
H
hjchen2 已提交
815
          input_ptr6 += 12;
H
hjchen2 已提交
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
          output_ptr0 += 6;
          output_ptr1 += 6;
          output_ptr2 += 6;
        }
        // remain w
        if (remain >= 4) {
          x0 = vld2q_f32(input_ptr0);
          x1.val[0] = vdupq_n_f32(input_ptr0[8]);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);

          x0 = vld2q_f32(input_ptr1);
          x1.val[0] = vdupq_n_f32(input_ptr1[8]);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
H
hjchen2 已提交
832
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
833 834 835 836 837 838 839 840
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);

          x0 = vld2q_f32(input_ptr2);
          x1.val[0] = vdupq_n_f32(input_ptr2[8]);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          y1.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y0.val[0] = vPoolPreq_f32<P>(y1.val[0], y0.val[0]);
H
hjchen2 已提交
841
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
H
hjchen2 已提交
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
          vst1q_f32(output_ptr0, y0.val[0]);

          x0 = vld2q_f32(input_ptr3);
          x1.val[0] = vdupq_n_f32(input_ptr3[8]);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y1.val[0] = vPoolPreq_f32<P>(x0.val[0], y1.val[0]);

          x0 = vld2q_f32(input_ptr4);
          x1.val[0] = vdupq_n_f32(input_ptr4[8]);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y1.val[0] = vPoolPreq_f32<P>(y0.val[0], y1.val[0]);
H
hjchen2 已提交
857
          y1.val[0] = vPoolPostq_f32<P>(y1.val[0], post);
H
hjchen2 已提交
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
          vst1q_f32(output_ptr1, y1.val[0]);

          x0 = vld2q_f32(input_ptr5);
          x1.val[0] = vdupq_n_f32(input_ptr5[8]);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);

          x0 = vld2q_f32(input_ptr6);
          x1.val[0] = vdupq_n_f32(input_ptr6[8]);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
H
hjchen2 已提交
873
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
H
hjchen2 已提交
874 875 876 877 878 879 880 881
          vst1q_f32(output_ptr2, y0.val[0]);

          input_ptr0 += 8;
          input_ptr1 += 8;
          input_ptr2 += 8;
          input_ptr3 += 8;
          input_ptr4 += 8;
          input_ptr5 += 8;
H
hjchen2 已提交
882
          input_ptr6 += 8;
H
hjchen2 已提交
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
          output_ptr0 += 4;
          output_ptr1 += 4;
          output_ptr2 += 4;
          remain -= 4;
        }
#endif  // __ARM_NEON__
        for (int r = 0; r < remain; ++r) {
          float m0 = PoolPre<P>(input_ptr0[2 * r], input_ptr0[2 * r + 1]);
          m0 = PoolPre<P>(m0, input_ptr0[2 * r + 2]);
          float m1 = PoolPre<P>(input_ptr1[2 * r], input_ptr1[2 * r + 1]);
          m1 = PoolPre<P>(m1, input_ptr1[2 * r + 2]);
          float m2 = PoolPre<P>(input_ptr2[2 * r], input_ptr2[2 * r + 1]);
          m2 = PoolPre<P>(m2, input_ptr2[2 * r + 2]);
          float m3 = PoolPre<P>(input_ptr3[2 * r], input_ptr3[2 * r + 1]);
          m3 = PoolPre<P>(m3, input_ptr3[2 * r + 2]);
          float m4 = PoolPre<P>(input_ptr4[2 * r], input_ptr4[2 * r + 1]);
          m4 = PoolPre<P>(m4, input_ptr4[2 * r + 2]);
          float m5 = PoolPre<P>(input_ptr5[2 * r], input_ptr5[2 * r + 1]);
          m5 = PoolPre<P>(m5, input_ptr5[2 * r + 2]);
          float m6 = PoolPre<P>(input_ptr6[2 * r], input_ptr6[2 * r + 1]);
          m6 = PoolPre<P>(m6, input_ptr6[2 * r + 2]);

          m0 = PoolPre<P>(PoolPre<P>(m0, m1), m2);
          m1 = PoolPre<P>(PoolPre<P>(m2, m3), m4);
          m2 = PoolPre<P>(PoolPre<P>(m4, m5), m6);
H
hjchen2 已提交
908 909 910
          output_ptr0[r] = PoolPost<P>(m0, avg);
          output_ptr1[r] = PoolPost<P>(m1, avg);
          output_ptr2[r] = PoolPost<P>(m2, avg);
H
hjchen2 已提交
911 912 913 914 915 916 917 918 919 920 921 922 923
        }
      }
      // remain h
      int start_h = valid_h_start + valid_h / 3 * 3;
      for (int h = start_h; h < valid_h_end; ++h) {
        size_t offset = (2 * h - padding_h) * input_w + input_w_start;
        const float *input_ptr0 = input_ptr + offset;
        const float *input_ptr1 = input_ptr0 + input_w;
        const float *input_ptr2 = input_ptr1 + input_w;
        float *output_ptr0 = output_ptr + h * output_w + valid_w_start;
        int remain = output_w_remain;
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
        float32x4x2_t x0, x1, x2, y0;
H
hjchen2 已提交
924
        float32x4_t post = vdupq_n_f32(1.f / 9);
H
hjchen2 已提交
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
        for (int loop = 0; loop < output_w_tiles; ++loop) {
          x0 = vld2q_f32(input_ptr0);
          x1 = vld2q_f32(input_ptr0 + 8);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);

          x0 = vld2q_f32(input_ptr1);
          x1 = vld2q_f32(input_ptr1 + 8);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
H
hjchen2 已提交
941 942
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
H
hjchen2 已提交
943 944 945 946 947 948 949 950 951 952 953 954 955
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);

          x0 = vld2q_f32(input_ptr2);
          x1 = vld2q_f32(input_ptr2 + 8);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
          y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);
H
hjchen2 已提交
956 957
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
          y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
H
hjchen2 已提交
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
          vst1q_f32(output_ptr0, y0.val[0]);
          vst1_f32(output_ptr0 + 4, vget_low_f32(y0.val[1]));

          input_ptr0 += 12;
          input_ptr1 += 12;
          input_ptr2 += 12;
          output_ptr0 += 6;
        }
        // remain w
        if (remain >= 4) {
          x0 = vld2q_f32(input_ptr0);
          x1.val[0] = vdupq_n_f32(input_ptr0[8]);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);

          x0 = vld2q_f32(input_ptr1);
          x1.val[0] = vdupq_n_f32(input_ptr1[8]);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
H
hjchen2 已提交
978
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
979 980 981 982 983 984 985 986
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);

          x0 = vld2q_f32(input_ptr2);
          x1.val[0] = vdupq_n_f32(input_ptr2[8]);
          x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
H
hjchen2 已提交
987
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
H
hjchen2 已提交
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
          vst1q_f32(output_ptr0, y0.val[0]);

          input_ptr0 += 8;
          input_ptr1 += 8;
          input_ptr2 += 8;
          output_ptr0 += 4;
          remain -= 4;
        }
#endif  // __ARM_NEON__
        for (int r = 0; r < remain; ++r) {
          float m0 = PoolPre<P>(input_ptr0[2 * r], input_ptr0[2 * r + 1]);
          m0 = PoolPre<P>(m0, input_ptr0[2 * r + 2]);
          float m1 = PoolPre<P>(input_ptr1[2 * r], input_ptr1[2 * r + 1]);
          m1 = PoolPre<P>(m1, input_ptr1[2 * r + 2]);
          float m2 = PoolPre<P>(input_ptr2[2 * r], input_ptr2[2 * r + 1]);
          m2 = PoolPre<P>(m2, input_ptr2[2 * r + 2]);

          m0 = PoolPre<P>(PoolPre<P>(m0, m1), m2);
H
hjchen2 已提交
1006
          output_ptr0[r] = PoolPost<P>(m0, avg);
H
hjchen2 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
        }
      }
    }
  }
};

template struct Pooling3x3<Max, 1>;
template struct Pooling3x3<Avg, 1>;
template struct Pooling3x3<Max, 2>;
template struct Pooling3x3<Avg, 2>;

}  // namespace math
}  // namespace operators
}  // namespace paddle_mobile

#endif  // POOL_OP