pooling3x3.cpp 60.1 KB
Newer Older
H
hjchen2 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#ifdef POOL_OP

#if defined(__ARM_NEON) || defined(__ARM_NEON__)
H
hjchen2 已提交
18

H
hjchen2 已提交
19
#include <arm_neon.h>
H
hjchen2 已提交
20
#include "operators/math/pooling.h"
H
hjchen2 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

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
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]);
  }
};
H
hjchen2 已提交
78 79 80 81 82 83 84 85 86 87 88 89

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;

  float *output_ptr = output + h_output * output_w;
H
hjchen2 已提交
90 91 92 93 94 95 96 97 98
  if (h_end - h_start <= 0) {
    memset(output_ptr, 0, output_w * sizeof(float));
    return;
  }

  const int valid_w_start = (padding_w + Stride - 1) / Stride;
  const int valid_w_end = (input_w + padding_w - 3) / Stride + 1;
  const int valid_w = valid_w_end - valid_w_start;

H
hjchen2 已提交
99 100 101
  // border left
  POOLING3X3_NORMAL_BORDER(0, valid_w_start)
  // middle
H
hjchen2 已提交
102
  int output_tiles = (valid_w_end - valid_w_start) / 6;
H
hjchen2 已提交
103
  int output_tiles_w = output_tiles * 6;
H
hjchen2 已提交
104 105 106
  Pooling3x3NormalRowLoadInput<P, Stride> PoolingCompute;
  float32x4x2_t x0, x1, x2, y0;
  float32x4_t post = vdupq_n_f32(1.f / (3 * (h_end - h_start)));
H
hjchen2 已提交
107
  for (int w = 0; w < output_tiles_w; w += 6) {
H
hjchen2 已提交
108 109 110 111 112 113 114 115 116 117 118 119
    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]));
  }
H
hjchen2 已提交
120 121 122 123 124 125 126
  int remain = valid_w - output_tiles_w;
  if (remain > 0) {
    int remain_start = valid_w_start + output_tiles_w;
    int input_w_offset = remain_start * Stride - padding_w;
    float *output_ptr0 = output_ptr + remain_start;
    y0.val[0] = vPoolInitq_f32<P>();
    y0.val[1] = vPoolInitq_f32<P>();
H
hjchen2 已提交
127
    for (int h_in = h_start; h_in < h_end; ++h_in) {
H
hjchen2 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
      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);
    switch (remain) {
      case 1:
        vst1q_lane_f32(output_ptr0, y0.val[0], 0);
        break;
      case 2:
        vst1_f32(output_ptr0, vget_low_f32(y0.val[0]));
        break;
      case 3:
        vst1_f32(output_ptr0, vget_low_f32(y0.val[0]));
        vst1q_lane_f32(output_ptr0 + 2, y0.val[0], 2);
        break;
      case 4:
        vst1q_f32(output_ptr0, y0.val[0]);
        break;
      case 5:
        vst1q_f32(output_ptr0, y0.val[0]);
        vst1q_lane_f32(output_ptr0 + 4, y0.val[1], 0);
        break;
H
hjchen2 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    }
  }
  // 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 已提交
172 173
    int valid_h = input_h - 2;
    int valid_h_end = valid_h_start + valid_h;
H
hjchen2 已提交
174
    int valid_w_start = padding_w;
H
hjchen2 已提交
175 176
    int valid_w = input_w - 2;
    int valid_w_end = valid_w_start + valid_w;
H
hjchen2 已提交
177

178 179 180 181 182 183 184 185 186 187
    #pragma omp parallel for collapse(2)
    for (int batch = 0; batch < output->dims()[0]; ++batch) {
      for (int c = 0; c < output->dims()[1]; ++c) {
        int channel = batch * output->dims()[1] + c;
        const float *input_ptr = input_data + channel * image_size;
        float *output_ptr = output_data + channel * 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);
H
hjchen2 已提交
188
        }
189 190 191 192 193 194 195 196 197 198
        // 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;
H
hjchen2 已提交
199
          float *output_ptr0 = output_ptr + h * output_w;
200 201 202
          float *output_ptr1 = output_ptr0 + output_w;
          float *output_ptr2 = output_ptr1 + output_w;
          float *output_ptr3 = output_ptr2 + output_w;
