depthwise_conv3x3.cpp 44.6 KB
Newer Older
W
wangliu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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. */
H
hjchen2 已提交
14

15 16
#if defined(__ARM_NEON__) || defined(__ARM_NEON)

H
hjchen2 已提交
17
#include "operators/math/depthwise_conv3x3.h"
E
eclipsess 已提交
18
#include <arm_neon.h>
W
wangliu 已提交
19 20 21 22

namespace paddle_mobile {
namespace operators {
namespace math {
H
hjchen2 已提交
23

24 25 26 27 28
#ifndef __aarch64__
inline float32x4_t vpaddq_f32(float32x4_t r0, float32x4_t r1) {
  float32x2_t sum0 = vpadd_f32(vget_low_f32(r0), vget_high_f32(r0));
  float32x2_t sum1 = vpadd_f32(vget_low_f32(r1), vget_high_f32(r1));
  return vcombine_f32(sum0, sum1);
W
wangliu 已提交
29
}
30
#endif
W
wangliu 已提交
31

32 33 34 35 36 37 38
template <int Stride = 1>
inline void Depth3x3NormalRowLoadInput(const float *input, float32x4_t *y) {
  y[0] = vld1q_f32(input);
  y[2] = vld1q_f32(input + 4);
  y[1] = vextq_f32(y[0], y[2], 1);
  y[2] = vextq_f32(y[0], y[2], 2);
}
W
wangliu 已提交
39

40 41 42 43 44 45 46 47
template <>
inline void Depth3x3NormalRowLoadInput<2>(const float *input, float32x4_t *y) {
  float32x4x2_t x = vld2q_f32(input);
  y[0] = x.val[0];
  y[1] = x.val[1];
  y[2] = vextq_f32(y[0], y[0], 1);
  y[2] = vsetq_lane_f32(input[8], y[2], 3);
}
W
wangliu 已提交
48

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#define DEPTHWISE_CONV3X3_NORMAL_BORDER(start, end)                      \
  for (int w = start; w < end; ++w) {                                    \
    const int w_in_start = -padding_w + w * Stride_w;                    \
    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;           \
    float value = 0;                                                     \
    for (int h_in = h_start; h_in < h_end; ++h_in) {                     \
      for (int w_in = w_start; w_in < w_end; ++w_in) {                   \
        value += filter[(h_in - h_in_start) * 3 + (w_in - w_in_start)] * \
                 input[h_in * input_w + w_in];                           \
      }                                                                  \
    }                                                                    \
    output_ptr[w] = value;                                               \
  }
W
wangliu 已提交
64

65 66 67 68 69 70 71 72 73 74 75
template <int Stride_h, int Stride_w>
inline void DepthwiseConv3x3NormalRow(const float *input, const float *filter,
                                      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, float32x4_t *ker) {
  const int h_in_start = -padding_h + h_output * Stride_h;
  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;

76 77 78 79 80
  int valid_w_start = (padding_w + Stride_w - 1) / Stride_w;
  int valid_w_end = (input_w + padding_w - 3) / Stride_w + 1;
  if (valid_w_end < valid_w_start) {
    valid_w_end = valid_w_start;
  }
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  // const int valid_w_end = output_w - valid_w_start;
  float *output_ptr = output + h_output * output_w;
  // border left
  DEPTHWISE_CONV3X3_NORMAL_BORDER(0, valid_w_start)
  // middle
  int output_tiles = (valid_w_end - valid_w_start) >> 2;
  float32x4_t _sum, _x[3];
  // valid w
  for (int w = 0; w < output_tiles * 4; w += 4) {
    _sum = vdupq_n_f32(0.f);
    int output_offset = valid_w_start + w;
    int input_w_offset = output_offset * Stride_w - padding_w;
    for (int h_in = h_start; h_in < h_end; ++h_in) {
      int index = h_in - h_in_start;
      Depth3x3NormalRowLoadInput<Stride_w>(
          input + h_in * input_w + input_w_offset, _x);
      _sum = vmlaq_lane_f32(_sum, _x[0], vget_low_f32(ker[index]), 0);
      _sum = vmlaq_lane_f32(_sum, _x[1], vget_low_f32(ker[index]), 1);
      _sum = vmlaq_lane_f32(_sum, _x[2], vget_high_f32(ker[index]), 0);
W
wangliu 已提交
100
    }
101
    vst1q_f32(output_ptr + output_offset, _sum);
W
wangliu 已提交
102
  }
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  // remain valid w
  int remain = (valid_w_end - valid_w_start) & 0x3;
  if (remain > 0) {
    _sum = vdupq_n_f32(0.f);
    int remain_start = valid_w_start + (output_tiles << 2);
    int input_w_offset = remain_start * Stride_w - padding_w;
    float *output_ptr0 = output_ptr + remain_start;

    for (int h_in = h_start; h_in < h_end; ++h_in) {
      int index = h_in - h_in_start;
      Depth3x3NormalRowLoadInput<Stride_w>(
          input + h_in * input_w + input_w_offset, _x);
      _sum = vmlaq_lane_f32(_sum, _x[0], vget_low_f32(ker[index]), 0);
      _sum = vmlaq_lane_f32(_sum, _x[1], vget_low_f32(ker[index]), 1);
      _sum = vmlaq_lane_f32(_sum, _x[2], vget_high_f32(ker[index]), 0);
    }
    switch (remain) {
      case 3:
        vst1q_lane_f32(output_ptr0 + 2, _sum, 2);
      case 2:
        vst1_f32(output_ptr0, vget_low_f32(_sum));
        break;
      case 1:
126
        vst1q_lane_f32(output_ptr0, _sum, 0);
127 128 129 130 131
        break;
    }
  }
  // border right
  DEPTHWISE_CONV3X3_NORMAL_BORDER(valid_w_end, output_w)
W
wangliu 已提交
132
}
E
eclipsess 已提交
133

134 135 136 137 138 139 140 141
template <>
void DepthwiseConv3x3S1<float, float>(const framework::Tensor &input,
                                      const framework::Tensor &filter,
                                      const std::vector<int> &paddings,
                                      framework::Tensor *output) {
  const float *input_data = input.data<float>();
  const float *filter_data = filter.data<float>();
  float *out_data = output->mutable_data<float>();
142 143 144 145 146 147 148 149 150 151 152

  const int input_h = input.dims()[2];
  const int input_w = input.dims()[3];
  const int output_h = output->dims()[2];
  const int output_w = output->dims()[3];
  const int padding_h = paddings[0];
  const int padding_w = paddings[1];
  const int image_size = input_h * input_w;
  const int out_image_size = output_h * output_w;
  const int valid_h_start = padding_h;
  const int valid_h_end = output_h - valid_h_start;
153 154
  const int valid_h =
      valid_h_end - valid_h_start > 0 ? valid_h_end - valid_h_start : 0;
155 156 157
  const int valid_w_start = padding_w;
  const int valid_w_end = output_w - valid_w_start;
  const int valid_w = valid_w_end - valid_w_start;
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

  #pragma omp parallel for
  for (int g = 0; g < input.dims()[1]; ++g) {
    const float *input_ptr = input_data + g * image_size;
    const float *filter_ptr = filter_data + g * 9;
    float *output_ptr = out_data + g * out_image_size;

    const float *filter_ptr0 = filter_ptr;
    const float *filter_ptr1 = filter_ptr0 + 3;
    const float *filter_ptr2 = filter_ptr1 + 3;
    float32x4_t _ker[3];
    _ker[0] = vld1q_f32(filter_ptr0);
    _ker[1] = vld1q_f32(filter_ptr1);
    _ker[2] = vld1q_f32(filter_ptr2);

    // pad top
    for (int h = 0; h < valid_h_start; ++h) {
      DepthwiseConv3x3NormalRow<1, 1>(input_ptr, filter_ptr, h, input_h,
                                      input_w, padding_h, padding_w, output_w,
                                      output_ptr, _ker);
    }
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
    // output 2x6
    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 - 1; h += 2) {
      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;
      float *output_ptr0 = output_ptr + h * output_w;
      float *output_ptr1 = output_ptr0 + output_w;
      // pad left
      if (padding_w) {
        float32x4_t row0 = vld1q_f32(input_ptr0);
        float32x4_t row1 = vld1q_f32(input_ptr1);
        float32x4_t row2 = vld1q_f32(input_ptr2);
        float32x4_t row3 = vld1q_f32(input_ptr3);
        float32x4_t zero = vdupq_n_f32(0.f);
        row0 = vextq_f32(zero, row0, 3);
        row1 = vextq_f32(zero, row1, 3);
        row2 = vextq_f32(zero, row2, 3);
        row3 = vextq_f32(zero, row3, 3);
        float32x4_t acc0, acc1;
        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;
          } else {
            acc0 = vmulq_f32(row0, _ker[0]);
            acc0 = vmlaq_f32(acc0, row1, _ker[1]);
            acc0 = vmlaq_f32(acc0, row2, _ker[2]);
            acc0 = vextq_f32(acc0, acc0, 1);
            acc1 = vmulq_f32(row1, _ker[0]);
            acc1 = vmlaq_f32(acc1, row2, _ker[1]);
            acc1 = vmlaq_f32(acc1, row3, _ker[2]);
            acc1 = vextq_f32(acc1, acc1, 1);
            float32x2_t sum = vpadd_f32(vget_low_f32(acc0), vget_low_f32(acc1));
            vst1_lane_f32(output_ptr0 + w, sum, 0);
            vst1_lane_f32(output_ptr1 + w, sum, 1);

            row0 = vextq_f32(zero, row0, 3);
            row1 = vextq_f32(zero, row1, 3);
            row2 = vextq_f32(zero, row2, 3);
            row3 = vextq_f32(zero, row3, 3);
224 225
          }
        }
226 227
        output_ptr0 += valid_w_start;
        output_ptr1 += valid_w_start;
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
      // valid
      float32x4_t _result0, _result1, _result2, _result3;
      for (int loop = 0; loop < output_w_tiles; ++loop) {
        float32x4_t _row00 = vld1q_f32(input_ptr0);
        float32x4_t _row01 = vld1q_f32(input_ptr0 + 4);
        float32x4_t _row10 = vld1q_f32(input_ptr1);
        float32x4_t _row11 = vld1q_f32(input_ptr1 + 4);

        float32x4_t _ext01 = vextq_f32(_row00, _row01, 1);
        float32x4_t _ext02 = vextq_f32(_row00, _row01, 2);
        float32x4_t _ext03 = vextq_f32(_row01, _row01, 1);
        float32x4_t _ext04 = vextq_f32(_row01, _row01, 2);

        _result0 = vmulq_lane_f32(_row00, vget_low_f32(_ker[0]), 0);
        _result0 = vmlaq_lane_f32(_result0, _ext01, vget_low_f32(_ker[0]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext02, vget_high_f32(_ker[0]), 0);
        _result1 = vmulq_lane_f32(_row01, vget_low_f32(_ker[0]), 0);
        _result1 = vmlaq_lane_f32(_result1, _ext03, vget_low_f32(_ker[0]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext04, vget_high_f32(_ker[0]), 0);

        _ext01 = vextq_f32(_row10, _row11, 1);
        _ext02 = vextq_f32(_row10, _row11, 2);
        _ext03 = vextq_f32(_row11, _row11, 1);
        _ext04 = vextq_f32(_row11, _row11, 2);

        _result0 = vmlaq_lane_f32(_result0, _row10, vget_low_f32(_ker[1]), 0);
        _result0 = vmlaq_lane_f32(_result0, _ext01, vget_low_f32(_ker[1]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext02, vget_high_f32(_ker[1]), 0);
        _result1 = vmlaq_lane_f32(_result1, _row11, vget_low_f32(_ker[1]), 0);
        _result1 = vmlaq_lane_f32(_result1, _ext03, vget_low_f32(_ker[1]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext04, vget_high_f32(_ker[1]), 0);

        _result2 = vmulq_lane_f32(_row10, vget_low_f32(_ker[0]), 0);
        _result2 = vmlaq_lane_f32(_result2, _ext01, vget_low_f32(_ker[0]), 1);
        _result2 = vmlaq_lane_f32(_result2, _ext02, vget_high_f32(_ker[0]), 0);
        _result3 = vmulq_lane_f32(_row11, vget_low_f32(_ker[0]), 0);
        _result3 = vmlaq_lane_f32(_result3, _ext03, vget_low_f32(_ker[0]), 1);
        _result3 = vmlaq_lane_f32(_result3, _ext04, vget_high_f32(_ker[0]), 0);

        _row00 = vld1q_f32(input_ptr2);
        _row01 = vld1q_f32(input_ptr2 + 4);
        _row10 = vld1q_f32(input_ptr3);
        _row11 = vld1q_f32(input_ptr3 + 4);

        _ext01 = vextq_f32(_row00, _row01, 1);
        _ext02 = vextq_f32(_row00, _row01, 2);
        _ext03 = vextq_f32(_row01, _row01, 1);
        _ext04 = vextq_f32(_row01, _row01, 2);

        _result0 = vmlaq_lane_f32(_result0, _row00, vget_low_f32(_ker[2]), 0);
        _result0 = vmlaq_lane_f32(_result0, _ext01, vget_low_f32(_ker[2]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext02, vget_high_f32(_ker[2]), 0);
        _result1 = vmlaq_lane_f32(_result1, _row01, vget_low_f32(_ker[2]), 0);
        _result1 = vmlaq_lane_f32(_result1, _ext03, vget_low_f32(_ker[2]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext04, vget_high_f32(_ker[2]), 0);

        _result2 = vmlaq_lane_f32(_result2, _row00, vget_low_f32(_ker[1]), 0);
        _result2 = vmlaq_lane_f32(_result2, _ext01, vget_low_f32(_ker[1]), 1);
        _result2 = vmlaq_lane_f32(_result2, _ext02, vget_high_f32(_ker[1]), 0);
        _result3 = vmlaq_lane_f32(_result3, _row01, vget_low_f32(_ker[1]), 0);
        _result3 = vmlaq_lane_f32(_result3, _ext03, vget_low_f32(_ker[1]), 1);
        _result3 = vmlaq_lane_f32(_result3, _ext04, vget_high_f32(_ker[1]), 0);

        _ext01 = vextq_f32(_row10, _row11, 1);
        _ext02 = vextq_f32(_row10, _row11, 2);
        _ext03 = vextq_f32(_row11, _row11, 1);
        _ext04 = vextq_f32(_row11, _row11, 2);

        _result2 = vmlaq_lane_f32(_result2, _row10, vget_low_f32(_ker[2]), 0);
        _result2 = vmlaq_lane_f32(_result2, _ext01, vget_low_f32(_ker[2]), 1);
        _result2 = vmlaq_lane_f32(_result2, _ext02, vget_high_f32(_ker[2]), 0);
        _result3 = vmlaq_lane_f32(_result3, _row11, vget_low_f32(_ker[2]), 0);
        _result3 = vmlaq_lane_f32(_result3, _ext03, vget_low_f32(_ker[2]), 1);
        _result3 = vmlaq_lane_f32(_result3, _ext04, vget_high_f32(_ker[2]), 0);

        vst1q_f32(output_ptr0, _result0);
        vst1_f32(output_ptr0 + 4, vget_low_f32(_result1));
        vst1q_f32(output_ptr1, _result2);
        vst1_f32(output_ptr1 + 4, vget_low_f32(_result3));

        input_ptr0 += 6;
        input_ptr1 += 6;
        input_ptr2 += 6;
        input_ptr3 += 6;
        output_ptr0 += 6;
        output_ptr1 += 6;
E
eclipsess 已提交
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
      // remain w
      if (output_w_remain > 0) {
        float32x4_t _row00 = vld1q_f32(input_ptr0);
        float32x4_t _row01 = vld1q_f32(input_ptr0 + 4);
        float32x4_t _row10 = vld1q_f32(input_ptr1);
        float32x4_t _row11 = vld1q_f32(input_ptr1 + 4);

        float32x4_t _ext01 = vextq_f32(_row00, _row01, 1);
        float32x4_t _ext02 = vextq_f32(_row00, _row01, 2);
        float32x4_t _ext03 = vextq_f32(_row01, _row01, 1);
        float32x4_t _ext04 = vextq_f32(_row01, _row01, 2);

        _result0 = vmulq_lane_f32(_row00, vget_low_f32(_ker[0]), 0);
        _result0 = vmlaq_lane_f32(_result0, _ext01, vget_low_f32(_ker[0]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext02, vget_high_f32(_ker[0]), 0);
        _result1 = vmulq_lane_f32(_row01, vget_low_f32(_ker[0]), 0);
        _result1 = vmlaq_lane_f32(_result1, _ext03, vget_low_f32(_ker[0]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext04, vget_high_f32(_ker[0]), 0);

        _ext01 = vextq_f32(_row10, _row11, 1);
        _ext02 = vextq_f32(_row10, _row11, 2);
        _ext03 = vextq_f32(_row11, _row11, 1);
        _ext04 = vextq_f32(_row11, _row11, 2);

        _result0 = vmlaq_lane_f32(_result0, _row10, vget_low_f32(_ker[1]), 0);
        _result0 = vmlaq_lane_f32(_result0, _ext01, vget_low_f32(_ker[1]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext02, vget_high_f32(_ker[1]), 0);
        _result1 = vmlaq_lane_f32(_result1, _row11, vget_low_f32(_ker[1]), 0);
        _result1 = vmlaq_lane_f32(_result1, _ext03, vget_low_f32(_ker[1]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext04, vget_high_f32(_ker[1]), 0);

        _result2 = vmulq_lane_f32(_row10, vget_low_f32(_ker[0]), 0);
        _result2 = vmlaq_lane_f32(_result2, _ext01, vget_low_f32(_ker[0]), 1);
        _result2 = vmlaq_lane_f32(_result2, _ext02, vget_high_f32(_ker[0]), 0);
        _result3 = vmulq_lane_f32(_row11, vget_low_f32(_ker[0]), 0);
        _result3 = vmlaq_lane_f32(_result3, _ext03, vget_low_f32(_ker[0]), 1);
        _result3 = vmlaq_lane_f32(_result3, _ext04, vget_high_f32(_ker[0]), 0);

        _row00 = vld1q_f32(input_ptr2);
        _row01 = vld1q_f32(input_ptr2 + 4);
        _row10 = vld1q_f32(input_ptr3);
        _row11 = vld1q_f32(input_ptr3 + 4);

        _ext01 = vextq_f32(_row00, _row01, 1);
        _ext02 = vextq_f32(_row00, _row01, 2);
        _ext03 = vextq_f32(_row01, _row01, 1);
        _ext04 = vextq_f32(_row01, _row01, 2);

        _result0 = vmlaq_lane_f32(_result0, _row00, vget_low_f32(_ker[2]), 0);
        _result0 = vmlaq_lane_f32(_result0, _ext01, vget_low_f32(_ker[2]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext02, vget_high_f32(_ker[2]), 0);
        _result1 = vmlaq_lane_f32(_result1, _row01, vget_low_f32(_ker[2]), 0);
        _result1 = vmlaq_lane_f32(_result1, _ext03, vget_low_f32(_ker[2]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext04, vget_high_f32(_ker[2]), 0);

        _result2 = vmlaq_lane_f32(_result2, _row00, vget_low_f32(_ker[1]), 0);
        _result2 = vmlaq_lane_f32(_result2, _ext01, vget_low_f32(_ker[1]), 1);
        _result2 = vmlaq_lane_f32(_result2, _ext02, vget_high_f32(_ker[1]), 0);
        _result3 = vmlaq_lane_f32(_result3, _row01, vget_low_f32(_ker[1]), 0);
        _result3 = vmlaq_lane_f32(_result3, _ext03, vget_low_f32(_ker[1]), 1);
        _result3 = vmlaq_lane_f32(_result3, _ext04, vget_high_f32(_ker[1]), 0);

        _ext01 = vextq_f32(_row10, _row11, 1);
        _ext02 = vextq_f32(_row10, _row11, 2);
        _ext03 = vextq_f32(_row11, _row11, 1);
        _ext04 = vextq_f32(_row11, _row11, 2);

        _result2 = vmlaq_lane_f32(_result2, _row10, vget_low_f32(_ker[2]), 0);
        _result2 = vmlaq_lane_f32(_result2, _ext01, vget_low_f32(_ker[2]), 1);
        _result2 = vmlaq_lane_f32(_result2, _ext02, vget_high_f32(_ker[2]), 0);
        _result3 = vmlaq_lane_f32(_result3, _row11, vget_low_f32(_ker[2]), 0);
        _result3 = vmlaq_lane_f32(_result3, _ext03, vget_low_f32(_ker[2]), 1);
        _result3 = vmlaq_lane_f32(_result3, _ext04, vget_high_f32(_ker[2]), 0);

        switch (output_w_remain) {
          case 5:
            vst1q_lane_f32(output_ptr0 + 4, _result1, 0);
            vst1q_lane_f32(output_ptr1 + 4, _result3, 0);
          case 4:
            vst1q_f32(output_ptr0, _result0);
            vst1q_f32(output_ptr1, _result2);
            break;
          case 3:
            vst1q_lane_f32(output_ptr0 + 2, _result0, 2);
            vst1q_lane_f32(output_ptr1 + 2, _result2, 2);
          case 2:
            vst1_f32(output_ptr0, vget_low_f32(_result0));
            vst1_f32(output_ptr1, vget_low_f32(_result2));
            break;
          case 1:
            vst1q_lane_f32(output_ptr0, _result0, 0);
            vst1q_lane_f32(output_ptr1, _result2, 0);
            break;
E
eclipsess 已提交
409
        }
410

411 412 413 414 415 416
        input_ptr0 += output_w_remain;
        input_ptr1 += output_w_remain;
        input_ptr2 += output_w_remain;
        input_ptr3 += output_w_remain;
        output_ptr0 += output_w_remain;
        output_ptr1 += output_w_remain;
417
      }
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
      // 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 zero = vdup_n_f32(0.f);
        float32x2_t acc0, acc1;
        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;
          } else {
            acc0 = vmul_f32(row0, vget_low_f32(_ker[0]));
            acc0 = vmla_f32(acc0, row1, vget_low_f32(_ker[1]));
            acc0 = vmla_f32(acc0, row2, vget_low_f32(_ker[2]));
            acc1 = vmul_f32(row1, vget_low_f32(_ker[0]));
            acc1 = vmla_f32(acc1, row2, vget_low_f32(_ker[1]));
            acc1 = vmla_f32(acc1, row3, vget_low_f32(_ker[2]));
            float32x2_t sum = vpadd_f32(acc0, acc1);
            vst1_lane_f32(output_ptr0, sum, 0);
            vst1_lane_f32(output_ptr1, sum, 1);
            row0 = vext_f32(row0, zero, 1);
            row1 = vext_f32(row1, zero, 1);
            row2 = vext_f32(row2, zero, 1);
            row3 = vext_f32(row3, zero, 1);
          }
          output_ptr0++;
          output_ptr1++;
448 449
        }
      }
450 451
    }
    // remain height
H
hjchen2 已提交
452
    int start_h = valid_h_start + (valid_h & 0xfffffffe);
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
    if (start_h < valid_h_end) {
      const float *input_ptr0 = input_ptr + (start_h - padding_h) * input_w;
      const float *input_ptr1 = input_ptr0 + input_w;
      const float *input_ptr2 = input_ptr1 + input_w;
      float *output_ptr0 = output_ptr + start_h * output_w;
      // pad left
      if (padding_w) {
        float32x4_t row0 = vld1q_f32(input_ptr0);
        float32x4_t row1 = vld1q_f32(input_ptr1);
        float32x4_t row2 = vld1q_f32(input_ptr2);
        float32x4_t zero = vdupq_n_f32(0.f);
        row0 = vextq_f32(zero, row0, 3);
        row1 = vextq_f32(zero, row1, 3);
        row2 = vextq_f32(zero, row2, 3);
        float32x4_t acc;
        for (int w = valid_w_start - 1; w >= 0; --w) {
          int padding = padding_w - w;
          if (padding >= 3) {
            output_ptr0[w] = 0.f;
          } else {
            acc = vmulq_f32(row0, _ker[0]);
            acc = vmlaq_f32(acc, row1, _ker[1]);
            acc = vmlaq_f32(acc, row2, _ker[2]);
            acc = vextq_f32(acc, acc, 1);
            float32x2_t sum = vpadd_f32(vget_low_f32(acc), vget_low_f32(acc));
            vst1_lane_f32(output_ptr0 + w, sum, 0);

            row0 = vextq_f32(zero, row0, 3);
            row1 = vextq_f32(zero, row1, 3);
            row2 = vextq_f32(zero, row2, 3);
          }
E
eclipsess 已提交
484
        }
485
        output_ptr0 += valid_w_start;
E
eclipsess 已提交
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
      // valid
      float32x4_t _result0, _result1;
      for (int loop = 0; loop < output_w_tiles; ++loop) {
        float32x4_t _row00 = vld1q_f32(input_ptr0);
        float32x4_t _row01 = vld1q_f32(input_ptr0 + 4);
        float32x4_t _row10 = vld1q_f32(input_ptr1);
        float32x4_t _row11 = vld1q_f32(input_ptr1 + 4);

        float32x4_t _ext01 = vextq_f32(_row00, _row01, 1);
        float32x4_t _ext02 = vextq_f32(_row00, _row01, 2);
        float32x4_t _ext03 = vextq_f32(_row01, _row01, 1);
        float32x4_t _ext04 = vextq_f32(_row01, _row01, 2);

        _result0 = vmulq_lane_f32(_row00, vget_low_f32(_ker[0]), 0);
        _result0 = vmlaq_lane_f32(_result0, _ext01, vget_low_f32(_ker[0]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext02, vget_high_f32(_ker[0]), 0);
        _result1 = vmulq_lane_f32(_row01, vget_low_f32(_ker[0]), 0);
        _result1 = vmlaq_lane_f32(_result1, _ext03, vget_low_f32(_ker[0]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext04, vget_high_f32(_ker[0]), 0);

        _ext01 = vextq_f32(_row10, _row11, 1);
        _ext02 = vextq_f32(_row10, _row11, 2);
        _ext03 = vextq_f32(_row11, _row11, 1);
        _ext04 = vextq_f32(_row11, _row11, 2);

        _result0 = vmlaq_lane_f32(_result0, _row10, vget_low_f32(_ker[1]), 0);
        _result0 = vmlaq_lane_f32(_result0, _ext01, vget_low_f32(_ker[1]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext02, vget_high_f32(_ker[1]), 0);
        _result1 = vmlaq_lane_f32(_result1, _row11, vget_low_f32(_ker[1]), 0);
        _result1 = vmlaq_lane_f32(_result1, _ext03, vget_low_f32(_ker[1]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext04, vget_high_f32(_ker[1]), 0);

        _row00 = vld1q_f32(input_ptr2);
        _row01 = vld1q_f32(input_ptr2 + 4);

        _ext01 = vextq_f32(_row00, _row01, 1);
        _ext02 = vextq_f32(_row00, _row01, 2);
        _ext03 = vextq_f32(_row01, _row01, 1);
        _ext04 = vextq_f32(_row01, _row01, 2);

        _result0 = vmlaq_lane_f32(_result0, _row00, vget_low_f32(_ker[2]), 0);
        _result0 = vmlaq_lane_f32(_result0, _ext01, vget_low_f32(_ker[2]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext02, vget_high_f32(_ker[2]), 0);
        _result1 = vmlaq_lane_f32(_result1, _row01, vget_low_f32(_ker[2]), 0);
        _result1 = vmlaq_lane_f32(_result1, _ext03, vget_low_f32(_ker[2]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext04, vget_high_f32(_ker[2]), 0);

        vst1q_f32(output_ptr0, _result0);
        vst1_f32(output_ptr0 + 4, vget_low_f32(_result1));

        input_ptr0 += 6;
        input_ptr1 += 6;
        input_ptr2 += 6;
        output_ptr0 += 6;
541
      }
E
eclipsess 已提交
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
      if (output_w_remain > 0) {
        float32x4_t _row00 = vld1q_f32(input_ptr0);
        float32x4_t _row01 = vld1q_f32(input_ptr0 + 4);
        float32x4_t _row10 = vld1q_f32(input_ptr1);
        float32x4_t _row11 = vld1q_f32(input_ptr1 + 4);

        float32x4_t _ext01 = vextq_f32(_row00, _row01, 1);
        float32x4_t _ext02 = vextq_f32(_row00, _row01, 2);
        float32x4_t _ext03 = vextq_f32(_row01, _row01, 1);
        float32x4_t _ext04 = vextq_f32(_row01, _row01, 2);

        _result0 = vmulq_lane_f32(_row00, vget_low_f32(_ker[0]), 0);
        _result0 = vmlaq_lane_f32(_result0, _ext01, vget_low_f32(_ker[0]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext02, vget_high_f32(_ker[0]), 0);
        _result1 = vmulq_lane_f32(_row01, vget_low_f32(_ker[0]), 0);
        _result1 = vmlaq_lane_f32(_result1, _ext03, vget_low_f32(_ker[0]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext04, vget_high_f32(_ker[0]), 0);

        _ext01 = vextq_f32(_row10, _row11, 1);
        _ext02 = vextq_f32(_row10, _row11, 2);
        _ext03 = vextq_f32(_row11, _row11, 1);
        _ext04 = vextq_f32(_row11, _row11, 2);

        _result0 = vmlaq_lane_f32(_result0, _row10, vget_low_f32(_ker[1]), 0);
        _result0 = vmlaq_lane_f32(_result0, _ext01, vget_low_f32(_ker[1]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext02, vget_high_f32(_ker[1]), 0);
        _result1 = vmlaq_lane_f32(_result1, _row11, vget_low_f32(_ker[1]), 0);
        _result1 = vmlaq_lane_f32(_result1, _ext03, vget_low_f32(_ker[1]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext04, vget_high_f32(_ker[1]), 0);

        _row00 = vld1q_f32(input_ptr2);
        _row01 = vld1q_f32(input_ptr2 + 4);

        _ext01 = vextq_f32(_row00, _row01, 1);
        _ext02 = vextq_f32(_row00, _row01, 2);
        _ext03 = vextq_f32(_row01, _row01, 1);
        _ext04 = vextq_f32(_row01, _row01, 2);

        _result0 = vmlaq_lane_f32(_result0, _row00, vget_low_f32(_ker[2]), 0);
        _result0 = vmlaq_lane_f32(_result0, _ext01, vget_low_f32(_ker[2]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext02, vget_high_f32(_ker[2]), 0);
        _result1 = vmlaq_lane_f32(_result1, _row01, vget_low_f32(_ker[2]), 0);
        _result1 = vmlaq_lane_f32(_result1, _ext03, vget_low_f32(_ker[2]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext04, vget_high_f32(_ker[2]), 0);

        switch (output_w_remain) {
          case 5:
            vst1q_lane_f32(output_ptr0 + 4, _result1, 0);
          case 4:
            vst1q_f32(output_ptr0, _result0);
            break;
          case 3:
            vst1q_lane_f32(output_ptr0 + 2, _result0, 2);
          case 2:
            vst1_f32(output_ptr0, vget_low_f32(_result0));
            break;
          case 1:
            vst1q_lane_f32(output_ptr0, _result0, 0);
            break;
E
eclipsess 已提交
602
        }
603

604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
        input_ptr0 += output_w_remain;
        input_ptr1 += output_w_remain;
        input_ptr2 += output_w_remain;
        output_ptr0 += output_w_remain;
      }
      // 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 zero = vdup_n_f32(0.f);
        float32x2_t acc;
        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;
620
          } else {
621 622 623 624 625 626 627 628
            acc = vmul_f32(row0, vget_low_f32(_ker[0]));
            acc = vmla_f32(acc, row1, vget_low_f32(_ker[1]));
            acc = vmla_f32(acc, row2, vget_low_f32(_ker[2]));
            float32x2_t sum = vpadd_f32(acc, acc);
            vst1_lane_f32(output_ptr0, sum, 0);
            row0 = vext_f32(row0, zero, 1);
            row1 = vext_f32(row1, zero, 1);
            row2 = vext_f32(row2, zero, 1);
629
          }
630
          output_ptr0++;
631 632 633
        }
      }
    }
634
    // pad bottom
635
    for (int h = valid_h_end; (h < output_h) && (h > valid_h_start - 1); ++h) {
636 637 638 639
      DepthwiseConv3x3NormalRow<1, 1>(input_ptr, filter_ptr, h, input_h,
                                      input_w, padding_h, padding_w, output_w,
                                      output_ptr, _ker);
    }
640 641
  }
}
E
eclipsess 已提交
642

643 644 645 646 647 648 649 650
template <>
void DepthwiseConv3x3S2<float, float>(const framework::Tensor &input,
                                      const framework::Tensor &filter,
                                      const std::vector<int> &paddings,
                                      framework::Tensor *output) {
  const float *input_data = input.data<float>();
  const float *filter_data = filter.data<float>();
  float *out_data = output->mutable_data<float>();
651 652 653 654 655 656 657 658 659 660

  const int input_h = input.dims()[2];
  const int input_w = input.dims()[3];
  const int output_h = output->dims()[2];
  const int output_w = output->dims()[3];
  const int padding_h = paddings[0];
  const int padding_w = paddings[1];
  const int image_size = input_h * input_w;
  const int out_image_size = output_h * output_w;
  const int valid_h_start = (padding_h + 1) / 2;
661 662
  const int valid_h_end =
      std::max((input_h + padding_h - 1) / 2, valid_h_start);
663 664
  const int valid_h =
      valid_h_end - valid_h_start > 0 ? valid_h_end - valid_h_start : 0;
665
  const int valid_w_start = (padding_w + 1) / 2;
666 667
  const int valid_w_end =
      std::max((input_w + padding_w - 1) / 2, valid_w_start);
668 669
  const int valid_w = valid_w_end - valid_w_start;
  const int input_w_start = 2 * valid_w_start - padding_w;
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708

  #pragma omp parallel for
  for (int g = 0; g < input.dims()[1]; ++g) {
    const float *input_ptr = input_data + g * image_size;
    const float *filter_ptr = filter_data + g * 9;
    float *output_ptr = out_data + g * out_image_size;

    const float *filter_ptr0 = filter_ptr;
    const float *filter_ptr1 = filter_ptr0 + 3;
    const float *filter_ptr2 = filter_ptr1 + 3;
    float32x4_t _ker[3];
    _ker[0] = vld1q_f32(filter_ptr0);
    _ker[1] = vld1q_f32(filter_ptr1);
    _ker[2] = vld1q_f32(filter_ptr2);

    // pad top
    for (int h = 0; h < valid_h_start; ++h) {
      DepthwiseConv3x3NormalRow<2, 2>(input_ptr, filter_ptr, h, input_h,
                                      input_w, padding_h, padding_w, output_w,
                                      output_ptr, _ker);
    }
    // valid 2x4
    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;
      const float *input_ptr4 = input_ptr3 + 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 << 1);
          if (padding >= 3) {
            output_ptr0[w] = 0;
            output_ptr1[w] = 0;
E
eclipsess 已提交
709
          } else {
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
            float32x4_t row0 = vld1q_f32(input_ptr0 - padding);
            float32x4_t row1 = vld1q_f32(input_ptr1 - padding);
            float32x4_t row2 = vld1q_f32(input_ptr2 - padding);
            float32x4_t row3 = vld1q_f32(input_ptr3 - padding);
            float32x4_t row4 = vld1q_f32(input_ptr4 - padding);
            float32x4_t acc0 = vmulq_f32(row0, _ker[0]);
            float32x4_t acc1 = vmulq_f32(row2, _ker[0]);
            acc0 = vmlaq_f32(acc0, row1, _ker[1]);
            acc1 = vmlaq_f32(acc1, row3, _ker[1]);
            acc0 = vmlaq_f32(acc0, row2, _ker[2]);
            acc1 = vmlaq_f32(acc1, row4, _ker[2]);
            float sum0 = vgetq_lane_f32(acc0, 2);
            float sum1 = vgetq_lane_f32(acc1, 2);
            if (padding == 1) {
              sum0 += vgetq_lane_f32(acc0, 1);
              sum1 += vgetq_lane_f32(acc1, 1);
            }
            output_ptr0[w] = sum0;
            output_ptr1[w] = sum1;
E
eclipsess 已提交
729 730
          }
        }
731 732 733 734 735 736 737
        input_ptr0 += input_w_start;
        input_ptr1 += input_w_start;
        input_ptr2 += input_w_start;
        input_ptr3 += input_w_start;
        input_ptr4 += input_w_start;
        output_ptr0 += valid_w_start;
        output_ptr1 += valid_w_start;
E
eclipsess 已提交
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 792 793 794 795 796 797 798 799 800 801 802
      // valid
      float32x4_t _result0, _result1, _ext;
      for (int loop = 0; loop < output_w_tiles; ++loop) {
        float32x4x2_t _row0 = vld2q_f32(input_ptr0);
        float32x4x2_t _row1 = vld2q_f32(input_ptr1);

        _ext = vextq_f32(_row0.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr0[8], _ext, 3);
        _result0 = vmulq_lane_f32(_row0.val[0], vget_low_f32(_ker[0]), 0);
        _result0 =
            vmlaq_lane_f32(_result0, _row0.val[1], vget_low_f32(_ker[0]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext, vget_high_f32(_ker[0]), 0);

        _ext = vextq_f32(_row1.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr1[8], _ext, 3);
        _result0 =
            vmlaq_lane_f32(_result0, _row1.val[0], vget_low_f32(_ker[1]), 0);
        _result0 =
            vmlaq_lane_f32(_result0, _row1.val[1], vget_low_f32(_ker[1]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext, vget_high_f32(_ker[1]), 0);

        _row0 = vld2q_f32(input_ptr2);
        _row1 = vld2q_f32(input_ptr3);

        _ext = vextq_f32(_row0.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr2[8], _ext, 3);
        _result0 =
            vmlaq_lane_f32(_result0, _row0.val[0], vget_low_f32(_ker[2]), 0);
        _result0 =
            vmlaq_lane_f32(_result0, _row0.val[1], vget_low_f32(_ker[2]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext, vget_high_f32(_ker[2]), 0);
        _result1 = vmulq_lane_f32(_row0.val[0], vget_low_f32(_ker[0]), 0);
        _result1 =
            vmlaq_lane_f32(_result1, _row0.val[1], vget_low_f32(_ker[0]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext, vget_high_f32(_ker[0]), 0);

        _ext = vextq_f32(_row1.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr3[8], _ext, 3);
        _result1 =
            vmlaq_lane_f32(_result1, _row1.val[0], vget_low_f32(_ker[1]), 0);
        _result1 =
            vmlaq_lane_f32(_result1, _row1.val[1], vget_low_f32(_ker[1]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext, vget_high_f32(_ker[1]), 0);

        _row0 = vld2q_f32(input_ptr4);

        _ext = vextq_f32(_row0.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr4[8], _ext, 3);
        _result1 =
            vmlaq_lane_f32(_result1, _row0.val[0], vget_low_f32(_ker[2]), 0);
        _result1 =
            vmlaq_lane_f32(_result1, _row0.val[1], vget_low_f32(_ker[2]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext, vget_high_f32(_ker[2]), 0);

        vst1q_f32(output_ptr0, _result0);
        vst1q_f32(output_ptr1, _result1);

        input_ptr0 += 8;
        input_ptr1 += 8;
        input_ptr2 += 8;
        input_ptr3 += 8;
        input_ptr4 += 8;
        output_ptr0 += 4;
        output_ptr1 += 4;
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 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
      // remain w
      if (output_w_remain > 0) {
        float32x4x2_t _row0 = vld2q_f32(input_ptr0);
        float32x4x2_t _row1 = vld2q_f32(input_ptr1);

        _ext = vextq_f32(_row0.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr0[8], _ext, 3);
        _result0 = vmulq_lane_f32(_row0.val[0], vget_low_f32(_ker[0]), 0);
        _result0 =
            vmlaq_lane_f32(_result0, _row0.val[1], vget_low_f32(_ker[0]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext, vget_high_f32(_ker[0]), 0);

        _ext = vextq_f32(_row1.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr1[8], _ext, 3);
        _result0 =
            vmlaq_lane_f32(_result0, _row1.val[0], vget_low_f32(_ker[1]), 0);
        _result0 =
            vmlaq_lane_f32(_result0, _row1.val[1], vget_low_f32(_ker[1]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext, vget_high_f32(_ker[1]), 0);

        _row0 = vld2q_f32(input_ptr2);
        _row1 = vld2q_f32(input_ptr3);

        _ext = vextq_f32(_row0.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr2[8], _ext, 3);
        _result0 =
            vmlaq_lane_f32(_result0, _row0.val[0], vget_low_f32(_ker[2]), 0);
        _result0 =
            vmlaq_lane_f32(_result0, _row0.val[1], vget_low_f32(_ker[2]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext, vget_high_f32(_ker[2]), 0);
        _result1 = vmulq_lane_f32(_row0.val[0], vget_low_f32(_ker[0]), 0);
        _result1 =
            vmlaq_lane_f32(_result1, _row0.val[1], vget_low_f32(_ker[0]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext, vget_high_f32(_ker[0]), 0);

        _ext = vextq_f32(_row1.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr3[8], _ext, 3);
        _result1 =
            vmlaq_lane_f32(_result1, _row1.val[0], vget_low_f32(_ker[1]), 0);
        _result1 =
            vmlaq_lane_f32(_result1, _row1.val[1], vget_low_f32(_ker[1]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext, vget_high_f32(_ker[1]), 0);

        _row0 = vld2q_f32(input_ptr4);

        _ext = vextq_f32(_row0.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr4[8], _ext, 3);
        _result1 =
            vmlaq_lane_f32(_result1, _row0.val[0], vget_low_f32(_ker[2]), 0);
        _result1 =
            vmlaq_lane_f32(_result1, _row0.val[1], vget_low_f32(_ker[2]), 1);
        _result1 = vmlaq_lane_f32(_result1, _ext, vget_high_f32(_ker[2]), 0);

        switch (output_w_remain) {
          case 3:
            vst1q_lane_f32(output_ptr0 + 2, _result0, 2);
            vst1q_lane_f32(output_ptr1 + 2, _result1, 2);
          case 2:
            vst1_f32(output_ptr0, vget_low_f32(_result0));
            vst1_f32(output_ptr1, vget_low_f32(_result1));
            break;
          case 1:
            vst1q_lane_f32(output_ptr0, _result0, 0);
            vst1q_lane_f32(output_ptr1, _result1, 0);
            break;
E
eclipsess 已提交
869
        }
870 871 872 873 874 875 876
        input_ptr0 += output_w_remain * 2;
        input_ptr1 += output_w_remain * 2;
        input_ptr2 += output_w_remain * 2;
        input_ptr3 += output_w_remain * 2;
        input_ptr4 += output_w_remain * 2;
        output_ptr0 += output_w_remain;
        output_ptr1 += output_w_remain;
E
eclipsess 已提交
877
      }
878 879 880 881 882 883 884 885 886 887 888 889 890
      // pad right
      if (padding_w > 0) {
        float32x4_t row0 = vld1q_f32(input_ptr0);
        float32x4_t row1 = vld1q_f32(input_ptr1);
        float32x4_t row2 = vld1q_f32(input_ptr2);
        float32x4_t row3 = vld1q_f32(input_ptr3);
        float32x4_t row4 = vld1q_f32(input_ptr4);
        float32x4_t acc0, acc1;
        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;
            *output_ptr1 = 0;
891
          } else {
892 893 894 895 896 897 898 899 900 901 902 903 904 905
            acc0 = vmulq_f32(row0, _ker[0]);
            acc1 = vmulq_f32(row2, _ker[0]);
            acc0 = vmlaq_f32(acc0, row1, _ker[1]);
            acc1 = vmlaq_f32(acc1, row3, _ker[1]);
            acc0 = vmlaq_f32(acc0, row2, _ker[2]);
            acc1 = vmlaq_f32(acc1, row4, _ker[2]);
            float sum0 = vgetq_lane_f32(acc0, 0);
            float sum1 = vgetq_lane_f32(acc1, 0);
            if (padding == 1) {
              sum0 += vgetq_lane_f32(acc0, 1);
              sum1 += vgetq_lane_f32(acc1, 1);
            }
            *output_ptr0 = sum0;
            *output_ptr1 = sum1;
906
          }
907 908
          output_ptr0++;
          output_ptr1++;
909 910
        }
      }
911 912
    }
    // remain height
H
hjchen2 已提交
913
    int start_h = valid_h_start + (valid_h & 0xfffffffe);
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
    if (start_h < valid_h_end) {
      const float *input_ptr0 = input_ptr + (2 * start_h - padding_h) * input_w;
      const float *input_ptr1 = input_ptr0 + input_w;
      const float *input_ptr2 = input_ptr1 + input_w;
      float *output_ptr0 = output_ptr + start_h * output_w;
      // pad left
      if (padding_w) {
        for (int w = valid_w_start - 1; w >= 0; --w) {
          int padding = padding_w - (w << 1);
          if (padding >= 3) {
            output_ptr0[w] = 0;
          } else {
            float32x4_t row0 = vld1q_f32(input_ptr0 - padding);
            float32x4_t row1 = vld1q_f32(input_ptr1 - padding);
            float32x4_t row2 = vld1q_f32(input_ptr2 - padding);
            float32x4_t acc0 = vmulq_f32(row0, _ker[0]);
            acc0 = vmlaq_f32(acc0, row1, _ker[1]);
            acc0 = vmlaq_f32(acc0, row2, _ker[2]);
            float sum0 = vgetq_lane_f32(acc0, 2);
            if (padding == 1) {
              sum0 += vgetq_lane_f32(acc0, 1);
            }
            output_ptr0[w] = sum0;
937 938
          }
        }
939 940 941 942
        input_ptr0 += input_w_start;
        input_ptr1 += input_w_start;
        input_ptr2 += input_w_start;
        output_ptr0 += valid_w_start;
943
      }
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
      // valid
      float32x4_t _result0, _ext;
      for (int loop = 0; loop < output_w_tiles; ++loop) {
        float32x4x2_t _row0 = vld2q_f32(input_ptr0);
        float32x4x2_t _row1 = vld2q_f32(input_ptr1);
        float32x4x2_t _row2 = vld2q_f32(input_ptr2);

        _ext = vextq_f32(_row0.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr0[8], _ext, 3);
        _result0 = vmulq_lane_f32(_row0.val[0], vget_low_f32(_ker[0]), 0);
        _result0 =
            vmlaq_lane_f32(_result0, _row0.val[1], vget_low_f32(_ker[0]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext, vget_high_f32(_ker[0]), 0);

        _ext = vextq_f32(_row1.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr1[8], _ext, 3);
        _result0 =
            vmlaq_lane_f32(_result0, _row1.val[0], vget_low_f32(_ker[1]), 0);
        _result0 =
            vmlaq_lane_f32(_result0, _row1.val[1], vget_low_f32(_ker[1]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext, vget_high_f32(_ker[1]), 0);

        _ext = vextq_f32(_row2.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr2[8], _ext, 3);
        _result0 =
            vmlaq_lane_f32(_result0, _row2.val[0], vget_low_f32(_ker[2]), 0);
        _result0 =
            vmlaq_lane_f32(_result0, _row2.val[1], vget_low_f32(_ker[2]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext, vget_high_f32(_ker[2]), 0);

        vst1q_f32(output_ptr0, _result0);

        input_ptr0 += 8;
        input_ptr1 += 8;
        input_ptr2 += 8;
        output_ptr0 += 4;
980
      }
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
      // remain w
      if (output_w_remain > 0) {
        float32x4x2_t _row0 = vld2q_f32(input_ptr0);
        float32x4x2_t _row1 = vld2q_f32(input_ptr1);
        float32x4x2_t _row2 = vld2q_f32(input_ptr2);

        _ext = vextq_f32(_row0.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr0[8], _ext, 3);
        _result0 = vmulq_lane_f32(_row0.val[0], vget_low_f32(_ker[0]), 0);
        _result0 =
            vmlaq_lane_f32(_result0, _row0.val[1], vget_low_f32(_ker[0]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext, vget_high_f32(_ker[0]), 0);

        _ext = vextq_f32(_row1.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr1[8], _ext, 3);
        _result0 =
            vmlaq_lane_f32(_result0, _row1.val[0], vget_low_f32(_ker[1]), 0);
        _result0 =
            vmlaq_lane_f32(_result0, _row1.val[1], vget_low_f32(_ker[1]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext, vget_high_f32(_ker[1]), 0);

        _ext = vextq_f32(_row2.val[0], _ext, 1);
        _ext = vsetq_lane_f32(input_ptr2[8], _ext, 3);
        _result0 =
            vmlaq_lane_f32(_result0, _row2.val[0], vget_low_f32(_ker[2]), 0);
        _result0 =
            vmlaq_lane_f32(_result0, _row2.val[1], vget_low_f32(_ker[2]), 1);
        _result0 = vmlaq_lane_f32(_result0, _ext, vget_high_f32(_ker[2]), 0);

        switch (output_w_remain) {
          case 3:
            vst1q_lane_f32(output_ptr0 + 2, _result0, 2);
          case 2:
            vst1_f32(output_ptr0, vget_low_f32(_result0));
            break;
          case 1:
            vst1q_lane_f32(output_ptr0, _result0, 0);
            break;
1019
        }
1020 1021 1022 1023
        input_ptr0 += output_w_remain * 2;
        input_ptr1 += output_w_remain * 2;
        input_ptr2 += output_w_remain * 2;
        output_ptr0 += output_w_remain;
1024
      }
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
      // pad right
      if (padding_w) {
        float32x4_t row0 = vld1q_f32(input_ptr0);
        float32x4_t row1 = vld1q_f32(input_ptr1);
        float32x4_t row2 = vld1q_f32(input_ptr2);
        float32x4_t acc0;
        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;
          } else {
            acc0 = vmulq_f32(row0, _ker[0]);
            acc0 = vmlaq_f32(acc0, row1, _ker[1]);
            acc0 = vmlaq_f32(acc0, row2, _ker[2]);
            float sum0 = vgetq_lane_f32(acc0, 0);
            if (padding == 1) {
              sum0 += vgetq_lane_f32(acc0, 1);
            }
            *output_ptr0 = sum0;
1044
          }
1045
          output_ptr0++;
1046 1047 1048
        }
      }
    }
1049
    // pad bottom
1050
    for (int h = valid_h_end; (h < output_h) && (h > valid_h_start - 1); ++h) {
1051 1052 1053 1054
      DepthwiseConv3x3NormalRow<2, 2>(input_ptr, filter_ptr, h, input_h,
                                      input_w, padding_h, padding_w, output_w,
                                      output_ptr, _ker);
    }
1055
  }
E
eclipsess 已提交
1056 1057
}

W
wangliu 已提交
1058 1059 1060
}  // namespace math
}  // namespace operators
}  // namespace paddle_mobile
1061 1062

#endif  // __ARM_NEON__