pooling3x3.cpp 37.5 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 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 161 162 163
/* 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();                               \
  }

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;
  for (int h = h_output; h < h_output_end; ++h) {
    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;
  int valid_w_end = output_w - valid_w_start;

  float *output_ptr = output + h_output * output_w;
  // border left
  POOLING3X3_NORMAL_BORDER(0, valid_w_start)
  // middle
  for (int w = valid_w_start; w < valid_w_end; ++w) {
    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;
    int valid_h_end = output_h - valid_h_start;
    int valid_h = valid_h_end - valid_h_start;
    int valid_w_start = padding_w;
    int valid_w_end = output_w - valid_w_start;
    int valid_w = valid_w_end - valid_w_start;

    #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;
        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 已提交
164
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
165
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
166
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
167 168 169 170 171 172 173 174
          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 已提交
175
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
176
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
177
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
178 179 180 181 182 183 184 185 186 187
          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 已提交
188
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
189
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
190
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
          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]);
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0]);
          y0.val[1] = vPoolPostq_f32<P>(y0.val[1]);
          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 已提交
207
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
208
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
209
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
          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]);
          y1.val[0] = vPoolPostq_f32<P>(y1.val[0]);
          y1.val[1] = vPoolPostq_f32<P>(y1.val[1]);
          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 已提交
226
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
227
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
228
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
          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]);
          y2.val[0] = vPoolPostq_f32<P>(y2.val[0]);
          y2.val[1] = vPoolPostq_f32<P>(y2.val[1]);
          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 已提交
245
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
246
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
247
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
          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]);
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0]);
          y0.val[1] = vPoolPostq_f32<P>(y0.val[1]);
          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 已提交
273
          x0.val[1] = vld1q_f32(input_ptr0 + 4);
H
hjchen2 已提交
274 275 276 277 278 279
          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 已提交
280
          x0.val[1] = vld1q_f32(input_ptr1 + 4);
H
hjchen2 已提交
281 282 283 284 285 286 287
          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 已提交
288
          x0.val[1] = vld1q_f32(input_ptr2 + 4);
H
hjchen2 已提交
289 290 291 292 293 294 295 296 297 298
          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]);
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0]);
          vst1q_f32(output_ptr0, y0.val[0]);

          x0.val[0] = vld1q_f32(input_ptr3);
H
hjchen2 已提交
299
          x0.val[1] = vld1q_f32(input_ptr3 + 4);
H
hjchen2 已提交
300 301 302 303 304 305 306 307 308 309
          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]);
          y1.val[0] = vPoolPostq_f32<P>(y1.val[0]);
          vst1q_f32(output_ptr1, y1.val[0]);

          x0.val[0] = vld1q_f32(input_ptr4);
H
hjchen2 已提交
310
          x0.val[1] = vld1q_f32(input_ptr4 + 4);
H
hjchen2 已提交
311 312 313 314 315 316 317 318 319 320
          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]);
          y2.val[0] = vPoolPostq_f32<P>(y2.val[0]);
          vst1q_f32(output_ptr2, y2.val[0]);

          x0.val[0] = vld1q_f32(input_ptr5);
H
hjchen2 已提交
321
          x0.val[1] = vld1q_f32(input_ptr5 + 4);
H
hjchen2 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
          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]);
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0]);
          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);
          output_ptr0[r] = PoolPost<P>(m0);
          output_ptr1[r] = PoolPost<P>(m1);
          output_ptr2[r] = PoolPost<P>(m2);
          output_ptr3[r] = PoolPost<P>(m3);
        }
      }
      // 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;
        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 已提交
381
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
382
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
383
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
384 385 386 387 388 389 390 391
          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 已提交
392
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
393
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
394
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
395 396
          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 已提交
397 398
          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 已提交
399 400 401 402 403 404
          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 已提交
405
          x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
H
hjchen2 已提交
406
          x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
407
          x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
H
hjchen2 已提交
408 409
          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 已提交
410 411
          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 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
          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] = vPoolPostq_f32<P>(y0.val[0]);
          y0.val[1] = vPoolPostq_f32<P>(y0.val[1]);
          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 已提交
427
          x0.val[1] = vld1q_f32(input_ptr0 + 4);
H
hjchen2 已提交
428 429 430 431 432 433
          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 已提交
434
          x0.val[1] = vld1q_f32(input_ptr1 + 4);
H
hjchen2 已提交
435 436 437
          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 已提交
438
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
439 440 441
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);

          x0.val[0] = vld1q_f32(input_ptr2);
H
hjchen2 已提交
442
          x0.val[1] = vld1q_f32(input_ptr2 + 4);
H
hjchen2 已提交
443 444 445
          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 已提交
446
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 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 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
          y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0]);
          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);
          output_ptr0[r] = PoolPost<P>(m0);
        }
      }
    }
  }
};

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;
    int valid_h_end = output_h - valid_h_start;
    int valid_h = valid_h_end - valid_h_start;
    int valid_w_start = (padding_w + 1) / 2;
    int valid_w_end = output_w - valid_w_start;
    int valid_w = valid_w_end - valid_w_start;

    #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;
        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 已提交
558 559
          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 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
          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]);
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0]);
          y0.val[1] = vPoolPostq_f32<P>(y0.val[1]);
          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]);
          y1.val[0] = vPoolPostq_f32<P>(y1.val[0]);
          y1.val[1] = vPoolPostq_f32<P>(y1.val[1]);
          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]);
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0]);
          y0.val[1] = vPoolPostq_f32<P>(y0.val[1]);
          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 已提交
636
          input_ptr6 += 12;
H
hjchen2 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
          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 已提交
653
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 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
          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]);
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0]);
          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]);
          y1.val[0] = vPoolPostq_f32<P>(y1.val[0]);
          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]);
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0]);
          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 已提交
703
          input_ptr6 += 8;
H
hjchen2 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
          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);
          output_ptr0[r] = PoolPost<P>(m0);
          output_ptr1[r] = PoolPost<P>(m1);
          output_ptr2[r] = PoolPost<P>(m2);
        }
      }
      // 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;
        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 已提交
761 762
          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 已提交
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
          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]);
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0]);
          y0.val[1] = vPoolPostq_f32<P>(y0.val[1]);
          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 已提交
798
          x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
          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]);
          y0.val[0] = vPoolPostq_f32<P>(y0.val[0]);
          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);
          output_ptr0[r] = PoolPost<P>(m0);
        }
      }
    }
  }
};

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