H
hjchen2 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
          // pad left
          if (padding_w) {
            float32x2_t row0 = vld1_f32(input_ptr0);
            float32x2_t row1 = vld1_f32(input_ptr1);
            float32x2_t row2 = vld1_f32(input_ptr2);
            float32x2_t row3 = vld1_f32(input_ptr3);
            float32x2_t row4 = vld1_f32(input_ptr4);
            float32x2_t row5 = vld1_f32(input_ptr5);
            float32x2_t pad0 = vPoolInit_f32<P>();
            float32x2_t acc0, acc1, acc2, acc3, acc12, acc34, post;
            for (int w = valid_w_start - 1; w >= 0; --w) {
              int padding = padding_w - w;
              if (padding >= 3) {
                output_ptr0[w] = 0.f;
                output_ptr1[w] = 0.f;
                output_ptr2[w] = 0.f;
                output_ptr3[w] = 0.f;
              } else {
                post = vdup_n_f32(1.f / (3 * (3 - padding)));
                acc12 = vPoolPre_f32<P>(row1, row2);
                acc34 = vPoolPre_f32<P>(row3, row4);
                acc0 = vPoolPre_f32<P>(row0, acc12);
                acc1 = vPoolPre_f32<P>(row3, acc12);
                acc2 = vPoolPre_f32<P>(row2, acc34);
                acc3 = vPoolPre_f32<P>(row5, acc34);
                acc0 = vpPoolPre_f32<P>(acc0, acc0);
                acc1 = vpPoolPre_f32<P>(acc1, acc1);
                acc2 = vpPoolPre_f32<P>(acc2, acc2);
                acc3 = vpPoolPre_f32<P>(acc3, acc3);
                acc0 = vPoolPost_f32<P>(acc0, post);
                acc1 = vPoolPost_f32<P>(acc1, post);
                acc2 = vPoolPost_f32<P>(acc2, post);
                acc3 = vPoolPost_f32<P>(acc3, post);
                vst1_lane_f32(output_ptr0 + w, acc0, 0);
                vst1_lane_f32(output_ptr1 + w, acc1, 0);
                vst1_lane_f32(output_ptr2 + w, acc2, 0);
                vst1_lane_f32(output_ptr3 + w, acc3, 0);
                row0 = vext_f32(pad0, row0, 1);
                row1 = vext_f32(pad0, row1, 1);
                row2 = vext_f32(pad0, row2, 1);
                row3 = vext_f32(pad0, row3, 1);
                row4 = vext_f32(pad0, row4, 1);
                row5 = vext_f32(pad0, row5, 1);
              }
            }
            output_ptr0 += valid_w_start;
            output_ptr1 += valid_w_start;
            output_ptr2 += valid_w_start;
            output_ptr3 += valid_w_start;
          }
          // valid
254 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 284 285 286 287 288 289 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 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
          float32x4x2_t x0, x1, x2;
          float32x4x2_t y0, y1, y2;
          float32x4_t post = vdupq_n_f32(1.f / 9);
          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);
            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);
            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);
            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);
            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);
            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);
            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], post);
            y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
            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);
            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);
            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], post);
            y1.val[1] = vPoolPostq_f32<P>(y1.val[1], post);
            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);
            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);
            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], post);
            y2.val[1] = vPoolPostq_f32<P>(y2.val[1], post);
            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);
            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);
            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], post);
            y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
            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 width
