pooling2x2.cpp 32.4 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
/* 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__)

#include <arm_neon.h>
#include "operators/math/pooling.h"

22 23
// TODO(hjchen2): Optimize Pooling2x2NormalRow and use inline assembly

H
hjchen2 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
namespace paddle_mobile {
namespace operators {
namespace math {

#define POOLING2X2_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 + 2;                       \
    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 = 1>
struct Pooling2x2NormalRowLoadInput {
45
  void operator()(const float *input, float32x4_t *x0, float32x4_t *x1) {
H
hjchen2 已提交
46 47 48 49 50 51 52 53 54
    x0[0] = vld1q_f32(input);
    x0[1] = vld1q_f32(input + 4);
    x1[0] = vextq_f32(x0[0], x0[1], 1);
    x1[1] = vextq_f32(x0[1], x0[1], 1);
  }
};

template <PoolingType P>
struct Pooling2x2NormalRowLoadInput<P, 2> {
55
  void operator()(const float *input, float32x4_t *x0, float32x4_t *x1) {
H
hjchen2 已提交
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 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 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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    float32x4x2_t t0 = vld2q_f32(input);
    float32x4x2_t t1 = vld2q_f32(input + 8);
    x0[0] = t0.val[0];
    x0[1] = t1.val[0];
    x1[0] = t0.val[1];
    x1[1] = t1.val[1];
  }
};

template <PoolingType P, int Stride>
inline void Pooling2x2NormalRow(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 + 2;
  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;
  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 - 2) / Stride + 1;
  const int valid_w = valid_w_end - valid_w_start;

  // border left
  POOLING2X2_NORMAL_BORDER(0, valid_w_start)
  // valid w
  Pooling2x2NormalRowLoadInput<P, Stride> load_input;
  int output_tiles = valid_w / 6;
  int output_tiles_w = output_tiles * 6;
  float32x4_t x0[2], x1[2], y0[2];
  float32x4_t post = vdupq_n_f32(1.f / (2 * (h_end - h_start)));
  for (int w = 0; w < output_tiles_w; w += 6) {
    int output_offset = valid_w_start + w;
    int input_w_offset = output_offset * Stride - padding_w;
    y0[0] = vPoolInitq_f32<P>();
    y0[1] = vPoolInitq_f32<P>();
    for (int h_in = h_start; h_in < h_end; ++h_in) {
      load_input(input + h_in * input_w + input_w_offset, x0, x1);
      y0[0] = vPoolPreq_f32<P>(y0[0], x0[0]);
      y0[0] = vPoolPreq_f32<P>(y0[0], x1[0]);
      y0[1] = vPoolPreq_f32<P>(y0[1], x0[1]);
      y0[1] = vPoolPreq_f32<P>(y0[1], x1[1]);
    }
    y0[0] = vPoolPostq_f32<P>(y0[0], post);
    y0[1] = vPoolPostq_f32<P>(y0[1], post);
    vst1q_f32(output_ptr + output_offset, y0[0]);
    vst1_f32(output_ptr + output_offset + 4, vget_low_f32(y0[1]));
  }
  // remain valid w
  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[0] = vPoolInitq_f32<P>();
    y0[1] = vPoolInitq_f32<P>();
    for (int h_in = h_start; h_in < h_end; ++h_in) {
      load_input(input + h_in * input_w + input_w_offset, x0, x1);
      y0[0] = vPoolPreq_f32<P>(y0[0], x0[0]);
      y0[0] = vPoolPreq_f32<P>(y0[0], x1[0]);
      y0[1] = vPoolPreq_f32<P>(y0[1], x0[1]);
      y0[1] = vPoolPreq_f32<P>(y0[1], x1[1]);
    }
    y0[0] = vPoolPostq_f32<P>(y0[0], post);
    y0[1] = vPoolPostq_f32<P>(y0[1], post);
    switch (remain) {
      case 1:
        vst1q_lane_f32(output_ptr0, y0[0], 0);
        break;
      case 2:
        vst1_f32(output_ptr0, vget_low_f32(y0[0]));
        break;
      case 3:
        vst1_f32(output_ptr0, vget_low_f32(y0[0]));
        vst1q_lane_f32(output_ptr0 + 2, y0[0], 2);
        break;
      case 4:
        vst1q_f32(output_ptr0, y0[0]);
        break;
      case 5:
        vst1q_f32(output_ptr0, y0[0]);
        vst1q_lane_f32(output_ptr0 + 4, y0[1], 0);
        break;
    }
  }
  // border right
  POOLING2X2_NORMAL_BORDER(valid_w_end, output_w)
}

template <PoolingType P>
struct Pooling2x2<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 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) {
          Pooling2x2NormalRow<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;
          float *output_ptr0 = output_ptr + h * output_w;
          float *output_ptr1 = output_ptr0 + output_w;
          float *output_ptr2 = output_ptr1 + output_w;
          float *output_ptr3 = output_ptr2 + output_w;
          // pad left
          if (padding_w) {
            for (int w = valid_w_start - 1; w >= 0; --w) {
              int padding = padding_w - w;
              if (padding >= 2) {
                output_ptr0[w] = 0.f;
                output_ptr1[w] = 0.f;
                output_ptr2[w] = 0.f;
                output_ptr3[w] = 0.f;
              } else {
                float acc0 = PoolPre<P>(*input_ptr0, *input_ptr1);
                float acc1 = PoolPre<P>(*input_ptr1, *input_ptr2);
                float acc2 = PoolPre<P>(*input_ptr2, *input_ptr3);
                float acc3 = PoolPre<P>(*input_ptr3, *input_ptr4);
                output_ptr0[w] = PoolPost<P>(acc0, 0.5f);
                output_ptr1[w] = PoolPost<P>(acc1, 0.5f);
                output_ptr2[w] = PoolPost<P>(acc2, 0.5f);
                output_ptr3[w] = PoolPost<P>(acc3, 0.5f);
              }
            }
            output_ptr0 += valid_w_start;
            output_ptr1 += valid_w_start;
            output_ptr2 += valid_w_start;
            output_ptr3 += valid_w_start;
          }
          // valid
          float32x4x2_t x0, x1, q0;
          float32x4x2_t y0, y1;
          float32x4_t post = vdupq_n_f32(0.25f);
          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] = vld1q_f32(input_ptr1);
            x1.val[1] = vld1q_f32(input_ptr1 + 4);
            q0.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
            q0.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
            y0.val[0] = vPoolPreq_f32<P>(x0.val[0], q0.val[0]);
            y0.val[1] = vPoolPreq_f32<P>(x0.val[1], q0.val[1]);

            q0.val[0] = vextq_f32(x1.val[0], x1.val[1], 1);
            q0.val[1] = vextq_f32(x1.val[1], x1.val[1], 1);
            y1.val[0] = vPoolPreq_f32<P>(x1.val[0], q0.val[0]);
            y1.val[1] = vPoolPreq_f32<P>(x1.val[1], q0.val[1]);
            y0.val[0] = vPoolPreq_f32<P>(y0.val[0], y1.val[0]);
            y0.val[1] = vPoolPreq_f32<P>(y0.val[1], y1.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_ptr2);
            x0.val[1] = vld1q_f32(input_ptr2 + 4);
            x1.val[0] = vld1q_f32(input_ptr3);
            x1.val[1] = vld1q_f32(input_ptr3 + 4);
            q0.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
            q0.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
            y0.val[0] = vPoolPreq_f32<P>(x0.val[0], q0.val[0]);
            y0.val[1] = vPoolPreq_f32<P>(x0.val[1], q0.val[1]);
            y1.val[0] = vPoolPreq_f32<P>(y1.val[0], y0.val[0]);
            y1.val[1] = vPoolPreq_f32<P>(y1.val[1], y0.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]));

            q0.val[0] = vextq_f32(x1.val[0], x1.val[1], 1);
            q0.val[1] = vextq_f32(x1.val[1], x1.val[1], 1);
            y1.val[0] = vPoolPreq_f32<P>(x1.val[0], q0.val[0]);
            y1.val[1] = vPoolPreq_f32<P>(x1.val[1], q0.val[1]);
            y0.val[0] = vPoolPreq_f32<P>(y0.val[0], y1.val[0]);
            y0.val[1] = vPoolPreq_f32<P>(y0.val[1], y1.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]));

            x0.val[0] = vld1q_f32(input_ptr4);
            x0.val[1] = vld1q_f32(input_ptr4 + 4);
            q0.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
            q0.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
            y1.val[0] = vPoolPreq_f32<P>(y1.val[0], x0.val[0]);
            y1.val[0] = vPoolPreq_f32<P>(y1.val[0], q0.val[0]);
            y1.val[1] = vPoolPreq_f32<P>(y1.val[1], x0.val[1]);
            y1.val[1] = vPoolPreq_f32<P>(y1.val[1], q0.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_ptr3, y1.val[0]);
            vst1_f32(output_ptr3 + 4, vget_low_f32(y1.val[1]));

            input_ptr0 += 6;
            input_ptr1 += 6;
            input_ptr2 += 6;
            input_ptr3 += 6;
            input_ptr4 += 6;
            output_ptr0 += 6;
            output_ptr1 += 6;
            output_ptr2 += 6;
            output_ptr3 += 6;
          }
          // remain width
          if (output_w_remain > 0) {
            float32x4x2_t y2, y3;
            x0.val[0] = vld1q_f32(input_ptr0);
            x0.val[1] = vld1q_f32(input_ptr0 + 4);
            x1.val[0] = vld1q_f32(input_ptr1);
            x1.val[1] = vld1q_f32(input_ptr1 + 4);
            q0.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
            q0.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
            y0.val[0] = vPoolPreq_f32<P>(x0.val[0], q0.val[0]);
            y0.val[1] = vPoolPreq_f32<P>(x0.val[1], q0.val[1]);

            q0.val[0] = vextq_f32(x1.val[0], x1.val[1], 1);
            q0.val[1] = vextq_f32(x1.val[1], x1.val[1], 1);
            y1.val[0] = vPoolPreq_f32<P>(x1.val[0], q0.val[0]);
            y1.val[1] = vPoolPreq_f32<P>(x1.val[1], q0.val[1]);
            y0.val[0] = vPoolPreq_f32<P>(y0.val[0], y1.val[0]);
            y0.val[1] = vPoolPreq_f32<P>(y0.val[1], y1.val[1]);
            y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
            y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);

            x0.val[0] = vld1q_f32(input_ptr2);
            x0.val[1] = vld1q_f32(input_ptr2 + 4);
            x1.val[0] = vld1q_f32(input_ptr3);
            x1.val[1] = vld1q_f32(input_ptr3 + 4);
            q0.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
            q0.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
            y2.val[0] = vPoolPreq_f32<P>(x0.val[0], q0.val[0]);
            y2.val[1] = vPoolPreq_f32<P>(x0.val[1], q0.val[1]);
            y1.val[0] = vPoolPreq_f32<P>(y1.val[0], y2.val[0]);
            y1.val[1] = vPoolPreq_f32<P>(y1.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);

            q0.val[0] = vextq_f32(x1.val[0], x1.val[1], 1);
            q0.val[1] = vextq_f32(x1.val[1], x1.val[1], 1);
            y3.val[0] = vPoolPreq_f32<P>(x1.val[0], q0.val[0]);
            y3.val[1] = vPoolPreq_f32<P>(x1.val[1], q0.val[1]);
            y2.val[0] = vPoolPreq_f32<P>(y2.val[0], y3.val[0]);
            y2.val[1] = vPoolPreq_f32<P>(y2.val[1], y3.val[1]);
            y2.val[0] = vPoolPostq_f32<P>(y2.val[0], post);
            y2.val[1] = vPoolPostq_f32<P>(y2.val[1], post);

            x0.val[0] = vld1q_f32(input_ptr4);
            x0.val[1] = vld1q_f32(input_ptr4 + 4);
            q0.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
            q0.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
            y3.val[0] = vPoolPreq_f32<P>(y3.val[0], x0.val[0]);
            y3.val[0] = vPoolPreq_f32<P>(y3.val[0], q0.val[0]);
            y3.val[1] = vPoolPreq_f32<P>(y3.val[1], x0.val[1]);
            y3.val[1] = vPoolPreq_f32<P>(y3.val[1], q0.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;
            output_ptr0 += output_w_remain;
            output_ptr1 += output_w_remain;
            output_ptr2 += output_w_remain;
            output_ptr3 += output_w_remain;
          }
          // pad right
          if (padding_w) {
            for (int w = valid_w_end; w < output_w; ++w) {
              int padding = w + 2 - (padding_w + input_w);
              if (padding >= 2) {
                *output_ptr0 = 0.f;
                *output_ptr1 = 0.f;
                *output_ptr2 = 0.f;
                *output_ptr3 = 0.f;
              } else {
                float acc0 = PoolPre<P>(*input_ptr0, *input_ptr1);
                float acc1 = PoolPre<P>(*input_ptr1, *input_ptr2);
                float acc2 = PoolPre<P>(*input_ptr2, *input_ptr3);
                float acc3 = PoolPre<P>(*input_ptr3, *input_ptr4);
                *output_ptr0 = PoolPost<P>(acc0, 0.5f);
                *output_ptr1 = PoolPost<P>(acc1, 0.5f);
                *output_ptr2 = PoolPost<P>(acc2, 0.5f);
                *output_ptr3 = PoolPost<P>(acc3, 0.5f);
              }
              output_ptr0++;
              output_ptr1++;
              output_ptr2++;
              output_ptr3++;
            }
          }
        }
        // remain height
H
hjchen2 已提交
427
        int start_h = valid_h_start + (valid_h & 0xFFFFFFFC);
H
hjchen2 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 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 558 559 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 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 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
        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;
          float *output_ptr0 = output_ptr + h * output_w;
          // pad left
          if (padding_w) {
            for (int w = valid_w_start - 1; w >= 0; --w) {
              int padding = padding_w - w;
              if (padding >= 2) {
                output_ptr0[w] = 0.f;
              } else {
                float acc0 = PoolPre<P>(*input_ptr0, *input_ptr1);
                output_ptr0[w] = PoolPost<P>(acc0, 0.5f);
              }
            }
            output_ptr0 += valid_w_start;
          }
          // valid
          float32x4x2_t x0, x1, q0, y0;
          float32x4_t post = vdupq_n_f32(0.25f);
          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] = vld1q_f32(input_ptr1);
            x1.val[1] = vld1q_f32(input_ptr1 + 4);
            q0.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
            q0.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
            y0.val[0] = vPoolPreq_f32<P>(x0.val[0], q0.val[0]);
            y0.val[1] = vPoolPreq_f32<P>(x0.val[1], q0.val[1]);

            q0.val[0] = vextq_f32(x1.val[0], x1.val[1], 1);
            q0.val[1] = vextq_f32(x1.val[1], x1.val[1], 1);
            y0.val[0] = vPoolPreq_f32<P>(y0.val[0], x1.val[0]);
            y0.val[1] = vPoolPreq_f32<P>(y0.val[1], x1.val[1]);
            y0.val[0] = vPoolPreq_f32<P>(y0.val[0], q0.val[0]);
            y0.val[1] = vPoolPreq_f32<P>(y0.val[1], q0.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;
            output_ptr0 += 6;
          }
          // remain width
          if (output_w_remain > 0) {
            x0.val[0] = vld1q_f32(input_ptr0);
            x0.val[1] = vld1q_f32(input_ptr0 + 4);
            x1.val[0] = vld1q_f32(input_ptr1);
            x1.val[1] = vld1q_f32(input_ptr1 + 4);
            q0.val[0] = vextq_f32(x0.val[0], x0.val[1], 1);
            q0.val[1] = vextq_f32(x0.val[1], x0.val[1], 1);
            y0.val[0] = vPoolPreq_f32<P>(x0.val[0], q0.val[0]);
            y0.val[1] = vPoolPreq_f32<P>(x0.val[1], q0.val[1]);

            q0.val[0] = vextq_f32(x1.val[0], x1.val[1], 1);
            q0.val[1] = vextq_f32(x1.val[1], x1.val[1], 1);
            y0.val[0] = vPoolPreq_f32<P>(y0.val[0], x1.val[0]);
            y0.val[1] = vPoolPreq_f32<P>(y0.val[1], x1.val[1]);
            y0.val[0] = vPoolPreq_f32<P>(y0.val[0], q0.val[0]);
            y0.val[1] = vPoolPreq_f32<P>(y0.val[1], q0.val[1]);
            y0.val[0] = vPoolPostq_f32<P>(y0.val[0], post);
            y0.val[1] = vPoolPostq_f32<P>(y0.val[1], post);

            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;
            output_ptr0 += output_w_remain;
          }
          // pad right
          if (padding_w) {
            for (int w = valid_w_end; w < output_w; ++w) {
              int padding = w + 2 - (padding_w + input_w);
              if (padding >= 2) {
                *output_ptr0 = 0.f;
              } else {
                float acc0 = PoolPre<P>(*input_ptr0, *input_ptr1);
                *output_ptr0 = PoolPost<P>(acc0, 0.5f);
              }
              output_ptr0++;
            }
          }
        }
        // bottom
        for (int h = valid_h_end; h < output_h; ++h) {
          Pooling2x2NormalRow<P, 1>(input_ptr, h, input_h, input_w, padding_h,
                                    padding_w, output_w, output_ptr);
        }
      }
    }
  }
};

template <PoolingType P>
struct Pooling2x2<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 = (input_h + padding_h) / 2;
    int valid_h = valid_h_end - valid_h_start;
    int valid_w_start = (padding_w + 1) / 2;
    int valid_w_end = (input_w + padding_w) / 2;
    int valid_w = valid_w_end - valid_w_start;

    bool ceil_mode = (((input_h + 2 * padding_h) / 2) < output_h) ||
                     (((input_w + 2 * padding_w) / 2) < output_w);
    int padding_b =
        padding_h + (ceil_mode ? 2 * output_h - (input_h + 2 * padding_h) : 0);
    int padding_r =
        padding_w + (ceil_mode ? 2 * output_w - (input_w + 2 * padding_w) : 0);

    #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) {
          Pooling2x2NormalRow<P, 2>(input_ptr, h, input_h, input_w, padding_h,
                                    padding_w, output_w, output_ptr);
        }
        // valid
        int output_w_tiles = valid_w / 4;
        int output_w_remain = valid_w - output_w_tiles * 4;
        for (int h = valid_h_start; h < valid_h_end - 1; h += 2) {
          const float *input_ptr0 = input_ptr + (2 * 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;
          float *output_ptr0 = output_ptr + h * output_w;
          float *output_ptr1 = output_ptr0 + output_w;
          // pad left
          if (padding_w) {
            for (int w = valid_w_start - 1; w >= 0; --w) {
              int padding = padding_w - w * 2;
              if (padding >= 2) {
                output_ptr0[w] = 0.f;
                output_ptr1[w] = 0.f;
              } else {
                float acc0 = PoolPre<P>(*input_ptr0, *input_ptr1);
                float acc1 = PoolPre<P>(*input_ptr2, *input_ptr3);
                output_ptr0[w] = PoolPost<P>(acc0, 0.5f);
                output_ptr1[w] = PoolPost<P>(acc1, 0.5f);
              }
            }
            input_ptr0 += (padding_w & 0x1);
            input_ptr1 += (padding_w & 0x1);
            input_ptr2 += (padding_w & 0x1);
            input_ptr3 += (padding_w & 0x1);
            output_ptr0 += valid_w_start;
            output_ptr1 += valid_w_start;
          }
          // valid
          float32x4x2_t x0, x1, x2, x3;
          float32x4_t y0, y1;
          float32x4_t post = vdupq_n_f32(0.25f);
          for (int loop = 0; loop < output_w_tiles; ++loop) {
            x0 = vld2q_f32(input_ptr0);
            x1 = vld2q_f32(input_ptr1);
            x2 = vld2q_f32(input_ptr2);
            x3 = vld2q_f32(input_ptr3);
            y0 = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
            y1 = vPoolPreq_f32<P>(x2.val[0], x2.val[1]);
            y0 = vPoolPreq_f32<P>(y0, x1.val[0]);
            y1 = vPoolPreq_f32<P>(y1, x3.val[0]);
            y0 = vPoolPreq_f32<P>(y0, x1.val[1]);
            y1 = vPoolPreq_f32<P>(y1, x3.val[1]);
            y0 = vPoolPostq_f32<P>(y0, post);
            y1 = vPoolPostq_f32<P>(y1, post);
            vst1q_f32(output_ptr0, y0);
            vst1q_f32(output_ptr1, y1);

            input_ptr0 += 8;
            input_ptr1 += 8;
            input_ptr2 += 8;
            input_ptr3 += 8;
            output_ptr0 += 4;
            output_ptr1 += 4;
          }
          // remain width
          if (output_w_remain > 0) {
            x0 = vld2q_f32(input_ptr0);
            x1 = vld2q_f32(input_ptr1);
            x2 = vld2q_f32(input_ptr2);
            x3 = vld2q_f32(input_ptr3);
            y0 = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
            y1 = vPoolPreq_f32<P>(x2.val[0], x2.val[1]);
            y0 = vPoolPreq_f32<P>(y0, x1.val[0]);
            y1 = vPoolPreq_f32<P>(y1, x3.val[0]);
            y0 = vPoolPreq_f32<P>(y0, x1.val[1]);
            y1 = vPoolPreq_f32<P>(y1, x3.val[1]);
            y0 = vPoolPostq_f32<P>(y0, post);
            y1 = vPoolPostq_f32<P>(y1, post);

            switch (output_w_remain) {
              case 1:
                vst1q_lane_f32(output_ptr0, y0, 0);
                vst1q_lane_f32(output_ptr1, y1, 0);
                break;
              case 2:
                vst1_f32(output_ptr0, vget_low_f32(y0));
                vst1_f32(output_ptr1, vget_low_f32(y1));
                break;
              case 3:
                vst1_f32(output_ptr0, vget_low_f32(y0));
                vst1q_lane_f32(output_ptr0 + 2, y0, 2);
                vst1_f32(output_ptr1, vget_low_f32(y1));
                vst1q_lane_f32(output_ptr1 + 2, y1, 2);
                break;
            }
            input_ptr0 += 2 * output_w_remain;
            input_ptr1 += 2 * output_w_remain;
            input_ptr2 += 2 * output_w_remain;
            input_ptr3 += 2 * output_w_remain;
            output_ptr0 += output_w_remain;
            output_ptr1 += output_w_remain;
          }
          // pad right
          if (padding_r) {
            for (int w = valid_w_end; w < output_w; ++w) {
              int padding = 2 * w + 2 - (padding_w + input_w);
              if (padding >= 2) {
                *output_ptr0 = 0.f;
                *output_ptr1 = 0.f;
              } else {
                float acc0 = PoolPre<P>(*input_ptr0, *input_ptr1);
                float acc1 = PoolPre<P>(*input_ptr2, *input_ptr3);
                *output_ptr0 = PoolPost<P>(acc0, 0.5f);
                *output_ptr1 = PoolPost<P>(acc1, 0.5f);
              }
              output_ptr0++;
              output_ptr1++;
            }
          }
        }
        // remain height
H
hjchen2 已提交
695
        int start_h = valid_h_start + (valid_h & 0xfffffffe);
H
hjchen2 已提交
696 697 698 699 700 701 702 703 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 761 762 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
        for (int h = start_h; h < valid_h_end; ++h) {
          const float *input_ptr0 = input_ptr + (2 * h - padding_h) * input_w;
          const float *input_ptr1 = input_ptr0 + input_w;
          float *output_ptr0 = output_ptr + h * output_w;
          // pad left
          if (padding_w) {
            for (int w = valid_w_start - 1; w >= 0; --w) {
              int padding = padding_w - 2 * w;
              if (padding >= 2) {
                output_ptr0[w] = 0.f;
              } else {
                float acc0 = PoolPre<P>(*input_ptr0, *input_ptr1);
                output_ptr0[w] = PoolPost<P>(acc0, 0.5f);
              }
            }
            input_ptr0 += (padding_w & 0x1);
            input_ptr1 += (padding_w & 0x1);
            output_ptr0 += valid_w_start;
          }
          // valid
          float32x4x2_t x0, x1;
          float32x4_t y0;
          float32x4_t post = vdupq_n_f32(0.25f);
          for (int loop = 0; loop < output_w_tiles; ++loop) {
            x0 = vld2q_f32(input_ptr0);
            x1 = vld2q_f32(input_ptr1);
            y0 = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
            y0 = vPoolPreq_f32<P>(y0, x1.val[0]);
            y0 = vPoolPreq_f32<P>(y0, x1.val[1]);
            y0 = vPoolPostq_f32<P>(y0, post);
            vst1q_f32(output_ptr0, y0);

            input_ptr0 += 8;
            input_ptr1 += 8;
            output_ptr0 += 4;
          }
          // remain width
          if (output_w_remain > 0) {
            x0 = vld2q_f32(input_ptr0);
            x1 = vld2q_f32(input_ptr1);
            y0 = vPoolPreq_f32<P>(x0.val[0], x0.val[1]);
            y0 = vPoolPreq_f32<P>(y0, x1.val[0]);
            y0 = vPoolPreq_f32<P>(y0, x1.val[1]);
            y0 = vPoolPostq_f32<P>(y0, post);

            switch (output_w_remain) {
              case 1:
                vst1q_lane_f32(output_ptr0, y0, 0);
                break;
              case 2:
                vst1_f32(output_ptr0, vget_low_f32(y0));
                break;
              case 3:
                vst1_f32(output_ptr0, vget_low_f32(y0));
                vst1q_lane_f32(output_ptr0 + 2, y0, 2);
                break;
            }
            input_ptr0 += 2 * output_w_remain;
            input_ptr1 += 2 * output_w_remain;
            output_ptr0 += output_w_remain;
          }
          // pad right
          if (padding_r) {
            for (int w = valid_w_end; w < output_w; ++w) {
              int padding = 2 * w + 2 - (padding_w + input_w);
              if (padding >= 2) {
                *output_ptr0 = 0.f;
              } else {
                float acc0 = PoolPre<P>(*input_ptr0, *input_ptr1);
                *output_ptr0 = PoolPost<P>(acc0, 0.5f);
              }
              output_ptr0++;
            }
          }
        }
        // bottom
        for (int h = valid_h_end; h < output_h; ++h) {
          Pooling2x2NormalRow<P, 2>(input_ptr, h, input_h, input_w, padding_h,
                                    padding_w, output_w, output_ptr);
        }
      }
    }
  }
};

template struct Pooling2x2<MAX, 1>;
template struct Pooling2x2<AVG, 1>;
template struct Pooling2x2<MAX, 2>;
template struct Pooling2x2<AVG, 2>;

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

#endif  // __ARM_NEON__
#endif  // POOL_OP