H
hjchen2 已提交
368 369
          if (output_w_remain > 0) {
            float32x4x2_t y3;
370 371 372
            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 已提交
373
            x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
374
            x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
375
            x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
376
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
H
hjchen2 已提交
377
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
378
            y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
379
            y0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
380 381 382 383

            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 已提交
384
            x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
385
            x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
386
            x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
387
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
H
hjchen2 已提交
388
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
389
            y1.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
390
            y1.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
391
            y0.val[0] = vPoolPreq_f32<P>(y1.val[0], y0.val[0]);
H
hjchen2 已提交
392
            y0.val[1] = vPoolPreq_f32<P>(y1.val[1], y0.val[1]);
393 394 395 396

            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 已提交
397
            x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
398
            x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
399
            x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
400
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
H
hjchen2 已提交
401
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
402
            y2.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
403
            y2.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
404
            y1.val[0] = vPoolPreq_f32<P>(y2.val[0], y1.val[0]);
H
hjchen2 已提交
405
            y1.val[1] = vPoolPreq_f32<P>(y2.val[1], y1.val[1]);
406
            y0.val[0] = vPoolPreq_f32<P>(y2.val[0], y0.val[0]);
H
hjchen2 已提交
407
            y0.val[1] = vPoolPreq_f32<P>(y2.val[1], y0.val[1]);
408
            y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
H
hjchen2 已提交
409
            y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
410 411 412 413

            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 已提交
414
            x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
415
            x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
416
            x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
417
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
H
hjchen2 已提交
418 419 420 421 422 423 424
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
            y3.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
            y3.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
            y1.val[0] = vPoolPreq_f32<P>(y3.val[0], y1.val[0]);
            y1.val[1] = vPoolPreq_f32<P>(y3.val[1], y1.val[1]);
            y2.val[0] = vPoolPreq_f32<P>(y3.val[0], y2.val[0]);
            y2.val[1] = vPoolPreq_f32<P>(y3.val[1], y2.val[1]);
425
            y1.val[0] = vPoolPostq_f32<P>(y1.val[0], post);
H
hjchen2 已提交
426
            y1.val[1] = vPoolPostq_f32<P>(y1.val[1], post);
427 428 429 430

            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 已提交
431
            x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
432
            x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
433
            x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
434
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
H
hjchen2 已提交
435
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
436
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
437 438 439
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
            y3.val[0] = vPoolPreq_f32<P>(x0.val[0], y3.val[0]);
            y3.val[1] = vPoolPreq_f32<P>(x0.val[1], y3.val[1]);
440
            y2.val[0] = vPoolPreq_f32<P>(x0.val[0], y2.val[0]);
H
hjchen2 已提交
441
            y2.val[1] = vPoolPreq_f32<P>(x0.val[1], y2.val[1]);
442
            y2.val[0] = vPoolPostq_f32<P>(y2.val[0], post);
H
hjchen2 已提交
443
            y2.val[1] = vPoolPostq_f32<P>(y2.val[1], post);
444 445 446 447

            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 已提交
448
            x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
449
            x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
450
            x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
451
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
H
hjchen2 已提交
452
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
453
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
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
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
            y3.val[0] = vPoolPreq_f32<P>(x0.val[0], y3.val[0]);
            y3.val[1] = vPoolPreq_f32<P>(x0.val[1], y3.val[1]);
            y3.val[0] = vPoolPostq_f32<P>(y3.val[0], post);
            y3.val[1] = vPoolPostq_f32<P>(y3.val[1], post);

            switch (output_w_remain) {
              case 1:
                vst1q_lane_f32(output_ptr0, y0.val[0], 0);
                vst1q_lane_f32(output_ptr1, y1.val[0], 0);
                vst1q_lane_f32(output_ptr2, y2.val[0], 0);
                vst1q_lane_f32(output_ptr3, y3.val[0], 0);
                break;
              case 2:
                vst1_f32(output_ptr0, vget_low_f32(y0.val[0]));
                vst1_f32(output_ptr1, vget_low_f32(y1.val[0]));
                vst1_f32(output_ptr2, vget_low_f32(y2.val[0]));
                vst1_f32(output_ptr3, vget_low_f32(y3.val[0]));
                break;
              case 3:
                vst1_f32(output_ptr0, vget_low_f32(y0.val[0]));
                vst1_f32(output_ptr1, vget_low_f32(y1.val[0]));
                vst1_f32(output_ptr2, vget_low_f32(y2.val[0]));
                vst1_f32(output_ptr3, vget_low_f32(y3.val[0]));
                vst1q_lane_f32(output_ptr0 + 2, y0.val[0], 2);
                vst1q_lane_f32(output_ptr1 + 2, y1.val[0], 2);
                vst1q_lane_f32(output_ptr2 + 2, y2.val[0], 2);
                vst1q_lane_f32(output_ptr3 + 2, y3.val[0], 2);
                break;
              case 4:
                vst1q_f32(output_ptr0, y0.val[0]);
                vst1q_f32(output_ptr1, y1.val[0]);
                vst1q_f32(output_ptr2, y2.val[0]);
                vst1q_f32(output_ptr3, y3.val[0]);
                break;
              case 5:
                vst1q_f32(output_ptr0, y0.val[0]);
                vst1q_f32(output_ptr1, y1.val[0]);
                vst1q_f32(output_ptr2, y2.val[0]);
                vst1q_f32(output_ptr3, y3.val[0]);
                vst1q_lane_f32(output_ptr0 + 4, y0.val[1], 0);
                vst1q_lane_f32(output_ptr1 + 4, y1.val[1], 0);
                vst1q_lane_f32(output_ptr2 + 4, y2.val[1], 0);
                vst1q_lane_f32(output_ptr3 + 4, y3.val[1], 0);
                break;
            }
            input_ptr0 += output_w_remain;
            input_ptr1 += output_w_remain;
            input_ptr2 += output_w_remain;
            input_ptr3 += output_w_remain;
            input_ptr4 += output_w_remain;
            input_ptr5 += output_w_remain;
            output_ptr0 += output_w_remain;
            output_ptr1 += output_w_remain;
            output_ptr2 += output_w_remain;
            output_ptr3 += output_w_remain;
510
          }
H
hjchen2 已提交
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 558 559
          // pad right
          if (padding_w) {
            float32x2_t row0 = vld1_f32(input_ptr0);
            float32x2_t row1 = vld1_f32(input_ptr1);
            float32x2_t row2 = vld1_f32(input_ptr2);
            float32x2_t row3 = vld1_f32(input_ptr3);
            float32x2_t row4 = vld1_f32(input_ptr4);
            float32x2_t row5 = vld1_f32(input_ptr5);
            float32x2_t pad0 = vPoolInit_f32<P>();
            float32x2_t acc0, acc1, acc2, acc3, acc12, acc34, post;
            for (int w = valid_w_end; w < output_w; ++w) {
              int padding = w + 3 - (padding_w + input_w);
              if (padding >= 3) {
                *output_ptr0 = 0.f;
                *output_ptr1 = 0.f;
                *output_ptr2 = 0.f;
                *output_ptr3 = 0.f;
              } else {
                post = vdup_n_f32(1.f / (3 * (3 - padding)));
                acc12 = vPoolPre_f32<P>(row1, row2);
                acc34 = vPoolPre_f32<P>(row3, row4);
                acc0 = vPoolPre_f32<P>(row0, acc12);
                acc1 = vPoolPre_f32<P>(row3, acc12);
                acc2 = vPoolPre_f32<P>(row2, acc34);
                acc3 = vPoolPre_f32<P>(row5, acc34);
                acc0 = vpPoolPre_f32<P>(acc0, acc0);
                acc1 = vpPoolPre_f32<P>(acc1, acc1);
                acc2 = vpPoolPre_f32<P>(acc2, acc2);
                acc3 = vpPoolPre_f32<P>(acc3, acc3);
                acc0 = vPoolPost_f32<P>(acc0, post);
                acc1 = vPoolPost_f32<P>(acc1, post);
                acc2 = vPoolPost_f32<P>(acc2, post);
                acc3 = vPoolPost_f32<P>(acc3, post);
                vst1_lane_f32(output_ptr0, acc0, 0);
                vst1_lane_f32(output_ptr1, acc1, 0);
                vst1_lane_f32(output_ptr2, acc2, 0);
                vst1_lane_f32(output_ptr3, acc3, 0);
                row0 = vext_f32(row0, pad0, 1);
                row1 = vext_f32(row1, pad0, 1);
                row2 = vext_f32(row2, pad0, 1);
                row3 = vext_f32(row3, pad0, 1);
                row4 = vext_f32(row4, pad0, 1);
                row5 = vext_f32(row5, pad0, 1);
              }
              output_ptr0++;
              output_ptr1++;
              output_ptr2++;
              output_ptr3++;
            }
560
          }
H
hjchen2 已提交
561
        }
562 563 564 565 566 567
        // remain height
        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;
H
hjchen2 已提交
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
          float *output_ptr0 = output_ptr + h * output_w;
          // pad left
          if (padding_w) {
            float32x2_t row0 = vld1_f32(input_ptr0);
            float32x2_t row1 = vld1_f32(input_ptr1);
            float32x2_t row2 = vld1_f32(input_ptr2);
            float32x2_t pad0 = vPoolInit_f32<P>();
            float32x2_t acc0, post;
            for (int w = valid_w_start - 1; w >= 0; --w) {
              int padding = padding_w - w;
              if (padding >= 3) {
                output_ptr0[w] = 0.f;
              } else {
                post = vdup_n_f32(1.f / (3 * (3 - padding)));
                acc0 = vPoolPre_f32<P>(row0, row1);
                acc0 = vPoolPre_f32<P>(acc0, row2);
                acc0 = vpPoolPre_f32<P>(acc0, acc0);
                acc0 = vPoolPost_f32<P>(acc0, post);
                vst1_lane_f32(output_ptr0 + w, acc0, 0);
                row0 = vext_f32(pad0, row0, 1);
                row1 = vext_f32(pad0, row1, 1);
                row2 = vext_f32(pad0, row2, 1);
              }
            }
            output_ptr0 += valid_w_start;
          }
          // valid
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 636 637 638 639 640 641 642 643 644
          float32x4x2_t x0, x1, x2, y0;
          float32x4_t post = vdupq_n_f32(1.f / 9);
          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);
            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);
            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);
            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);
            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]);

            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);
            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);
            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], post);
            y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
            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 width
H
hjchen2 已提交
645
          if (output_w_remain > 0) {
646 647 648
            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 已提交
649
            x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
650
            x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
651
            x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
652
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
H
hjchen2 已提交
653
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
654
            y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
655
            y0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
656 657 658 659

            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 已提交
660
            x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
661
            x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
662
            x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
663
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
H
hjchen2 已提交
664
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
665
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
666
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
667
            y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
H
hjchen2 已提交
668
            y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);
669 670 671 672

            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 已提交
673
            x1.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
674
            x2.val[0] = vextq_f32(x0.val[0], x0.val[1], 2);
H
hjchen2 已提交
675
            x2.val[1] = vextq_f32(x0.val[1], x0.val[1], 2);
676
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x1.val[0]);
H
hjchen2 已提交
677
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x1.val[1]);
678
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
679
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
680
            y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
H
hjchen2 已提交
681
            y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);
682
            y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
H
hjchen2 已提交
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
            y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
            // restore
            switch (output_w_remain) {
              case 1:
                vst1q_lane_f32(output_ptr0, y0.val[0], 0);
                break;
              case 2:
                vst1_f32(output_ptr0, vget_low_f32(y0.val[0]));
                break;
              case 3:
                vst1_f32(output_ptr0, vget_low_f32(y0.val[0]));
                vst1q_lane_f32(output_ptr0 + 2, y0.val[0], 2);
                break;
              case 4:
                vst1q_f32(output_ptr0, y0.val[0]);
                break;
              case 5:
                vst1q_f32(output_ptr0, y0.val[0]);
                vst1q_lane_f32(output_ptr0 + 4, y0.val[1], 0);
                break;
            }
            input_ptr0 += output_w_remain;
            input_ptr1 += output_w_remain;
            input_ptr2 += output_w_remain;
            output_ptr0 += output_w_remain;
708
          }
H
hjchen2 已提交
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
          // pad right
          if (padding_w) {
            float32x2_t row0 = vld1_f32(input_ptr0);
            float32x2_t row1 = vld1_f32(input_ptr1);
            float32x2_t row2 = vld1_f32(input_ptr2);
            float32x2_t pad0 = vPoolInit_f32<P>();
            float32x2_t acc0, post;
            for (int w = valid_w_end; w < output_w; ++w) {
              int padding = w + 3 - (padding_w + input_w);
              if (padding >= 3) {
                *output_ptr0 = 0.f;
              } else {
                post = vdup_n_f32(1.f / (3 * (3 - padding)));
                acc0 = vPoolPre_f32<P>(row0, row1);
                acc0 = vPoolPre_f32<P>(acc0, row2);
                acc0 = vpPoolPre_f32<P>(acc0, acc0);
                acc0 = vPoolPost_f32<P>(acc0, post);
                vst1_lane_f32(output_ptr0, acc0, 0);
                row0 = vext_f32(row0, pad0, 1);
                row1 = vext_f32(row1, pad0, 1);
                row2 = vext_f32(row2, pad0, 1);
              }
              output_ptr0++;
            }
733
          }
H
hjchen2 已提交
734
        }
H
hjchen2 已提交
735 736 737 738 739
        // pad 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);
        }
H
hjchen2 已提交
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
      }
    }
  }
};

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 已提交
761 762
    int valid_h_end = (input_h + padding_h - 1) / 2;
    int valid_h = valid_h_end - valid_h_start;
H
hjchen2 已提交
763
    int valid_w_start = (padding_w + 1) / 2;
H
hjchen2 已提交
764 765 766 767 768 769 770 771 772 773 774 775 776
    int valid_w_end = (input_w + padding_w - 1) / 2;
    int valid_w = valid_w_end - valid_w_start;

    int padding_height = input_h + 2 * padding_h;
    int padding_width = input_w + 2 * padding_w;
    bool ceil_mode = (((padding_height - 1) / 2) < output_h) ||
                     (((padding_width - 1) / 2) < output_w);
    int padding_b =
        padding_h + (ceil_mode ? 2 * output_h - (padding_height - 1) : 0);
    int padding_r =
        padding_w + (ceil_mode ? 2 * output_w - (padding_width - 1) : 0);
    // for pad left
    int valid_input_w_start = (valid_w_start << 1) - padding_w;
H
hjchen2 已提交
777

778 779 780 781 782 783 784 785 786 787
    #pragma omp parallel for collapse(2)
    for (int batch = 0; batch < output->dims()[0]; ++batch) {
      for (int c = 0; c < output->dims()[1]; ++c) {
        int channel = batch * output->dims()[1] + c;
        const float *input_ptr = input_data + channel * image_size;
        float *output_ptr = output_data + channel * 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);
H
hjchen2 已提交
788
        }
789 790 791 792
        // 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 - 2; h += 3) {
H
hjchen2 已提交
793
          const float *input_ptr0 = input_ptr + (2 * h - padding_h) * input_w;
794 795 796 797 798 799
          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;
H
hjchen2 已提交
800
          float *output_ptr0 = output_ptr + h * output_w;
801 802
          float *output_ptr1 = output_ptr0 + output_w;
          float *output_ptr2 = output_ptr1 + output_w;
H
hjchen2 已提交
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 843 844 845 846 847 848 849 850 851 852
          // pad left
          if (padding_w) {
            float32x2_t row0 = vld1_f32(input_ptr0);
            float32x2_t row1 = vld1_f32(input_ptr1);
            float32x2_t row2 = vld1_f32(input_ptr2);
            float32x2_t row3 = vld1_f32(input_ptr3);
            float32x2_t row4 = vld1_f32(input_ptr4);
            float32x2_t row5 = vld1_f32(input_ptr5);
            float32x2_t row6 = vld1_f32(input_ptr6);
            float32x2_t pad0 = vPoolInit_f32<P>();
            float32x2_t acc0, acc1, acc2, post;
            for (int w = valid_w_start - 1; w >= 0; --w) {
              int padding = padding_w - (w << 1);
              if (padding >= 3) {
                output_ptr0[w] = 0.f;
                output_ptr1[w] = 0.f;
                output_ptr2[w] = 0.f;
              } else {
                post = vdup_n_f32(1.f / (3 * (3 - padding)));
                acc0 = vPoolPre_f32<P>(row0, row1);
                acc1 = vPoolPre_f32<P>(row2, row3);
                acc2 = vPoolPre_f32<P>(row4, row5);
                acc0 = vPoolPre_f32<P>(acc0, row2);
                acc1 = vPoolPre_f32<P>(acc1, row4);
                acc2 = vPoolPre_f32<P>(acc2, row6);
                if (padding == 1) {
                  acc0 = vpPoolPre_f32<P>(acc0, acc0);
                  acc1 = vpPoolPre_f32<P>(acc1, acc1);
                  acc2 = vpPoolPre_f32<P>(acc2, acc2);
                }
                acc0 = vPoolPost_f32<P>(acc0, post);
                acc1 = vPoolPost_f32<P>(acc1, post);
                acc2 = vPoolPost_f32<P>(acc2, post);
                vst1_lane_f32(output_ptr0 + w, acc0, 0);
                vst1_lane_f32(output_ptr1 + w, acc1, 0);
                vst1_lane_f32(output_ptr2 + w, acc2, 0);
              }
            }
            input_ptr0 += valid_input_w_start;
            input_ptr1 += valid_input_w_start;
            input_ptr2 += valid_input_w_start;
            input_ptr3 += valid_input_w_start;
            input_ptr4 += valid_input_w_start;
            input_ptr5 += valid_input_w_start;
            input_ptr6 += valid_input_w_start;
            output_ptr0 += valid_w_start;
            output_ptr1 += valid_w_start;
            output_ptr2 += valid_w_start;
          }
          // valid
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 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 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
          float32x4x2_t x0, x1, x2;
          float32x4x2_t y0, y1, y2;
          float32x4_t post = vdupq_n_f32(1.f / 9);
          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]);
            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_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], post);
            y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
            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], post);
            y1.val[1] = vPoolPostq_f32<P>(y1.val[1], post);
            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], post);
            y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
            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;
            input_ptr6 += 12;
            output_ptr0 += 6;
            output_ptr1 += 6;
            output_ptr2 += 6;
          }
          // remain width
H
hjchen2 已提交
956
          if (output_w_remain > 0) {
957
            x0 = vld2q_f32(input_ptr0);
H
hjchen2 已提交
958
            x1 = vld2q_f32(input_ptr0 + 8);
959
            x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
H
hjchen2 已提交
960
            x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
961
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
H
hjchen2 已提交
962
            x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
963
            y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
964
            y0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
965 966

            x0 = vld2q_f32(input_ptr1);
H
hjchen2 已提交
967
            x1 = vld2q_f32(input_ptr1 + 8);
968
            x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
H
hjchen2 已提交
969
            x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
970
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
H
hjchen2 已提交
971
            x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
972
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
973
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
974
            y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
H
hjchen2 已提交
975
            y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);
976 977

            x0 = vld2q_f32(input_ptr2);
H
hjchen2 已提交
978
            x1 = vld2q_f32(input_ptr2 + 8);
979
            x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
H
hjchen2 已提交
980
            x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
981
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
H
hjchen2 已提交
982
            x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
983
            y1.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
984
            y1.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
985
            y0.val[0] = vPoolPreq_f32<P>(y1.val[0], y0.val[0]);
H
hjchen2 已提交
986
            y0.val[1] = vPoolPreq_f32<P>(y1.val[1], y0.val[1]);
987
            y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
H
hjchen2 已提交
988
            y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
989 990

            x0 = vld2q_f32(input_ptr3);
H
hjchen2 已提交
991
            x1 = vld2q_f32(input_ptr3 + 8);
992
            x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
H
hjchen2 已提交
993
            x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
994
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
H
hjchen2 已提交
995
            x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
996
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
997
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
998
            y1.val[0] = vPoolPreq_f32<P>(x0.val[0], y1.val[0]);
H
hjchen2 已提交
999
            y1.val[1] = vPoolPreq_f32<P>(x0.val[1], y1.val[1]);
1000 1001

            x0 = vld2q_f32(input_ptr4);
H
hjchen2 已提交
1002
            x1 = vld2q_f32(input_ptr4 + 8);
1003
            x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
H
hjchen2 已提交
1004
            x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
1005
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
H
hjchen2 已提交
1006 1007 1008 1009 1010
            x0.val[1] = vPoolPreq_f32<P>(x1.val[0], 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]);
1011
            y1.val[0] = vPoolPostq_f32<P>(y1.val[0], post);
H
hjchen2 已提交
1012
            y1.val[1] = vPoolPostq_f32<P>(y1.val[1], post);
1013 1014

            x0 = vld2q_f32(input_ptr5);
H
hjchen2 已提交
1015
            x1 = vld2q_f32(input_ptr5 + 8);
1016
            x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
H
hjchen2 已提交
1017
            x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
1018
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
H
hjchen2 已提交
1019
            x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
1020
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
1021 1022 1023
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.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]);
1024 1025

            x0 = vld2q_f32(input_ptr6);
H
hjchen2 已提交
1026
            x1 = vld2q_f32(input_ptr6 + 8);
1027
            x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
H
hjchen2 已提交
1028
            x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
1029
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
H
hjchen2 已提交
1030
            x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
1031
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
1032 1033 1034 1035 1036
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.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], post);
            y2.val[1] = vPoolPostq_f32<P>(y2.val[1], post);
1037

H
hjchen2 已提交
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
            switch (output_w_remain) {
              case 1:
                vst1q_lane_f32(output_ptr0, y0.val[0], 0);
                vst1q_lane_f32(output_ptr1, y1.val[0], 0);
                vst1q_lane_f32(output_ptr2, y2.val[0], 0);
                break;
              case 2:
                vst1_f32(output_ptr0, vget_low_f32(y0.val[0]));
                vst1_f32(output_ptr1, vget_low_f32(y1.val[0]));
                vst1_f32(output_ptr2, vget_low_f32(y2.val[0]));
                break;
              case 3:
                vst1_f32(output_ptr0, vget_low_f32(y0.val[0]));
                vst1_f32(output_ptr1, vget_low_f32(y1.val[0]));
                vst1_f32(output_ptr2, vget_low_f32(y2.val[0]));
                vst1q_lane_f32(output_ptr0 + 2, y0.val[0], 2);
                vst1q_lane_f32(output_ptr1 + 2, y1.val[0], 2);
                vst1q_lane_f32(output_ptr2 + 2, y2.val[0], 2);
                break;
              case 4:
                vst1q_f32(output_ptr0, y0.val[0]);
                vst1q_f32(output_ptr1, y1.val[0]);
                vst1q_f32(output_ptr2, y2.val[0]);
                break;
              case 5:
                vst1q_f32(output_ptr0, y0.val[0]);
                vst1q_f32(output_ptr1, y1.val[0]);
                vst1q_f32(output_ptr2, y2.val[0]);
                vst1q_lane_f32(output_ptr0 + 4, y0.val[1], 0);
                vst1q_lane_f32(output_ptr1 + 4, y1.val[1], 0);
                vst1q_lane_f32(output_ptr2 + 4, y2.val[1], 0);
                break;
            }
            input_ptr0 += (output_w_remain << 1);
            input_ptr1 += (output_w_remain << 1);
            input_ptr2 += (output_w_remain << 1);
            input_ptr3 += (output_w_remain << 1);
            input_ptr4 += (output_w_remain << 1);
            input_ptr5 += (output_w_remain << 1);
            input_ptr6 += (output_w_remain << 1);
            output_ptr0 += output_w_remain;
            output_ptr1 += output_w_remain;
            output_ptr2 += output_w_remain;
1081
          }
H
hjchen2 已提交
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
          // pad right
          if (padding_r > 0) {
            float32x2_t row0 = vld1_f32(input_ptr0);
            float32x2_t row1 = vld1_f32(input_ptr1);
            float32x2_t row2 = vld1_f32(input_ptr2);
            float32x2_t row3 = vld1_f32(input_ptr3);
            float32x2_t row4 = vld1_f32(input_ptr4);
            float32x2_t row5 = vld1_f32(input_ptr5);
            float32x2_t row6 = vld1_f32(input_ptr6);
            float32x2_t pad0 = vPoolInit_f32<P>();
            float32x2_t acc0, acc1, acc2, post;
            for (int w = valid_w_end; w < output_w; ++w) {
              int padding = 2 * w + 3 - (padding_w + input_w);
              if (padding >= 3) {
                *output_ptr0 = 0.f;
                *output_ptr1 = 0.f;
                *output_ptr2 = 0.f;
              } else {
                post = vdup_n_f32(1.f / (3 * (3 - padding)));
                acc0 = vPoolPre_f32<P>(row0, row1);
                acc1 = vPoolPre_f32<P>(row2, row3);
                acc2 = vPoolPre_f32<P>(row4, row5);
                acc0 = vPoolPre_f32<P>(acc0, row2);
                acc1 = vPoolPre_f32<P>(acc1, row4);
                acc2 = vPoolPre_f32<P>(acc2, row6);
                if (padding == 1) {
                  acc0 = vpPoolPre_f32<P>(acc0, acc0);
                  acc1 = vpPoolPre_f32<P>(acc1, acc1);
                  acc2 = vpPoolPre_f32<P>(acc2, acc2);
                }
                acc0 = vPoolPost_f32<P>(acc0, post);
                acc1 = vPoolPost_f32<P>(acc1, post);
                acc2 = vPoolPost_f32<P>(acc2, post);
                vst1_lane_f32(output_ptr0, acc0, 0);
                vst1_lane_f32(output_ptr1, acc1, 0);
                vst1_lane_f32(output_ptr2, acc2, 0);
              }
              output_ptr0++;
              output_ptr1++;
              output_ptr2++;
            }
1123
          }
H
hjchen2 已提交
1124
        }
1125 1126 1127
        // remain height
        int start_h = valid_h_start + valid_h / 3 * 3;
        for (int h = start_h; h < valid_h_end; ++h) {
H
hjchen2 已提交
1128
          const float *input_ptr0 = input_ptr + (2 * h - padding_h) * input_w;
1129 1130
          const float *input_ptr1 = input_ptr0 + input_w;
          const float *input_ptr2 = input_ptr1 + input_w;
H
hjchen2 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
          float *output_ptr0 = output_ptr + h * output_w;
          // pad left
          if (padding_w) {
            float32x2_t row0 = vld1_f32(input_ptr0);
            float32x2_t row1 = vld1_f32(input_ptr1);
            float32x2_t row2 = vld1_f32(input_ptr2);
            float32x2_t pad0 = vPoolInit_f32<P>();
            float32x2_t acc0, post;
            for (int w = valid_w_start - 1; w >= 0; --w) {
              int padding = padding_w - (w << 1);
              if (padding >= 3) {
                output_ptr0[w] = 0.f;
              } else {
                post = vdup_n_f32(1.f / (3 * (3 - padding)));
                acc0 = vPoolPre_f32<P>(row0, row1);
                acc0 = vPoolPre_f32<P>(acc0, row2);
                if (padding == 1) {
                  acc0 = vpPoolPre_f32<P>(acc0, acc0);
                }
                acc0 = vPoolPost_f32<P>(acc0, post);
                vst1_lane_f32(output_ptr0 + w, acc0, 0);
              }
            }
            input_ptr0 += valid_input_w_start;
            input_ptr1 += valid_input_w_start;
            input_ptr2 += valid_input_w_start;
            output_ptr0 += valid_w_start;
          }
          // valid
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
          float32x4x2_t x0, x1, x2, y0;
          float32x4_t post = vdupq_n_f32(1.f / 9);
          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]);
            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_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], post);
            y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
            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 width
H
hjchen2 已提交
1204
          if (output_w_remain > 0) {
1205
            x0 = vld2q_f32(input_ptr0);
H
hjchen2 已提交
1206
            x1 = vld2q_f32(input_ptr0 + 8);
1207
            x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
H
hjchen2 已提交
1208
            x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
1209
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
H
hjchen2 已提交
1210
            x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
1211
            y0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
1212
            y0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
1213 1214

            x0 = vld2q_f32(input_ptr1);
H
hjchen2 已提交
1215
            x1 = vld2q_f32(input_ptr1 + 8);
1216
            x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
H
hjchen2 已提交
1217
            x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
1218
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
H
hjchen2 已提交
1219
            x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
1220
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
1221
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
1222
            y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
H
hjchen2 已提交
1223
            y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);
1224 1225

            x0 = vld2q_f32(input_ptr2);
H
hjchen2 已提交
1226
            x1 = vld2q_f32(input_ptr2 + 8);
1227
            x2.val[0] = vextq_f32(x0.val[0], x1.val[0], 1);
H
hjchen2 已提交
1228
            x2.val[1] = vextq_f32(x1.val[0], x1.val[0], 1);
1229
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
H
hjchen2 已提交
1230
            x0.val[1] = vPoolPreq_f32<P>(x1.val[0], x1.val[1]);
1231
            x0.val[0] = vPoolPreq_f32<P>(x0.val[0], x2.val[0]);
H
hjchen2 已提交
1232
            x0.val[1] = vPoolPreq_f32<P>(x0.val[1], x2.val[1]);
1233
            y0.val[0] = vPoolPreq_f32<P>(x0.val[0], y0.val[0]);
H
hjchen2 已提交
1234
            y0.val[1] = vPoolPreq_f32<P>(x0.val[1], y0.val[1]);
1235
            y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
H
hjchen2 已提交
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
            y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);
            // restore
            switch (output_w_remain) {
              case 1:
                vst1q_lane_f32(output_ptr0, y0.val[0], 0);
                break;
              case 2:
                vst1_f32(output_ptr0, vget_low_f32(y0.val[0]));
                break;
              case 3:
                vst1_f32(output_ptr0, vget_low_f32(y0.val[0]));
                vst1q_lane_f32(output_ptr0 + 2, y0.val[0], 2);
                break;
              case 4:
                vst1q_f32(output_ptr0, y0.val[0]);
                break;
              case 5:
                vst1q_f32(output_ptr0, y0.val[0]);
                vst1q_lane_f32(output_ptr0 + 4, y0.val[1], 0);
                break;
            }
            input_ptr0 += (output_w_remain << 1);
            input_ptr1 += (output_w_remain << 1);
            input_ptr2 += (output_w_remain << 1);
            output_ptr0 += output_w_remain;
1261
          }
H
hjchen2 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
          // pad right
          if (padding_r > 0) {
            float32x2_t row0 = vld1_f32(input_ptr0);
            float32x2_t row1 = vld1_f32(input_ptr1);
            float32x2_t row2 = vld1_f32(input_ptr2);
            float32x2_t pad0 = vPoolInit_f32<P>();
            float32x2_t acc0, post;
            for (int w = valid_w_end; w < output_w; ++w) {
              int padding = 2 * w + 3 - (padding_w + input_w);
              if (padding >= 3) {
                *output_ptr0 = 0.f;
              } else {
                post = vdup_n_f32(1.f / (3 * (3 - padding)));
                acc0 = vPoolPre_f32<P>(row0, row1);
                acc0 = vPoolPre_f32<P>(acc0, row2);
                if (padding == 1) {
                  acc0 = vpPoolPre_f32<P>(acc0, acc0);
                }
                acc0 = vPoolPost_f32<P>(acc0, post);
                vst1_lane_f32(output_ptr0, acc0, 0);
              }
              output_ptr0++;
            }
1285
          }
H
hjchen2 已提交
1286
        }
H
hjchen2 已提交
1287 1288 1289 1290 1291
        // 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);
        }
H
hjchen2 已提交
1292 1293 1294 1295 1296
      }
    }
  }
};

1297 1298 1299 1300
template struct Pooling3x3<MAX, 1>;
template struct Pooling3x3<AVG, 1>;
template struct Pooling3x3<MAX, 2>;
template struct Pooling3x3<AVG, 2>;
H
hjchen2 已提交
1301 1302 1303 1304 1305

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

H
hjchen2 已提交
1306
#endif  // __ARM_NEON
H
hjchen2 已提交
1307
#endif  // POOL_OP