depthwise_conv3x3.cpp 82.3 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

#include "operators/math/depthwise_conv3x3.h"
#include <vector>
17
#if __ARM_NEON
E
eclipsess 已提交
18
#include <arm_neon.h>
L
liuruilong 已提交
19
#endif
W
wangliu 已提交
20 21 22 23

namespace paddle_mobile {
namespace operators {
namespace math {
H
hjchen2 已提交
24 25 26 27 28 29

void DepthwiseConv3x3(const framework::Tensor *input,
                      const std::vector<int> &strides,
                      const std::vector<int> &paddings,
                      const framework::Tensor *filter, framework::Tensor *bias,
                      framework::Tensor *output, bool if_bias) {
W
wangliu 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  const int batch_size = input->dims()[0];

  const int input_height = input->dims()[2];

  const int input_width = input->dims()[3];

  const int output_channels = output->dims()[1];

  const int output_height = output->dims()[2];
  const int output_width = output->dims()[3];
  const int _kernel_size = 3;
  const int stride_height = strides[0];
  const int stride_width = strides[1];
  const int padding_height = paddings[0];
  const int padding_width = paddings[1];
  const float zero = 0;
  const int input_channel_stride = input_height * input_width;
  const int output_channel_stride = output_height * output_width;
  const int filter_channel_stride = 9;

  const float *input_data = input->data<float>();
  const float *filter_data = filter->data<float>();
  if (if_bias) {
    math::expand_bias(*bias, 1, output->dims());
    output->ShareDataWith(*bias);
  }
  float *output_data = output->mutable_data<float>();

  const int input_batch_stride = output_channels * input_channel_stride;
  const int output_batch_stride = output_channels * output_channel_stride;
  const int filter_batch_stride = output_channels * output_channel_stride;
  const float *pos1, *pos2, *pos3, *filter1, *filter2, *filter3, *output_ptr;
  int hstart, wstart, hend, wend;
  float result;
  for (int i = 0; i < batch_size; ++i) {
    for (int c = 0; c < output_channels; ++c) {
      filter1 = filter_data;
      filter2 = filter1 + 3;
      filter3 = filter2 + 3;

      for (int ph = 0; ph < output_height; ph++) {
        for (int pw = 0; pw < output_width; pw++) {
          hstart = ph * stride_height - padding_height;
          wstart = pw * stride_width - padding_width;
H
hjchen2 已提交
74 75 76 77 78 79
          hend = std::min(hstart + _kernel_size, input_height + padding_height);
          wend = std::min(wstart + _kernel_size, input_width + padding_width);
          hstart = std::max(hstart, 0);
          wstart = std::max(wstart, 0);
          hend = std::min(hend, input_height);
          wend = std::min(wend, input_width);
W
wangliu 已提交
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
          pos1 = input_data + hstart * input_width + wstart;
          pos2 = input_data + (hstart + 1) * input_width + wstart;
          pos3 = input_data + (hstart + 2) * input_width + wstart;
          output_ptr = output_data + ph * output_width + pw;

          if (hend - hstart != 3 || wend - wstart != 3) {
            result = 0;
            float fake_input[9] = {0};
            if (hstart == 0 && wstart == 0) {
              // 左上角
              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (j >= 3 - hend && k >= 3 - wend) {
                    fake_input[3 * j + k] =
                        input_data[(j - (3 - hend)) * input_width + k -
                                   (3 - wend)];
                  }
                }
              }
            } else if (hstart == 0 && wend == input_width) {
              // 右上角
              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (j >= 3 - hend && k <= input_width - wstart - 1) {
                    fake_input[3 * j + k] =
                        input_data[(j - (3 - hend)) * input_width + k + wstart];
                  }
                }
              }

            } else if (hend == input_height && wstart == 0) {
              // 左下角

              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (j <= input_height - 1 - hstart && k >= 3 - wend) {
                    fake_input[3 * j + k] =
                        input_data[(j + hstart) * input_width + k - (3 - wend)];
                  }
                }
              }
            } else if (hend == input_height && wend == input_width) {
              // 右下角
              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (j <= input_height - hstart - 1 &&
                      k <= input_width - wstart - 1) {
                    fake_input[3 * j + k] =
                        input_data[(j + hstart) * input_width + k + wstart];
                  }
                }
              }
            } else if (hstart == 0) {
              // 顶部
              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (j >= 3 - hend) {
                    fake_input[3 * j + k] =
                        input_data[(j - (3 - hend)) * input_width + k + wstart];
                  }
                }
              }

            } else if (hend == input_height) {
              // 底部
              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (j <= input_height - hstart - 1) {
                    fake_input[3 * j + k] =
                        input_data[(j + hstart) * input_width + k + wstart];
                  }
                }
              }

            } else if (wstart == 0) {
              // 左侧
              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (k >= 3 - wend) {
                    fake_input[3 * j + k] =
                        input_data[(j + hstart) * input_width +
                                   (k - (3 - wend))];
                  }
                }
              }

            } else if (wend == input_width) {
              // 右侧
              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (k <= input_width - wstart - 1) {
                    fake_input[3 * j + k] =
                        input_data[(j + hstart) * input_width + k + wstart];
                  }
                }
              }
            }
            for (int l = 0; l < 9; ++l) {
              result += fake_input[l] * filter1[l];
            }
            if (if_bias) {
              output_data[ph * output_width + pw] += result;
            } else {
              output_data[ph * output_width + pw] = result;
            }

          } else {
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
#if __ARM_NEON
#if __aarch64__
            const float32x4_t data1 = vld1q_f32(pos1);
            const float32x4_t data2 = vld1q_f32(pos2);
            const float32x4_t data3 = vld1q_f32(pos3);

            const float32x4_t v_filter1 = vld1q_f32(filter1);
            const float32x4_t v_filter2 = vld1q_f32(filter2);
            const float32x4_t v_filter3 = vld1q_f32(filter3);
            float32x4_t mula = vmulq_f32(data1, v_filter1);
            mula = vmlaq_f32(mula, data2, v_filter2);
            mula = vmlaq_f32(mula, data3, v_filter3);
            float32x2_t res = vpadd_f32(
                vget_high_f32(vsetq_lane_f32(0, mula, 3)), vget_low_f32(mula));
            res = vpadd_f32(res, res);
            if (if_bias) {
              output_data[ph * output_width + pw] += vget_lane_f32(res, 0);
            } else {
              output_data[ph * output_width + pw] = vget_lane_f32(res, 0);
            }
#else
W
wangliu 已提交
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
            asm volatile(

                "vld1.32  {q1}, [%[pos1]]        \n\t"
                "vld1.32  {q4}, [%[filter1]]        \n\t"
                "vmov.f32 q0,    #0.0              \n\t"

                "vld1.32  {q2}, [%[pos2]]        \n\t"
                "vld1.32  {q5}, [%[filter2]]        \n\t"
                "vmla.f32 q0, q1, q4           \n\t"

                "vld1.32  {q3}, [%[pos3]]        \n\t"
                "vld1.32  {q6}, [%[filter3]]        \n\t"

                "vmla.f32 q0, q2, q5           \n\t"
                "vmla.f32 q0, q3, q6          \n\t"

                "vmov.f32 d1[1],  %[zero]         \n\t"

                "vadd.f32  d4, d0, d1           \n\t"
                "vadd.f32  s10, s8, s9            \n\t"
                "vst1.32 {d5[0]},[%[output_ptr]]    \n\t"
                :
                : [input_data] "r"(input_data), [pos1] "r"(pos1),
                  [pos2] "r"(pos2), [pos3] "r"(pos3), [filter1] "r"(filter1),
                  [filter2] "r"(filter2), [filter3] "r"(filter3),
                  [output_ptr] "r"(output_ptr), [zero] "r"(zero)
                : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6");
235
#endif  // __aarch64__
W
wangliu 已提交
236 237
#else

238
#endif  // __ARM_NEON
W
wangliu 已提交
239 240 241 242 243 244 245 246 247 248 249 250
          }
        }
      }
      input_data += input_channel_stride;
      output_data += output_channel_stride;
      filter_data += filter_channel_stride;
    }
    input_data += input_batch_stride;
    output_data += output_batch_stride;
  }
}

H
hjchen2 已提交
251 252 253
void DepthwiseConv3x3s1p1(const framework::Tensor *input,
                          const framework::Tensor *filter,
                          framework::Tensor *output, framework::Tensor *bias,
254
                          bool if_bias, bool if_relu) {
255
#if __ARM_NEON
W
wangliu 已提交
256 257
  const int batch_size = static_cast<int>(input->dims()[0]);
  const int c = static_cast<int>(input->dims()[1]);
258 259
  const int h = static_cast<int>(input->dims()[2]);
  const int w = static_cast<int>(input->dims()[3]);
W
wangliu 已提交
260
  const int hxw = h * w;
261
  // const int l = h;
262 263

  // leftTop, rightTop, leftBottom, rightBottom
264 265 266 267
  const int lt = 0;
  const int rt = w - 1;
  const int lb = (h - 1) * w;
  const int rb = h * w - 1;
268

269 270 271 272
  const float *bias_data;
  if (if_bias) {
    bias_data = bias->data<float>();
  }
273 274
  float32x4_t zero = vdupq_n_f32(0.0);

W
wangliu 已提交
275
  for (int b = 0; b < batch_size; ++b) {
276
#pragma omp parallel for
W
wangliu 已提交
277
    for (int j = 0; j < c; ++j) {
278 279 280 281
      const float *filter_data_tmp = filter->data<float>() + j * 9;
      const float *input_data = input->data<float>() + j * hxw;
      float *output_data = output->mutable_data<float>() + j * hxw;
      float32x4_t vbias;
W
wangliu 已提交
282 283 284 285
      if (if_bias) {
        vbias = vdupq_n_f32(bias_data[j]);
      }

E
eclipsess 已提交
286
      int w_mid = w - 2;  // l=1->l_mid=-1,l=2->l_mid=0
W
wangliu 已提交
287 288 289 290 291 292 293 294 295 296
      float w00 = filter_data_tmp[0];
      float w01 = filter_data_tmp[1];
      float w02 = filter_data_tmp[2];
      float w10 = filter_data_tmp[3];
      float w11 = filter_data_tmp[4];
      float w12 = filter_data_tmp[5];
      float w20 = filter_data_tmp[6];
      float w21 = filter_data_tmp[7];
      float w22 = filter_data_tmp[8];

297 298 299 300 301 302
      output_data[lt] = w11 * input_data[0] + w12 * input_data[1] +
                        w21 * input_data[w] + w22 * input_data[w + 1];
      output_data[rt] = w10 * input_data[w - 2] + w11 * input_data[w - 1] +
                        w20 * input_data[2 * w - 2] +
                        w21 * input_data[2 * w - 1];
      output_data[lb] =
E
eclipsess 已提交
303 304
          w01 * input_data[(h - 2) * w] + w02 * input_data[(h - 2) * w + 1] +
          w11 * input_data[(h - 1) * w] + w12 * input_data[(h - 1) * w + 1];
305
      output_data[rb] =
E
eclipsess 已提交
306 307
          w00 * input_data[h * w - w - 2] + w01 * input_data[h * w - w - 1] +
          w10 * input_data[h * w - 2] + w11 * input_data[h * w - 1];
E
eclipsess 已提交
308
      if (if_bias) {
309 310 311 312 313 314 315 316 317 318
        output_data[lt] += bias_data[j];
        output_data[rt] += bias_data[j];
        output_data[lb] += bias_data[j];
        output_data[rb] += bias_data[j];
      }
      if (if_relu) {
        output_data[lt] = output_data[lt] < 0 ? 0 : output_data[lt];
        output_data[rt] = output_data[rt] < 0 ? 0 : output_data[rt];
        output_data[lb] = output_data[lb] < 0 ? 0 : output_data[lb];
        output_data[rb] = output_data[rb] < 0 ? 0 : output_data[rb];
E
eclipsess 已提交
319
      }
W
wangliu 已提交
320

E
eclipsess 已提交
321
      for (int i = 1; i < h - 1; ++i) {
322 323 324
        int left = i * w;
        int right = i * w + w - 1;
        output_data[left] =
E
eclipsess 已提交
325
            w01 * input_data[i * w - w] + w02 * input_data[i * w - w + 1] +
E
eclipsess 已提交
326
            w11 * input_data[i * w] + w12 * input_data[i * w + 1] +
E
eclipsess 已提交
327 328
            w21 * input_data[i * w + w] + w22 * input_data[i * w + w + 1];

329 330 331 332 333 334
        output_data[right] = w00 * input_data[i * w + w - 1 - w - 1] +
                             w01 * input_data[i * w + w - 1 - w] +
                             w10 * input_data[i * w + w - 1 - 1] +
                             w11 * input_data[i * w + w - 1] +
                             w20 * input_data[i * w + w - 1 + w - 1] +
                             w21 * input_data[i * w + w - 1 + w];
E
eclipsess 已提交
335
        if (if_bias) {
336 337 338 339 340 341
          output_data[left] += bias_data[j];
          output_data[right] += bias_data[j];
        }
        if (if_relu) {
          output_data[left] = output_data[left] < 0 ? 0 : output_data[left];
          output_data[right] = output_data[right] < 0 ? 0 : output_data[right];
E
eclipsess 已提交
342
        }
W
wangliu 已提交
343 344 345 346 347 348 349 350
      }

      // top 1 row and bottom 1 row
      const float *input_tmp = input_data;

      float32x4_t in0, in1, in2, in3, in4, in5, in6, in7, tmp0, tmp1, tmp2,
          tmp3, tmp4, tmp5, out0;
      in0 = vld1q_f32(input_tmp);
E
eclipsess 已提交
351 352
      in2 = vld1q_f32(input_tmp + w);
      const float *input_tmp_end = input_tmp + (h - 2) * w;
W
wangliu 已提交
353
      in4 = vld1q_f32(input_tmp_end);
E
eclipsess 已提交
354 355
      in6 = vld1q_f32(input_tmp_end + w);
      int c_mid = w_mid;
W
wangliu 已提交
356 357 358
      auto output_ptr = output_data + 1;
      for (; c_mid > 3; c_mid -= 4) {
        in1 = vld1q_f32(input_tmp + 4);
E
eclipsess 已提交
359
        in3 = vld1q_f32(input_tmp + w + 4);
W
wangliu 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373

        tmp0 = vextq_f32(in0, in1, 1);
        tmp1 = vextq_f32(in0, in1, 2);

        tmp2 = vextq_f32(in2, in3, 1);
        tmp3 = vextq_f32(in2, in3, 2);

        out0 = vmulq_n_f32(in0, w10);
        out0 = vmlaq_n_f32(out0, tmp0, w11);
        out0 = vmlaq_n_f32(out0, tmp1, w12);
        out0 = vmlaq_n_f32(out0, in2, w20);
        out0 = vmlaq_n_f32(out0, tmp2, w21);
        out0 = vmlaq_n_f32(out0, tmp3, w22);
        out0 = vaddq_f32(out0, vbias);
374 375 376
        if (if_relu) {
          out0 = vmaxq_f32(out0, zero);
        }
W
wangliu 已提交
377 378 379
        vst1q_f32(output_ptr, out0);

        in5 = vld1q_f32(input_tmp_end + 4);
E
eclipsess 已提交
380
        in7 = vld1q_f32(input_tmp_end + w + 4);
W
wangliu 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393

        tmp0 = vextq_f32(in4, in5, 1);
        tmp1 = vextq_f32(in4, in5, 2);
        tmp2 = vextq_f32(in6, in7, 1);
        tmp3 = vextq_f32(in6, in7, 2);

        out0 = vmulq_n_f32(in4, w00);
        out0 = vmlaq_n_f32(out0, tmp0, w01);
        out0 = vmlaq_n_f32(out0, tmp1, w02);
        out0 = vmlaq_n_f32(out0, in6, w10);
        out0 = vmlaq_n_f32(out0, tmp2, w11);
        out0 = vmlaq_n_f32(out0, tmp3, w12);
        out0 = vaddq_f32(out0, vbias);
394 395 396
        if (if_relu) {
          out0 = vmaxq_f32(out0, zero);
        }
E
eclipsess 已提交
397
        vst1q_f32(output_ptr + (h - 1) * w, out0);
W
wangliu 已提交
398 399 400 401 402 403 404 405 406 407 408 409

        // can optimize to each 8 stride.
        input_tmp += 4;
        input_tmp_end += 4;
        output_ptr += 4;
        in0 = in1;
        in2 = in3;
        in4 = in5;
        in6 = in7;
      }

      // top right pad
E
eclipsess 已提交
410 411
      float32x4_t pad0 = vdupq_n_f32(input_data[w - 1]);
      float32x4_t pad1 = vdupq_n_f32(input_data[2 * w - 1]);
W
wangliu 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424

      tmp0 = vextq_f32(in0, pad0, 1);
      tmp1 = vextq_f32(in0, pad0, 2);
      tmp2 = vextq_f32(in2, pad1, 1);
      tmp3 = vextq_f32(in2, pad1, 2);

      out0 = vmulq_n_f32(in0, w10);
      out0 = vmlaq_n_f32(out0, tmp0, w11);
      out0 = vmlaq_n_f32(out0, tmp1, w12);
      out0 = vmlaq_n_f32(out0, in2, w20);
      out0 = vmlaq_n_f32(out0, tmp2, w21);
      out0 = vmlaq_n_f32(out0, tmp3, w22);
      out0 = vaddq_f32(out0, vbias);
425 426 427
      if (if_relu) {
        out0 = vmaxq_f32(out0, zero);
      }
W
wangliu 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441

      for (int i = 0; i < c_mid; ++i) {
        if (i == 0) {
          vst1q_lane_f32(output_ptr + i, out0, 0);
        }
        if (i == 1) {
          vst1q_lane_f32(output_ptr + i, out0, 1);
        }
        if (i == 2) {
          vst1q_lane_f32(output_ptr + i, out0, 2);
        }
      }

      // bottom right pad
E
eclipsess 已提交
442 443
      float32x4_t pad2 = vdupq_n_f32(input_data[h * w - 1 - w]);
      float32x4_t pad3 = vdupq_n_f32(input_data[h * w - 1]);
W
wangliu 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456

      tmp0 = vextq_f32(in4, pad2, 1);
      tmp1 = vextq_f32(in4, pad2, 2);
      tmp2 = vextq_f32(in6, pad3, 1);
      tmp3 = vextq_f32(in6, pad3, 2);

      out0 = vmulq_n_f32(in4, w00);
      out0 = vmlaq_n_f32(out0, tmp0, w01);
      out0 = vmlaq_n_f32(out0, tmp1, w02);
      out0 = vmlaq_n_f32(out0, in6, w10);
      out0 = vmlaq_n_f32(out0, tmp2, w11);
      out0 = vmlaq_n_f32(out0, tmp3, w12);
      out0 = vaddq_f32(out0, vbias);
457 458 459
      if (if_relu) {
        out0 = vmaxq_f32(out0, zero);
      }
W
wangliu 已提交
460 461 462

      for (int i = 0; i < c_mid; ++i) {
        if (i == 0) {
E
eclipsess 已提交
463
          vst1q_lane_f32(output_ptr + (h - 1) * w + i, out0, 0);
W
wangliu 已提交
464 465
        }
        if (i == 1) {
E
eclipsess 已提交
466
          vst1q_lane_f32(output_ptr + (h - 1) * w + i, out0, 1);
W
wangliu 已提交
467 468
        }
        if (i == 2) {
E
eclipsess 已提交
469
          vst1q_lane_f32(output_ptr + (h - 1) * w + i, out0, 2);
W
wangliu 已提交
470 471 472 473
        }
      }
      // mid

E
eclipsess 已提交
474 475 476
      for (int i = 0; i < h - 2; ++i) {
        auto output_ptr = output_data + (i + 1) * w + 1;
        input_tmp = input_data + i * w;
W
wangliu 已提交
477
        auto in0_tmp = vld1q_f32(input_tmp);
E
eclipsess 已提交
478 479 480
        auto in2_tmp = vld1q_f32(input_tmp + w);
        auto in4_tmp = vld1q_f32(input_tmp + w + w);
        c_mid = w_mid;
W
wangliu 已提交
481 482
        for (; c_mid > 3; c_mid -= 4) {
          auto in1_tmp = vld1q_f32(input_tmp + 4);
E
eclipsess 已提交
483 484
          auto in3_tmp = vld1q_f32(input_tmp + w + 4);
          auto in5_tmp = vld1q_f32(input_tmp + w + w + 4);
W
wangliu 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

          tmp0 = vextq_f32(in0_tmp, in1_tmp, 1);
          tmp1 = vextq_f32(in0_tmp, in1_tmp, 2);
          tmp2 = vextq_f32(in2_tmp, in3_tmp, 1);
          tmp3 = vextq_f32(in2_tmp, in3_tmp, 2);
          tmp4 = vextq_f32(in4_tmp, in5_tmp, 1);
          tmp5 = vextq_f32(in4_tmp, in5_tmp, 2);

          out0 = vmulq_n_f32(in0_tmp, w00);
          out0 = vmlaq_n_f32(out0, tmp0, w01);
          out0 = vmlaq_n_f32(out0, tmp1, w02);
          out0 = vmlaq_n_f32(out0, in2_tmp, w10);
          out0 = vmlaq_n_f32(out0, tmp2, w11);
          out0 = vmlaq_n_f32(out0, tmp3, w12);
          out0 = vmlaq_n_f32(out0, in4_tmp, w20);
          out0 = vmlaq_n_f32(out0, tmp4, w21);
          out0 = vmlaq_n_f32(out0, tmp5, w22);
          out0 = vaddq_f32(out0, vbias);
503 504 505
          if (if_relu) {
            out0 = vmaxq_f32(out0, zero);
          }
W
wangliu 已提交
506 507 508 509 510 511 512 513 514 515

          vst1q_f32(output_ptr, out0);

          output_ptr += 4;
          input_tmp += 4;
          in0_tmp = in1_tmp;
          in2_tmp = in3_tmp;
          in4_tmp = in5_tmp;
        }

E
eclipsess 已提交
516 517 518
        float32x4_t pad0 = vdupq_n_f32(input_data[i * w + w - 1]);
        float32x4_t pad1 = vdupq_n_f32(input_data[i * w + w - 1 + w]);
        float32x4_t pad2 = vdupq_n_f32(input_data[i * w + w - 1 + w + w]);
W
wangliu 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536

        tmp0 = vextq_f32(in0_tmp, pad0, 1);
        tmp1 = vextq_f32(in0_tmp, pad0, 2);
        tmp2 = vextq_f32(in2_tmp, pad1, 1);
        tmp3 = vextq_f32(in2_tmp, pad1, 2);
        tmp4 = vextq_f32(in4_tmp, pad2, 1);
        tmp5 = vextq_f32(in4_tmp, pad2, 2);

        out0 = vmulq_n_f32(in0_tmp, w00);
        out0 = vmlaq_n_f32(out0, tmp0, w01);
        out0 = vmlaq_n_f32(out0, tmp1, w02);
        out0 = vmlaq_n_f32(out0, in2_tmp, w10);
        out0 = vmlaq_n_f32(out0, tmp2, w11);
        out0 = vmlaq_n_f32(out0, tmp3, w12);
        out0 = vmlaq_n_f32(out0, in4_tmp, w20);
        out0 = vmlaq_n_f32(out0, tmp4, w21);
        out0 = vmlaq_n_f32(out0, tmp5, w22);
        out0 = vaddq_f32(out0, vbias);
537 538 539
        if (if_relu) {
          out0 = vmaxq_f32(out0, zero);
        }
W
wangliu 已提交
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554

        for (int i = 0; i < c_mid; ++i) {
          if (i == 0) {
            vst1q_lane_f32(output_ptr + i, out0, 0);
          }
          if (i == 1) {
            vst1q_lane_f32(output_ptr + i, out0, 1);
          }
          if (i == 2) {
            vst1q_lane_f32(output_ptr + i, out0, 2);
          }
        }
      }
    }
  }
L
liuruilong 已提交
555
#endif
W
wangliu 已提交
556
}
E
eclipsess 已提交
557

H
hjchen2 已提交
558 559 560 561 562 563
void DepthwiseConvAddBNRelu3x3s1p1(const framework::Tensor *input,
                                   const framework::Tensor *filter,
                                   framework::Tensor *output,
                                   const framework::Tensor *new_scale,
                                   const framework::Tensor *new_bias,
                                   bool if_relu) {
564
#if __ARM_NEON
E
eclipsess 已提交
565
  const float *input_data = input->data<float>();
E
eclipsess 已提交
566
  const float *filter_data = filter->data<float>();
567
  float *output_data = output->mutable_data<float>();
E
eclipsess 已提交
568 569 570 571
  const float *newscale_data = new_scale->data<float>();
  const float *newbias_data = new_bias->data<float>();

  const int batch_size = static_cast<int>(input->dims()[0]);
572 573 574 575 576 577 578 579 580
  const int input_channel = static_cast<int>(input->dims()[1]);

  const int input_height = static_cast<int>(input->dims()[2]);
  const int input_width = static_cast<int>(input->dims()[3]);
  const int output_height = static_cast<int>(output->dims()[2]);
  const int output_width = static_cast<int>(output->dims()[3]);

  const int hxw = input_height * input_width;

E
eclipsess 已提交
581 582 583
  //  const int l = input_height;
  const int h = input_height;
  const int w = input_width;
E
eclipsess 已提交
584 585
  float32x4_t vzero = vdupq_n_f32(0);

586
  for (int b = 0; b < batch_size; b++) {
587
#pragma omp parallel for
588
    for (int c = 0; c < input_channel; c++) {
589 590 591 592 593
      const float *filter_data = filter->data<float>() + c * 9;
      const float *input_data = input->data<float>() + c * hxw;
      float *output_data = output->data<float>() + c * hxw;
      float32x4_t vnewbias = vdupq_n_f32(newbias_data[c]);
      float32x4_t vnewscale = vdupq_n_f32(newscale_data[c]);
594 595 596 597 598 599 600 601 602 603

      float w00 = filter_data[0];
      float w01 = filter_data[1];
      float w02 = filter_data[2];
      float w10 = filter_data[3];
      float w11 = filter_data[4];
      float w12 = filter_data[5];
      float w20 = filter_data[6];
      float w21 = filter_data[7];
      float w22 = filter_data[8];
E
eclipsess 已提交
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
      for (int i = 1; i < output_height - 1; i++) {
        float *output_ptr;
        float32x4_t in0, in1, in2, in3, in4, in5, tmp0, tmp1, tmp2, tmp3, tmp4,
            tmp5, out0;
        for (int m = 1; m < output_width - 4; m += 4) {
          output_ptr = output_data + i * output_width + m;
          in0 = vld1q_f32(input_data + (i - 1) * input_width + m - 1);
          in1 = vld1q_f32(input_data + (i - 1) * input_width + m + 3);
          in2 = vld1q_f32(input_data + i * input_width + m - 1);
          in3 = vld1q_f32(input_data + i * input_width + m + 3);
          in4 = vld1q_f32(input_data + (i + 1) * input_width + m - 1);
          in5 = vld1q_f32(input_data + (i + 1) * input_width + m + 3);

          tmp0 = vextq_f32(in0, in1, 1);
          tmp1 = vextq_f32(in0, in1, 2);
          tmp2 = vextq_f32(in2, in3, 1);
          tmp3 = vextq_f32(in2, in3, 2);
          tmp4 = vextq_f32(in4, in5, 1);
          tmp5 = vextq_f32(in4, in5, 2);

          out0 = vmulq_n_f32(in0, w00);
          out0 = vmlaq_n_f32(out0, tmp0, w01);
          out0 = vmlaq_n_f32(out0, tmp1, w02);
          out0 = vmlaq_n_f32(out0, in2, w10);
          out0 = vmlaq_n_f32(out0, tmp2, w11);
          out0 = vmlaq_n_f32(out0, tmp3, w12);
          out0 = vmlaq_n_f32(out0, in4, w20);
          out0 = vmlaq_n_f32(out0, tmp4, w21);
          out0 = vmlaq_n_f32(out0, tmp5, w22);

          out0 = vmlaq_f32(vnewbias, vnewscale, out0);
          if (if_relu) {
            out0 = vmaxq_f32(out0, vzero);
          }
          vst1q_f32(output_ptr, out0);
        }
        int m;
        for (m = 1; (m + 3) < output_width - 1; m = m + 4) {
        }

        for (int j = m; j < output_width - 1; j++) {
          output_data[i * output_width + j] =
              input_data[(i - 1) * input_width + j - 1] * w00 +
              input_data[(i - 1) * input_width + j] * w01 +
              input_data[(i - 1) * input_width + j + 1] * w02 +
              input_data[(i)*input_width + j - 1] * w10 +
              input_data[(i)*input_width + j] * w11 +
              input_data[(i)*input_width + j + 1] * w12 +
              input_data[(i + 1) * input_width + j - 1] * w20 +
              input_data[(i + 1) * input_width + j] * w21 +
              input_data[(i + 1) * input_width + j + 1] * w22;
          output_data[i * output_width + j] =
              newscale_data[c] * output_data[i * output_width + j] +
              newbias_data[c];
          if (if_relu) {
            output_data[i * output_width + j] =
                output_data[i * output_width + j] < 0
                    ? 0
                    : output_data[i * output_width + j];
          }
        }
      }

E
eclipsess 已提交
668
      output_data[0] = w11 * input_data[0] + w12 * input_data[1] +
E
eclipsess 已提交
669 670 671 672 673 674 675 676 677 678
                       w21 * input_data[w] + w22 * input_data[w + 1];
      output_data[w - 1] = w10 * input_data[w - 2] + w11 * input_data[w - 1] +
                           w20 * input_data[2 * w - 2] +
                           w21 * input_data[2 * w - 1];
      output_data[(h - 1) * w] =
          w01 * input_data[(h - 2) * w] + w02 * input_data[(h - 2) * w + 1] +
          w11 * input_data[(h - 1) * w] + w12 * input_data[(h - 1) * w + 1];
      output_data[h * w - 1] =
          w00 * input_data[h * w - w - 2] + w01 * input_data[h * w - w - 1] +
          w10 * input_data[h * w - 2] + w11 * input_data[h * w - 1];
679
      output_data[0] = output_data[0] * newscale_data[c] + newbias_data[c];
E
eclipsess 已提交
680 681 682 683 684 685
      output_data[w - 1] =
          output_data[w - 1] * newscale_data[c] + newbias_data[c];
      output_data[(h - 1) * w] =
          output_data[(h - 1) * w] * newscale_data[c] + newbias_data[c];
      output_data[h * w - 1] =
          output_data[h * w - 1] * newscale_data[c] + newbias_data[c];
686

E
eclipsess 已提交
687 688
      if (if_relu) {
        output_data[0] = output_data[0] < 0 ? 0 : output_data[0];
E
eclipsess 已提交
689 690 691 692 693
        output_data[w - 1] = output_data[w - 1] < 0 ? 0 : output_data[w - 1];
        output_data[(h - 1) * w] =
            output_data[(h - 1) * w] < 0 ? 0 : output_data[(h - 1) * w];
        output_data[h * w - 1] =
            output_data[h * w - 1] < 0 ? 0 : output_data[h * w - 1];
E
eclipsess 已提交
694
      }
E
eclipsess 已提交
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
      for (int i = 1; i < h - 1; ++i) {
        output_data[i * w] =
            w01 * input_data[i * w - w] + w02 * input_data[i * w - w + 1] +
            w11 * input_data[i * w] + w12 * input_data[i * w + 1] +
            w21 * input_data[i * w + w] + w22 * input_data[i * w + w + 1];

        output_data[i * w + w - 1] = w00 * input_data[i * w + w - 1 - w - 1] +
                                     w01 * input_data[i * w + w - 1 - w] +
                                     w10 * input_data[i * w + w - 1 - 1] +
                                     w11 * input_data[i * w + w - 1] +
                                     w20 * input_data[i * w + w - 1 + w - 1] +
                                     w21 * input_data[i * w + w - 1 + w];
        output_data[i * w] =
            output_data[i * w] * newscale_data[c] + newbias_data[c];
        output_data[i * w + w - 1] =
            output_data[i * w + w - 1] * newscale_data[c] + newbias_data[c];
711

E
eclipsess 已提交
712
        if (if_relu) {
E
eclipsess 已提交
713 714 715
          output_data[i * w] = output_data[i * w] < 0 ? 0 : output_data[i * w];
          output_data[i * w + w - 1] =
              output_data[i * w + w - 1] < 0 ? 0 : output_data[i * w + w - 1];
E
eclipsess 已提交
716 717 718
        }
      }

719 720 721 722 723 724 725 726
      int m;
      for (m = 1; m < output_width - 4; m += 4) {
        float *output_ptr = output_data + m;
        float32x4_t in0, in1, in2, in3, tmp0, tmp1, tmp2, tmp3, out0;
        in0 = vld1q_f32(input_data + m - 1);
        in1 = vld1q_f32(input_data + m + 3);
        in2 = vld1q_f32(input_data + input_width + m - 1);
        in3 = vld1q_f32(input_data + input_width + m + 3);
E
eclipsess 已提交
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
        tmp0 = vextq_f32(in0, in1, 1);
        tmp1 = vextq_f32(in0, in1, 2);
        tmp2 = vextq_f32(in2, in3, 1);
        tmp3 = vextq_f32(in2, in3, 2);
        out0 = vmulq_n_f32(in0, w10);
        out0 = vmlaq_n_f32(out0, tmp0, w11);
        out0 = vmlaq_n_f32(out0, tmp1, w12);
        out0 = vmlaq_n_f32(out0, in2, w20);
        out0 = vmlaq_n_f32(out0, tmp2, w21);
        out0 = vmlaq_n_f32(out0, tmp3, w22);
        out0 = vmlaq_f32(vnewbias, vnewscale, out0);
        if (if_relu) {
          out0 = vmaxq_f32(out0, vzero);
        }
        vst1q_f32(output_ptr, out0);
742
      }
743 744

      for (m = 1; (m + 3) < output_width - 1; m += 4) {
745 746 747 748 749 750 751 752
      }
      for (int j = m; j < output_width - 1; j++) {
        output_data[j] = input_data[j - 1] * w10 + input_data[j] * w11 +
                         input_data[j + 1] * w12 +
                         input_data[input_width + j - 1] * w20 +
                         input_data[input_width + j] * w21 +
                         input_data[input_width + j + 1] * w22;
        output_data[j] = output_data[j] * newscale_data[c] + newbias_data[c];
E
eclipsess 已提交
753

754 755 756 757
        if (if_relu) {
          output_data[j] = output_data[j] < 0 ? 0 : output_data[j];
        }
      }
E
eclipsess 已提交
758

759
      for (m = 1; m < output_width - 4; m += 4) {
760 761
        float *output_ptr =
            output_data + (output_height - 1) * output_width + m;
E
eclipsess 已提交
762

763 764 765 766 767 768 769 770 771 772
        float32x4_t in0, in1, in2, in3, tmp0, tmp1, tmp2, tmp3, out0;
        in0 = vld1q_f32(input_data + (output_height - 2) * input_width + m - 1);
        in1 = vld1q_f32(input_data + (output_height - 2) * input_width + m + 3);
        in2 = vld1q_f32(input_data + (output_height - 1) * input_width + m - 1);
        in3 = vld1q_f32(input_data + (output_height - 1) * input_width + m + 3);
        tmp0 = vextq_f32(in0, in1, 1);
        tmp1 = vextq_f32(in0, in1, 2);
        tmp2 = vextq_f32(in2, in3, 1);
        tmp3 = vextq_f32(in2, in3, 2);
        out0 = vmulq_n_f32(in0, w00);
E
eclipsess 已提交
773 774
        out0 = vmlaq_n_f32(out0, tmp0, w01);
        out0 = vmlaq_n_f32(out0, tmp1, w02);
775
        out0 = vmlaq_n_f32(out0, in2, w10);
E
eclipsess 已提交
776 777 778 779 780 781
        out0 = vmlaq_n_f32(out0, tmp2, w11);
        out0 = vmlaq_n_f32(out0, tmp3, w12);
        out0 = vmlaq_f32(vnewbias, vnewscale, out0);
        if (if_relu) {
          out0 = vmaxq_f32(out0, vzero);
        }
782
        vst1q_f32(output_ptr, out0);
E
eclipsess 已提交
783
      }
784 785 786 787 788 789 790 791 792 793 794 795 796 797
      for (m = 1; (m + 3) < output_width - 1; m = m + 4) {
      }
      for (int j = m; j < output_width - 1; j++) {
        output_data[(output_height - 1) * input_width + j] =
            input_data[(output_height - 2) * input_width + j - 1] * w00 +
            input_data[(output_height - 2) * input_width + j] * w01 +
            input_data[(output_height - 2) * input_width + j + 1] * w02 +
            input_data[(output_height - 1) * input_width + j - 1] * w10 +
            input_data[(output_height - 1) * input_width + j] * w11 +
            input_data[(output_height - 1) * input_width + j + 1] * w12;
        output_data[(output_height - 1) * output_width + j] =
            output_data[(output_height - 1) * output_width + j] *
                newscale_data[c] +
            newbias_data[c];
E
eclipsess 已提交
798

799 800 801 802 803 804
        if (if_relu) {
          output_data[(output_height - 1) * output_width + j] =
              output_data[(output_height - 1) * output_width + j] < 0
                  ? 0
                  : output_data[(output_height - 1) * output_width + j];
        }
E
eclipsess 已提交
805
      }
806 807
    }
  }
E
eclipsess 已提交
808

809
    /*
810 811 812 813 814 815 816 817
        const float *input_data = input->data<float>();
        const float *filter_data = filter->data<float>();
        float *output_data = output->data<float>();
        const float *newscale_data = new_scale->data<float>();
        const float *newbias_data = new_bias->data<float>();

        const int h = static_cast<int>(input->dims()[2]);
        const int w = static_cast<int>(input->dims()[3]);
E
eclipsess 已提交
818
//        const int l = h;
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833

        const int batch_size = static_cast<int>(input->dims()[0]);
        const int c = static_cast<int>(input->dims()[1]);
        const int hxw = h * w;
        float32x4_t vnewbias = vdupq_n_f32(0.0);
        float32x4_t vnewscale = vdupq_n_f32(1.0);
        float32x4_t vzero = vdupq_n_f32(0);

        for (int b = 0; b < batch_size; ++b) {
          const float *filter_data_tmp = filter_data;

          for (int j = 0; j < c; ++j) {
            vnewbias = vdupq_n_f32(newbias_data[j]);
            vnewscale = vdupq_n_f32(newscale_data[j]);

E
eclipsess 已提交
834
            int w_mid = w - 2;  // l=1->l_mid=-1,l=2->l_mid=0
835 836 837 838 839 840 841 842 843 844 845
            float w00 = filter_data_tmp[0];
            float w01 = filter_data_tmp[1];
            float w02 = filter_data_tmp[2];
            float w10 = filter_data_tmp[3];
            float w11 = filter_data_tmp[4];
            float w12 = filter_data_tmp[5];
            float w20 = filter_data_tmp[6];
            float w21 = filter_data_tmp[7];
            float w22 = filter_data_tmp[8];

            output_data[0] = w11 * input_data[0] + w12 * input_data[1] +
E
eclipsess 已提交
846 847 848 849 850 851 852 853 854 855 856 857
                             w21 * input_data[w] + w22 * input_data[w + 1];

            output_data[w - 1] = w10 * input_data[w - 2] + w11 * input_data[w -
       1] + w20 * input_data[2 * w - 2] + w21 * input_data[2 * w - 1];

            output_data[(h - 1) * w] =
                w01 * input_data[(h - 2) * w] + w02 * input_data[(h - 2) * w +
       1] + w11 * input_data[(h - 1) * w] + w12 * input_data[(h - 1) * w + 1];
            output_data[h * w - 1] = w00 * input_data[h*w-w-2] +
                                     w01 * input_data[h*w-w-1] +
                                     w10 * input_data[h * w - 2] +
                                     w11 * input_data[h * w - 1];
858
            output_data[0] = output_data[0] * newscale_data[j] +
E
eclipsess 已提交
859 860 861 862 863
       newbias_data[j]; output_data[w - 1] = output_data[w - 1] *
       newscale_data[j] + newbias_data[j]; output_data[(h - 1) * w] =
                output_data[(h - 1) * w] * newscale_data[j] + newbias_data[j];
            output_data[h * w - 1] =
                output_data[h * w - 1] * newscale_data[j] + newbias_data[j];
E
eclipsess 已提交
864

865 866
            if (if_relu) {
              output_data[0] = output_data[0] < 0 ? 0 : output_data[0];
E
eclipsess 已提交
867 868 869 870
              output_data[w - 1] = output_data[w - 1] < 0 ? 0 : output_data[w -
       1]; output_data[(h - 1) * w] = output_data[(h - 1) * w] < 0 ? 0 :
       output_data[(h - 1) * w]; output_data[h * w - 1] = output_data[h * w - 1]
       < 0 ? 0 : output_data[h * w - 1];
871
            }
E
eclipsess 已提交
872 873 874 875 876 877 878 879 880 881 882
            for (int i = 1; i < h - 1; ++i) {
              output_data[i * w] =
                  w01 * input_data[i * w - w] + w02 * input_data[i * w - w + 1]
       + w11 * input_data[i * w] + w12 * input_data[i * w + 1] + w21 *
       input_data[i * w + w] + w22 * input_data[i * w + w + 1]; output_data[i *
       w + w - 1] = w00 * input_data[i * w + w - 1 - w - 1] + w01 * input_data[i
       * w + w - 1 - w] + w10 * input_data[i * w + w - 1 - 1] + w11 *
       input_data[i * w + w - 1] + w20 * input_data[i * w + w - 1 + w - 1] + w21
       * input_data[i * w + w - 1 + w]; output_data[i * w] = output_data[i * w]
       * newscale_data[j] + newbias_data[j]; output_data[i * w + w - 1] =
                  output_data[i * w + w - 1] * newscale_data[j] +
883 884 885
       newbias_data[j];

              if (if_relu) {
E
eclipsess 已提交
886 887 888
                output_data[i * w] = output_data[i * w] < 0 ? 0 : output_data[i
       * w]; output_data[i * w + w - 1] = output_data[i * w + w - 1] < 0 ? 0 :
       output_data[i * w + w - 1];
889 890
              }
            }
E
eclipsess 已提交
891

892 893 894 895 896
            // top 1 row and bottom 1 row
            const float *input_tmp = input_data;

            float32x4_t in0, in1, in2, in3, in4, in5, in6, in7, tmp0, tmp1,
       tmp2, tmp3, tmp4, tmp5, out0; in0 = vld1q_f32(input_tmp); in2 =
E
eclipsess 已提交
897 898 899
       vld1q_f32(input_tmp + w); const float *input_tmp_end = input_tmp + (h -
       2) * w; in4 = vld1q_f32(input_tmp_end); in6 = vld1q_f32(input_tmp_end +
       w); int c_mid = w_mid; auto output_ptr = output_data + 1; for (; c_mid >
900
       3; c_mid -= 4) { in1 = vld1q_f32(input_tmp + 4); in3 =
E
eclipsess 已提交
901
       vld1q_f32(input_tmp + w + 4);
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921

              tmp0 = vextq_f32(in0, in1, 1);
              tmp1 = vextq_f32(in0, in1, 2);

              tmp2 = vextq_f32(in2, in3, 1);
              tmp3 = vextq_f32(in2, in3, 2);

              out0 = vmulq_n_f32(in0, w10);
              out0 = vmlaq_n_f32(out0, tmp0, w11);
              out0 = vmlaq_n_f32(out0, tmp1, w12);
              out0 = vmlaq_n_f32(out0, in2, w20);
              out0 = vmlaq_n_f32(out0, tmp2, w21);
              out0 = vmlaq_n_f32(out0, tmp3, w22);
              out0 = vmlaq_f32(vnewbias, vnewscale, out0);
              if (if_relu) {
                out0 = vmaxq_f32(out0, vzero);
              }
              vst1q_f32(output_ptr, out0);

              in5 = vld1q_f32(input_tmp_end + 4);
E
eclipsess 已提交
922
              in7 = vld1q_f32(input_tmp_end + w + 4);
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938

              tmp0 = vextq_f32(in4, in5, 1);
              tmp1 = vextq_f32(in4, in5, 2);
              tmp2 = vextq_f32(in6, in7, 1);
              tmp3 = vextq_f32(in6, in7, 2);

              out0 = vmulq_n_f32(in4, w00);
              out0 = vmlaq_n_f32(out0, tmp0, w01);
              out0 = vmlaq_n_f32(out0, tmp1, w02);
              out0 = vmlaq_n_f32(out0, in6, w10);
              out0 = vmlaq_n_f32(out0, tmp2, w11);
              out0 = vmlaq_n_f32(out0, tmp3, w12);
              out0 = vmlaq_f32(vnewbias, vnewscale, out0);
              if (if_relu) {
                out0 = vmaxq_f32(out0, vzero);
              }
E
eclipsess 已提交
939
              vst1q_f32(output_ptr + (h - 1) * w, out0);
940 941 942 943 944 945 946 947 948 949

              // can optimize to each 8 stride.
              input_tmp += 4;
              input_tmp_end += 4;
              output_ptr += 4;
              in0 = in1;
              in2 = in3;
              in4 = in5;
              in6 = in7;
            }
E
eclipsess 已提交
950

951
            // top right pad
E
eclipsess 已提交
952 953
            float32x4_t pad0 = vdupq_n_f32(input_data[w - 1]);
            float32x4_t pad1 = vdupq_n_f32(input_data[2 * w - 1]);
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 980

            tmp0 = vextq_f32(in0, pad0, 1);
            tmp1 = vextq_f32(in0, pad0, 2);
            tmp2 = vextq_f32(in2, pad1, 1);
            tmp3 = vextq_f32(in2, pad1, 2);

            out0 = vmulq_n_f32(in0, w10);
            out0 = vmlaq_n_f32(out0, tmp0, w11);
            out0 = vmlaq_n_f32(out0, tmp1, w12);
            out0 = vmlaq_n_f32(out0, in2, w20);
            out0 = vmlaq_n_f32(out0, tmp2, w21);
            out0 = vmlaq_n_f32(out0, tmp3, w22);
            out0 = vmlaq_f32(vnewbias, vnewscale, out0);
            if (if_relu) {
              out0 = vmaxq_f32(out0, vzero);
            }
            for (int i = 0; i < c_mid; ++i) {
              if (i == 0) {
                vst1q_lane_f32(output_ptr + i, out0, 0);
              }
              if (i == 1) {
                vst1q_lane_f32(output_ptr + i, out0, 1);
              }
              if (i == 2) {
                vst1q_lane_f32(output_ptr + i, out0, 2);
              }
            }
981

982
            // bottom right pad
E
eclipsess 已提交
983 984
            float32x4_t pad2 = vdupq_n_f32(input_data[h * w - 1 - w]);
            float32x4_t pad3 = vdupq_n_f32(input_data[h * w - 1]);
985

986 987 988 989
            tmp0 = vextq_f32(in4, pad2, 1);
            tmp1 = vextq_f32(in4, pad2, 2);
            tmp2 = vextq_f32(in6, pad3, 1);
            tmp3 = vextq_f32(in6, pad3, 2);
990

991
            out0 = vmulq_n_f32(in4, w00);
992 993
            out0 = vmlaq_n_f32(out0, tmp0, w01);
            out0 = vmlaq_n_f32(out0, tmp1, w02);
994
            out0 = vmlaq_n_f32(out0, in6, w10);
995 996 997 998 999 1000
            out0 = vmlaq_n_f32(out0, tmp2, w11);
            out0 = vmlaq_n_f32(out0, tmp3, w12);
            out0 = vmlaq_f32(vnewbias, vnewscale, out0);
            if (if_relu) {
              out0 = vmaxq_f32(out0, vzero);
            }
1001 1002
            for (int i = 0; i < c_mid; ++i) {
              if (i == 0) {
E
eclipsess 已提交
1003
                vst1q_lane_f32(output_ptr + (h - 1) * w + i, out0, 0);
1004 1005
              }
              if (i == 1) {
E
eclipsess 已提交
1006
                vst1q_lane_f32(output_ptr + (h - 1) * w + i, out0, 1);
1007 1008
              }
              if (i == 2) {
E
eclipsess 已提交
1009
                vst1q_lane_f32(output_ptr + (h - 1) * w + i, out0, 2);
1010 1011 1012 1013 1014
              }
            }
            // mid


E
eclipsess 已提交
1015 1016 1017
            for (int i = 0; i < h - 2; ++i) {
              auto output_ptr = output_data + (i + 1) * w + 1;
              input_tmp = input_data + i * w;
1018
              auto in0_tmp = vld1q_f32(input_tmp);
E
eclipsess 已提交
1019 1020 1021
              auto in2_tmp = vld1q_f32(input_tmp + w);
              auto in4_tmp = vld1q_f32(input_tmp + w + w);
              c_mid = w_mid;
1022 1023
              for (; c_mid > 3; c_mid -= 4) {
                auto in1_tmp = vld1q_f32(input_tmp + 4);
E
eclipsess 已提交
1024 1025
                auto in3_tmp = vld1q_f32(input_tmp + w + 4);
                auto in5_tmp = vld1q_f32(input_tmp + w + w + 4);
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047

                tmp0 = vextq_f32(in0_tmp, in1_tmp, 1);
                tmp1 = vextq_f32(in0_tmp, in1_tmp, 2);
                tmp2 = vextq_f32(in2_tmp, in3_tmp, 1);
                tmp3 = vextq_f32(in2_tmp, in3_tmp, 2);
                tmp4 = vextq_f32(in4_tmp, in5_tmp, 1);
                tmp5 = vextq_f32(in4_tmp, in5_tmp, 2);

                out0 = vmulq_n_f32(in0_tmp, w00);
                out0 = vmlaq_n_f32(out0, tmp0, w01);
                out0 = vmlaq_n_f32(out0, tmp1, w02);
                out0 = vmlaq_n_f32(out0, in2_tmp, w10);
                out0 = vmlaq_n_f32(out0, tmp2, w11);
                out0 = vmlaq_n_f32(out0, tmp3, w12);
                out0 = vmlaq_n_f32(out0, in4_tmp, w20);
                out0 = vmlaq_n_f32(out0, tmp4, w21);
                out0 = vmlaq_n_f32(out0, tmp5, w22);
                out0 = vmlaq_f32(vnewbias, vnewscale, out0);
                if (if_relu) {
                  out0 = vmaxq_f32(out0, vzero);
                }
                vst1q_f32(output_ptr, out0);
1048

1049 1050 1051 1052 1053 1054
                output_ptr += 4;
                input_tmp += 4;
                in0_tmp = in1_tmp;
                in2_tmp = in3_tmp;
                in4_tmp = in5_tmp;
              }
1055

E
eclipsess 已提交
1056 1057 1058
              float32x4_t pad0 = vdupq_n_f32(input_data[i * w + w - 1]);
              float32x4_t pad1 = vdupq_n_f32(input_data[i * w + w - 1 + w]);
              float32x4_t pad2 = vdupq_n_f32(input_data[i * w + w - 1 + w + w]);
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090

              tmp0 = vextq_f32(in0_tmp, pad0, 1);
              tmp1 = vextq_f32(in0_tmp, pad0, 2);
              tmp2 = vextq_f32(in2_tmp, pad1, 1);
              tmp3 = vextq_f32(in2_tmp, pad1, 2);
              tmp4 = vextq_f32(in4_tmp, pad2, 1);
              tmp5 = vextq_f32(in4_tmp, pad2, 2);

              out0 = vmulq_n_f32(in0_tmp, w00);
              out0 = vmlaq_n_f32(out0, tmp0, w01);
              out0 = vmlaq_n_f32(out0, tmp1, w02);
              out0 = vmlaq_n_f32(out0, in2_tmp, w10);
              out0 = vmlaq_n_f32(out0, tmp2, w11);
              out0 = vmlaq_n_f32(out0, tmp3, w12);
              out0 = vmlaq_n_f32(out0, in4_tmp, w20);
              out0 = vmlaq_n_f32(out0, tmp4, w21);
              out0 = vmlaq_n_f32(out0, tmp5, w22);
              out0 = vmlaq_f32(vnewbias, vnewscale, out0);
              if (if_relu) {
                out0 = vmaxq_f32(out0, vzero);
              }
              for (int i = 0; i < c_mid; ++i) {
                if (i == 0) {
                  vst1q_lane_f32(output_ptr + i, out0, 0);
                }
                if (i == 1) {
                  vst1q_lane_f32(output_ptr + i, out0, 1);
                }
                if (i == 2) {
                  vst1q_lane_f32(output_ptr + i, out0, 2);
                }
              }
1091
            }
1092 1093 1094
            output_data += hxw;
            input_data += hxw;
            filter_data_tmp += 9;
E
eclipsess 已提交
1095 1096
          }
        }
1097 1098
    */

L
liuruilong 已提交
1099
#endif
E
eclipsess 已提交
1100
}
1101

E
eclipsess 已提交
1102
/// w!=h not fix
H
hjchen2 已提交
1103 1104 1105 1106 1107 1108
void DepthwiseConvAddBNRelu3x3s2p1(const framework::Tensor *input,
                                   const framework::Tensor *filter,
                                   framework::Tensor *output,
                                   const framework::Tensor *new_scale,
                                   const framework::Tensor *new_bias,
                                   bool if_relu) {
1109
#if __ARM_NEON
L
liuruilong 已提交
1110

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
  const int batch_size = input->dims()[0];

  const int input_height = input->dims()[2];

  const int input_width = input->dims()[3];

  const int output_channels = output->dims()[1];

  const int output_height = output->dims()[2];
  const int output_width = output->dims()[3];
  const int _kernel_size = 3;
  const int stride_height = 2;
  const int stride_width = 2;
  const int padding_height = 1;
  const int padding_width = 1;
  const float zero = 0;
  const int input_channel_stride = input_height * input_width;
  const int output_channel_stride = output_height * output_width;
  const int filter_channel_stride = 9;
  const float *newscale_data = new_scale->data<float>();
  const float *newbias_data = new_bias->data<float>();

  const float *input_data = input->data<float>();
  const float *filter_data = filter->data<float>();

  float *output_data = output->mutable_data<float>();

  const int input_batch_stride = output_channels * input_channel_stride;
  const int output_batch_stride = output_channels * output_channel_stride;
  const int filter_batch_stride = output_channels * output_channel_stride;
  const float *pos1, *pos2, *pos3, *filter1, *filter2, *filter3, *output_ptr;
  int hstart, wstart, hend, wend;
  float result;
  for (int i = 0; i < batch_size; ++i) {
    for (int c = 0; c < output_channels; ++c) {
      filter1 = filter_data;
      filter2 = filter1 + 3;
      filter3 = filter2 + 3;

      for (int ph = 0; ph < output_height; ph++) {
        for (int pw = 0; pw < output_width; pw++) {
          hstart = ph * stride_height - padding_height;
          wstart = pw * stride_width - padding_width;
H
hjchen2 已提交
1154 1155 1156 1157 1158 1159
          hend = std::min(hstart + _kernel_size, input_height + padding_height);
          wend = std::min(wstart + _kernel_size, input_width + padding_width);
          hstart = std::max(hstart, 0);
          wstart = std::max(wstart, 0);
          hend = std::min(hend, input_height);
          wend = std::min(wend, input_width);
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 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 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 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
          pos1 = input_data + hstart * input_width + wstart;
          pos2 = input_data + (hstart + 1) * input_width + wstart;
          pos3 = input_data + (hstart + 2) * input_width + wstart;
          output_ptr = output_data + ph * output_width + pw;

          if (hend - hstart != 3 || wend - wstart != 3) {
            result = 0;
            float fake_input[9] = {0};
            if (hstart == 0 && wstart == 0) {
              // 左上角
              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (j >= 3 - hend && k >= 3 - wend) {
                    fake_input[3 * j + k] =
                        input_data[(j - (3 - hend)) * input_width + k -
                                   (3 - wend)];
                  }
                }
              }
            } else if (hstart == 0 && wend == input_width) {
              // 右上角
              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (j >= 3 - hend && k <= input_width - wstart - 1) {
                    fake_input[3 * j + k] =
                        input_data[(j - (3 - hend)) * input_width + k + wstart];
                  }
                }
              }

            } else if (hend == input_height && wstart == 0) {
              // 左下角

              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (j <= input_height - 1 - hstart && k >= 3 - wend) {
                    fake_input[3 * j + k] =
                        input_data[(j + hstart) * input_width + k - (3 - wend)];
                  }
                }
              }
            } else if (hend == input_height && wend == input_width) {
              // 右下角
              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (j <= input_height - hstart - 1 &&
                      k <= input_width - wstart - 1) {
                    fake_input[3 * j + k] =
                        input_data[(j + hstart) * input_width + k + wstart];
                  }
                }
              }
            } else if (hstart == 0) {
              // 顶部
              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (j >= 3 - hend) {
                    fake_input[3 * j + k] =
                        input_data[(j - (3 - hend)) * input_width + k + wstart];
                  }
                }
              }

            } else if (hend == input_height) {
              // 底部
              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (j <= input_height - hstart - 1) {
                    fake_input[3 * j + k] =
                        input_data[(j + hstart) * input_width + k + wstart];
                  }
                }
              }

            } else if (wstart == 0) {
              // 左侧
              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (k >= 3 - wend) {
                    fake_input[3 * j + k] =
                        input_data[(j + hstart) * input_width +
                                   (k - (3 - wend))];
                  }
                }
              }

            } else if (wend == input_width) {
              // 右侧
              for (int j = 0; j < 3; ++j) {
                for (int k = 0; k < 3; ++k) {
                  if (k <= input_width - wstart - 1) {
                    fake_input[3 * j + k] =
                        input_data[(j + hstart) * input_width + k + wstart];
                  }
                }
              }
            }
            for (int l = 0; l < 9; ++l) {
              result += fake_input[l] * filter1[l];
            }
            output_data[ph * output_width + pw] =
                newscale_data[c] * result + newbias_data[c];

            if (if_relu) {
              output_data[ph * output_width + pw] =
                  output_data[ph * output_width + pw] < 0
                      ? 0
                      : output_data[ph * output_width + pw];
            }
          } else {
            const float32x4_t data1 = vld1q_f32(pos1);
            const float32x4_t data2 = vld1q_f32(pos2);
            const float32x4_t data3 = vld1q_f32(pos3);

            const float32x4_t v_filter1 = vld1q_f32(filter1);
            const float32x4_t v_filter2 = vld1q_f32(filter2);
            const float32x4_t v_filter3 = vld1q_f32(filter3);
            float32x4_t mula = vmulq_f32(data1, v_filter1);
            mula = vmlaq_f32(mula, data2, v_filter2);
            mula = vmlaq_f32(mula, data3, v_filter3);
            float32x2_t res = vpadd_f32(
                vget_high_f32(vsetq_lane_f32(0, mula, 3)), vget_low_f32(mula));
            res = vpadd_f32(res, res);
            output_data[ph * output_width + pw] =
                vget_lane_f32(res, 0) * newscale_data[c] + newbias_data[c];

            if (if_relu) {
              output_data[ph * output_width + pw] =
                  output_data[ph * output_width + pw] < 0
                      ? 0
                      : output_data[ph * output_width + pw];
            }
          }
        }
      }
      input_data += input_channel_stride;
      output_data += output_channel_stride;
      filter_data += filter_channel_stride;
    }
    input_data += input_batch_stride;
    output_data += output_batch_stride;
  }
L
liuruilong 已提交
1302
#endif
1303
}
E
eclipsess 已提交
1304

H
hjchen2 已提交
1305 1306
void DepthwiseConv3x3s2p1v2(const framework::Tensor *input,
                            const framework::Tensor *filter,
1307
                            framework::Tensor *output, framework::Tensor *bias,
1308
                            bool if_bias, bool if_relu) {
1309
#if __ARM_NEON
E
eclipsess 已提交
1310 1311
  const float *input_data = input->data<float>();
  const float *filter_data = filter->data<float>();
1312
  float *output_data = output->mutable_data<float>();
1313 1314 1315 1316
  const float *bias_data;
  if (if_bias) {
    bias_data = bias->data<float>();
  }
E
eclipsess 已提交
1317 1318 1319 1320 1321 1322 1323 1324 1325

  const int in_h = static_cast<int>(input->dims()[2]);
  const int in_w = static_cast<int>(input->dims()[3]);
  const int out_h = static_cast<int>(output->dims()[2]);
  const int out_w = static_cast<int>(output->dims()[3]);
  const int out_l = out_h;
  const int in_l = in_h;
  const int inhxw = in_h * in_w;
  const int outhxw = out_h * out_w;
E
eclipsess 已提交
1326
  /// todo : fix if_pad when w != h
E
eclipsess 已提交
1327 1328
  const int if_pad_r = in_w - 1 == (out_w - 1) * 2 ? 1 : 0;
  const int if_pad_b = in_h - 1 == (out_h - 1) * 2 ? 1 : 0;
E
eclipsess 已提交
1329 1330 1331 1332 1333 1334 1335 1336 1337
  const int batch_size = static_cast<int>(input->dims()[0]);
  const int c = static_cast<int>(input->dims()[1]);
  const float *input_row_ptr;
  float *output_row_ptr;

  const int w_times = (out_w - 2) / 3;

  float32x4_t vbias = vdupq_n_f32(0.0);

E
eclipsess 已提交
1338
  float32x4x2_t input_buff_mid{}, input_buff_bottom[w_times + 1];
E
eclipsess 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
  float32x4_t elewise_res0, elewise_res1, elewise_res2, res3;
  int out2in_mid;
  float32x4_t zero = vdupq_n_f32(0.0);
  for (int b = batch_size; b > 0; --b) {
    const float *filter_data_tmp = filter_data;
    for (int j = 0; j < c; ++j) {
      auto output_data_tmp = output_data + j * out_h * out_w;
      auto input_data_tmp = input_data + j * in_h * in_w;
      auto input_const = input_data_tmp;

      if (if_bias) {
        vbias = vdupq_n_f32(bias_data[j]);
      }

      float w00 = filter_data_tmp[0];
      float w01 = filter_data_tmp[1];
      float w02 = filter_data_tmp[2];
      float w10 = filter_data_tmp[3];
      float w11 = filter_data_tmp[4];
      float w12 = filter_data_tmp[5];
      float w20 = filter_data_tmp[6];
      float w21 = filter_data_tmp[7];
      float w22 = filter_data_tmp[8];

      int h_mid = 0;

      for (; h_mid < out_h - 1; h_mid++) {
        input_row_ptr = input_data_tmp + 1 + h_mid * 2 * in_w;
        output_row_ptr = output_data_tmp + 1 + h_mid * out_w;

        for (int w4 = 0; w4 < w_times + 1; w4++) {
          if (h_mid == 0) {
            elewise_res1 = zero;
            elewise_res0 = zero;
            elewise_res2 = zero;
          } else {
            elewise_res1 = vmulq_n_f32(input_buff_bottom[w4].val[1], w01);
            elewise_res0 = vmulq_n_f32(input_buff_bottom[w4].val[0], w00);
            elewise_res2 = vmulq_n_f32(input_buff_bottom[w4].val[0], w02);
          }
          input_buff_mid = vld2q_f32(input_row_ptr);
          input_buff_bottom[w4] = vld2q_f32(input_row_ptr + in_w);

          elewise_res1 = vmlaq_n_f32(elewise_res1, input_buff_mid.val[1], w11);
          elewise_res0 = vmlaq_n_f32(elewise_res0, input_buff_mid.val[0], w10);
          elewise_res2 = vmlaq_n_f32(elewise_res2, input_buff_mid.val[0], w12);

          elewise_res1 =
              vmlaq_n_f32(elewise_res1, input_buff_bottom[w4].val[1], w21);
          elewise_res0 =
              vmlaq_n_f32(elewise_res0, input_buff_bottom[w4].val[0], w20);
          elewise_res2 =
              vmlaq_n_f32(elewise_res2, input_buff_bottom[w4].val[0], w22);

          res3 = vaddq_f32(vextq_f32(elewise_res2, zero, 1),
                           vaddq_f32(elewise_res0, elewise_res1));
          res3 = vaddq_f32(res3, vbias);
1396 1397 1398
          if (if_relu) {
            res3 = vmaxq_f32(res3, zero);
          }
E
eclipsess 已提交
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
          vst1q_f32(output_row_ptr, res3);

          input_row_ptr += 6;
          output_row_ptr += 3;
        }
      }
      clock();

      input_row_ptr = input_data_tmp + 1 + h_mid * 2 * in_w;
      output_row_ptr = output_data_tmp + 1 + h_mid * out_w;

      for (int w4 = 0; w4 < w_times + 1; w4++) {
        elewise_res1 = vmulq_n_f32(input_buff_bottom[w4].val[1], w01);
        elewise_res0 = vmulq_n_f32(input_buff_bottom[w4].val[0], w00);
        elewise_res2 = vmulq_n_f32(input_buff_bottom[w4].val[0], w02);

        input_buff_mid = vld2q_f32(input_row_ptr);
        input_buff_bottom[w4] = vld2q_f32(input_row_ptr + in_w);

        elewise_res1 = vmlaq_n_f32(elewise_res1, input_buff_mid.val[1], w11);
        elewise_res0 = vmlaq_n_f32(elewise_res0, input_buff_mid.val[0], w10);
        elewise_res2 = vmlaq_n_f32(elewise_res2, input_buff_mid.val[0], w12);

E
eclipsess 已提交
1422
        if (!if_pad_b) {
E
eclipsess 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
          elewise_res1 =
              vmlaq_n_f32(elewise_res1, input_buff_bottom[w4].val[1], w21);
          elewise_res0 =
              vmlaq_n_f32(elewise_res0, input_buff_bottom[w4].val[0], w20);
          elewise_res2 =
              vmlaq_n_f32(elewise_res2, input_buff_bottom[w4].val[0], w22);
        }
        res3 = vaddq_f32(vextq_f32(elewise_res2, zero, 1),
                         vaddq_f32(elewise_res0, elewise_res1));
        res3 = vaddq_f32(res3, vbias);
1433 1434 1435
        if (if_relu) {
          res3 = vmaxq_f32(res3, zero);
        }
E
eclipsess 已提交
1436 1437 1438 1439

        if ((w4 != w_times)) {
          vst1q_f32(output_row_ptr, res3);
        } else {
E
eclipsess 已提交
1440
          if (out_w - 2 - w_times * 3 == 1) {
E
eclipsess 已提交
1441
            vst1q_lane_f32(output_row_ptr, res3, 0);
E
eclipsess 已提交
1442
          } else if (out_w - 2 - w_times * 3 == 2) {
E
eclipsess 已提交
1443 1444 1445 1446 1447 1448 1449 1450
            vst1q_lane_f32(output_row_ptr, res3, 0);
            vst1q_lane_f32(output_row_ptr + 1, res3, 1);
          }
        }
        input_row_ptr += 6;
        output_row_ptr += 3;
      }

1451 1452 1453 1454 1455 1456 1457 1458 1459
      // leftTop, rightTop, leftBottom, rightBottom
      int lt = 0;
      int rt = out_w - 1;
      int lb = out_w * (out_h - 1);
      int rb = out_h * out_w - 1;

      output_data_tmp[lt] = input_const[0] * w11 + input_const[1] * w12 +
                            input_const[in_w] * w21 +
                            input_const[in_w + 1] * w22;
E
eclipsess 已提交
1460

E
eclipsess 已提交
1461
      out2in_mid = (out_w - 1) * 2;
1462
      output_data_tmp[rt] =
E
eclipsess 已提交
1463 1464 1465
          w10 * input_const[out2in_mid - 1] + w11 * input_const[out2in_mid] +
          w20 * input_const[out2in_mid + in_w - 1] +
          w21 * input_const[out2in_mid + in_w] +
E
eclipsess 已提交
1466 1467
          (1 - if_pad_r) * (w12 * input_const[out2in_mid + 1] +
                            w22 * input_const[out2in_mid + in_w + 1]);
E
eclipsess 已提交
1468

E
eclipsess 已提交
1469
      out2in_mid = (out_h - 1) * 2 * in_w;
E
eclipsess 已提交
1470

1471
      output_data_tmp[lb] =
E
eclipsess 已提交
1472 1473 1474
          w01 * input_const[out2in_mid - in_w] +
          w02 * input_const[out2in_mid - in_w + 1] +
          w11 * input_const[out2in_mid] + w12 * input_const[out2in_mid + 1] +
E
eclipsess 已提交
1475 1476
          (1 - if_pad_b) * (w21 * input_const[out2in_mid + in_w] +
                            w22 * input_const[out2in_mid + in_w + 1]);
E
eclipsess 已提交
1477
      out2in_mid = (out_h - 1) * 2 * in_w + (out_w - 1) * 2;
E
eclipsess 已提交
1478

1479
      output_data_tmp[rb] =
E
eclipsess 已提交
1480 1481 1482
          w00 * input_const[out2in_mid - in_w - 1] +
          w01 * input_const[out2in_mid - in_w] +
          w10 * input_const[out2in_mid - 1] + w11 * input_const[out2in_mid] +
E
eclipsess 已提交
1483 1484 1485 1486 1487 1488
          (1 - if_pad_r) * (w20 * input_const[out2in_mid + in_w - 1] +
                            w21 * input_const[out2in_mid + in_w]) +
          (1 - if_pad_b) * (w02 * input_const[out2in_mid - in_w + 1] +
                            w12 * input_const[out2in_mid + 1]) +
          (1 - if_pad_r) * (1 - if_pad_b) * w22 *
              input_const[out2in_mid + in_w + 1];
E
eclipsess 已提交
1489
      if (if_bias) {
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
        output_data_tmp[lt] += bias_data[j];
        output_data_tmp[rt] += bias_data[j];
        output_data_tmp[lb] += bias_data[j];
        output_data_tmp[rb] += bias_data[j];
      }
      if (if_relu) {
        output_data_tmp[lt] = output_data_tmp[lt] < 0 ? 0 : output_data_tmp[lt];
        output_data_tmp[rt] = output_data_tmp[rt] < 0 ? 0 : output_data_tmp[rt];
        output_data_tmp[lb] = output_data_tmp[lb] < 0 ? 0 : output_data_tmp[lb];
        output_data_tmp[rb] = output_data_tmp[rb] < 0 ? 0 : output_data_tmp[rb];
E
eclipsess 已提交
1500 1501 1502
      }
      for (int i = 1; i < out_h - 1; i++) {
        out2in_mid = i * 2 * in_w;
1503 1504 1505 1506 1507 1508 1509
        int left = i * out_w;
        output_data_tmp[left] = w01 * input_const[out2in_mid - in_w] +
                                w02 * input_const[out2in_mid - in_w + 1] +
                                w11 * input_const[out2in_mid] +
                                w12 * input_const[out2in_mid + 1] +
                                w21 * input_const[out2in_mid + in_w] +
                                w22 * input_const[out2in_mid + in_w + 1];
E
eclipsess 已提交
1510

E
eclipsess 已提交
1511
        out2in_mid = i * 2 * in_w + (out_w - 1) * 2;
1512 1513
        int right = i * out_w + out_w - 1;
        output_data_tmp[right] =
E
eclipsess 已提交
1514 1515 1516 1517 1518
            w00 * input_const[out2in_mid - in_w - 1] +
            w01 * input_const[out2in_mid - in_w] +
            w10 * input_const[out2in_mid - 1] + w11 * input_const[out2in_mid] +
            w20 * input_const[out2in_mid + in_w - 1] +
            w21 * input_const[out2in_mid + in_w] +
E
eclipsess 已提交
1519 1520 1521
            (1 - if_pad_r) * (w02 * input_const[out2in_mid - in_w + 1] +
                              w12 * input_const[out2in_mid + 1] +
                              w22 * input_const[out2in_mid + in_w + 1]);
E
eclipsess 已提交
1522
        if (if_bias) {
1523 1524 1525 1526 1527 1528 1529 1530
          output_data_tmp[left] += bias_data[j];
          output_data_tmp[right] += bias_data[j];
        }
        if (if_relu) {
          output_data_tmp[left] =
              output_data_tmp[left] < 0 ? 0 : output_data_tmp[left];
          output_data_tmp[right] =
              output_data_tmp[right] < 0 ? 0 : output_data_tmp[right];
E
eclipsess 已提交
1531 1532 1533 1534 1535 1536 1537
        }
      }
      filter_data_tmp += 9;
    }
    input_data += inhxw * c;
    output_data += outhxw * c;
  }
L
liuruilong 已提交
1538
#endif
E
eclipsess 已提交
1539 1540
}

H
hjchen2 已提交
1541 1542 1543 1544 1545 1546
void DepthwiseConvAddBNRelu3x3s2p1v2(const framework::Tensor *input,
                                     const framework::Tensor *filter,
                                     framework::Tensor *output,
                                     const framework::Tensor *new_scale,
                                     const framework::Tensor *new_bias,
                                     bool if_relu) {
1547
#if __ARM_NEON
1548
  // #ifdef _OPENMP
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
  //  const float *newscale_data = new_scale->data<float>();
  //  const float *newbias_data = new_bias->data<float>();
  //
  //  const int batch_size = static_cast<int>(input->dims()[0]);
  //  const int input_channel = static_cast<int>(input->dims()[1]);
  //
  //  const int input_height = static_cast<int>(input->dims()[2]);
  //  const int input_width = static_cast<int>(input->dims()[3]);
  //  const int output_height = static_cast<int>(output->dims()[2]);
  //  const int output_width = static_cast<int>(output->dims()[3]);
  //  const int inhxw = input_height * input_width;
  //  const int outhxw = output_height * output_width;
  //
  //  float32x4_t zero = vdupq_n_f32(0.0);
  //  for (int b = 0; b < batch_size; b++) {
  //    #pragma omp parallel for
  //    for (int c = 0; c < input_channel; c++) {
  //      const float *filter_data = filter->data<float>() + c * 9;
  //      const float *input_data = input->data<float>() + c * inhxw;
  //      float *output_data = output->data<float>() + c * outhxw;
  //      float32x4_t vnewbias = vdupq_n_f32(newbias_data[c]);
  //      float32x4_t vnewscale = vdupq_n_f32(newscale_data[c]);
  //
  //      float w00 = filter_data[0];
  //      float w01 = filter_data[1];
  //      float w02 = filter_data[2];
  //      float w10 = filter_data[3];
  //      float w11 = filter_data[4];
  //      float w12 = filter_data[5];
  //      float w20 = filter_data[6];
  //      float w21 = filter_data[7];
  //      float w22 = filter_data[8];
  //
  //      int m;
  //      for (m = 1; m < output_width - 2; m = m + 3) {
  //        float *output_ptr = output_data + m;
  //        float32x4x2_t input_buff_mid{}, input_buff_bottom{};
  //        float32x4_t in0, in1, in2, in3, tmp0, tmp1, tmp2, tmp3, out0;
  //        input_buff_mid = vld2q_f32(input_data + (2 * m - 1));
  //        input_buff_bottom = vld2q_f32(input_data + input_width + (2 * m -
  //        1));
  //
  //        in0 = input_buff_mid.val[0];
  //        tmp0 = input_buff_mid.val[1];
  //        tmp1 = vextq_f32(in0, zero, 1);
  //
  //        in2 = input_buff_bottom.val[0];
  //        tmp2 = input_buff_bottom.val[1];
  //        tmp3 = vextq_f32(in2, zero, 1);
  //
  //        out0 = vmulq_n_f32(in0, w10);
  //        out0 = vmlaq_n_f32(out0, tmp0, w11);
  //        out0 = vmlaq_n_f32(out0, tmp1, w12);
  //        out0 = vmlaq_n_f32(out0, in2, w20);
  //        out0 = vmlaq_n_f32(out0, tmp2, w21);
  //        out0 = vmlaq_n_f32(out0, tmp3, w22);
  //        out0 = vmlaq_f32(vnewbias, vnewscale, out0);
  //        if (if_relu) {
  //          out0 = vmaxq_f32(out0, zero);
  //        }
  //        vst1q_lane_f32(output_ptr, out0, 0);
  //        vst1q_lane_f32(output_ptr + 1, out0, 1);
  //        vst1q_lane_f32(output_ptr + 2, out0, 2);
  //      }
  //      for (m = 1; m < output_width - 2; m += 3) {
  //      }
  //      for (int j = m; j < output_width; j++) {
  //        output_data[j] = input_data[2 * j - 1] * w10 + input_data[2 * j] *
  //        w11 +
  //                         input_data[2 * j + 1] * w12 +
  //                         input_data[2 * j - 1 + input_width] * w20 +
  //                         input_data[2 * j + input_width] * w21 +
  //                         input_data[2 * j + 1 + input_width] * w22;
  //        output_data[j] = newscale_data[c] * output_data[j] +
  //        newbias_data[c]; if (if_relu) {
  //          output_data[j] = output_data[j] < 0 ? 0 : output_data[j];
  //        }
  //      }
  //
  //      for (int i = 1; i < output_height; i += 1) {
  //        for (int m = 1; m < output_width - 2; m += 3) {
  //          float *output_ptr = output_data + i * output_width + m;
  //          float32x4x2_t input_buff_top{}, input_buff_mid{},
  //          input_buff_bottom{}; float32x4_t in0, in1, in2, in3, in4, in5,
  //          tmp0, tmp1, tmp2, tmp3,
  //              tmp4, tmp5, out0;
  //          input_buff_top =
  //              vld2q_f32(input_data + (2 * i - 1) * input_width + (2 * m -
  //              1));
  //          input_buff_mid =
  //              vld2q_f32(input_data + (2 * i) * input_width + (2 * m - 1));
  //          input_buff_bottom =
  //              vld2q_f32(input_data + (2 * i + 1) * input_width + (2 * m -
  //              1));
  //
  //          in0 = input_buff_top.val[0];
  //          tmp0 = input_buff_top.val[1];
  //          tmp1 = vextq_f32(in0, zero, 1);
  //
  //          in2 = input_buff_mid.val[0];
  //          tmp2 = input_buff_mid.val[1];
  //          tmp3 = vextq_f32(in2, zero, 1);
  //
  //          in4 = input_buff_bottom.val[0];
  //          tmp4 = input_buff_bottom.val[1];
  //          tmp5 = vextq_f32(in4, zero, 1);
  //
  //          out0 = vmulq_n_f32(in0, w00);
  //          out0 = vmlaq_n_f32(out0, tmp0, w01);
  //          out0 = vmlaq_n_f32(out0, tmp1, w02);
  //          out0 = vmlaq_n_f32(out0, in2, w10);
  //          out0 = vmlaq_n_f32(out0, tmp2, w11);
  //          out0 = vmlaq_n_f32(out0, tmp3, w12);
  //          out0 = vmlaq_n_f32(out0, in4, w20);
  //          out0 = vmlaq_n_f32(out0, tmp4, w21);
  //          out0 = vmlaq_n_f32(out0, tmp5, w22);
  //          out0 = vmlaq_f32(vnewbias, vnewscale, out0);
  //          if (if_relu) {
  //            out0 = vmaxq_f32(out0, zero);
  //          }
  //          vst1q_lane_f32(output_ptr, out0, 0);
  //          vst1q_lane_f32(output_ptr + 1, out0, 1);
  //          vst1q_lane_f32(output_ptr + 2, out0, 2);
  //        }
  //        int m;
  //        for (m = 1; m < output_width - 2; m += 3) {
  //        }
  //        for (int j = m; j < output_width; j++) {
  //          output_data[i * output_width + j] =
  //              input_data[(2 * i - 1) * input_width + 2 * j - 1] * w00 +
  //              input_data[(2 * i - 1) * input_width + 2 * j] * w01 +
  //              input_data[(2 * i - 1) * input_width + 2 * j + 1] * w02 +
  //              input_data[(2 * i) * input_width + 2 * j - 1] * w10 +
  //              input_data[(2 * i) * input_width + 2 * j] * w11 +
  //              input_data[(2 * i) * input_width + 2 * j + 1] * w12 +
  //              input_data[(2 * i + 1) * input_width + 2 * j - 1] * w20 +
  //              input_data[(2 * i + 1) * input_width + 2 * j] * w21 +
  //              input_data[(2 * i + 1) * input_width + 2 * j + 1] * w22;
  //          output_data[i * output_width + j] =
  //              newscale_data[c] * output_data[i * output_width + j] +
  //              newbias_data[c];
  //          if (if_relu) {
  //            output_data[i * output_width + j] =
  //                output_data[i * output_width + j] < 0
  //                    ? 0
  //                    : output_data[i * output_width + j];
  //          }
  //        }
  //      }
  //      output_data[0] = input_data[0] * w11 + input_data[1] * w12 +
  //                       input_data[input_height] * w21 +
  //                       input_data[input_height + 1] * w22;
  //
  //      output_data[0] = newscale_data[c] * output_data[0] + newbias_data[c];
  //      if (if_relu) {
  //        output_data[0] = output_data[0] < 0 ? 0 : output_data[0];
  //      }
  //      for (int i = 1; i < output_height; i++) {
  //        output_data[i * output_width] =
  //            input_data[(2 * i - 1) * input_width] * w01 +
  //            input_data[(2 * i - 1) * input_width + 1] * w02 +
  //            input_data[(2 * i) * input_width] * w11 +
  //            input_data[(2 * i) * input_width + 1] * w12 +
  //            input_data[(2 * i + 1) * input_width] * w21 +
  //            input_data[(2 * i + 1) * input_width + 1] * w22;
  //
  //        output_data[i * output_width] =
  //            newscale_data[c] * output_data[i * output_width] +
  //            newbias_data[c];
  //        if (if_relu) {
  //          output_data[i * output_width] = output_data[i * output_width] < 0
  //                                              ? 0
  //                                              : output_data[i *
  //                                              output_width];
  //        }
  //      }
  //    }
  //  }
  //
1728
  // #else
1729 1730 1731

  const float *input_data = input->data<float>();
  const float *filter_data = filter->data<float>();
1732
  float *output_data = output->mutable_data<float>();
1733 1734 1735 1736 1737 1738 1739
  const float *newscale_data = new_scale->data<float>();
  const float *newbias_data = new_bias->data<float>();

  const int in_h = static_cast<int>(input->dims()[2]);
  const int in_w = static_cast<int>(input->dims()[3]);
  const int out_h = static_cast<int>(output->dims()[2]);
  const int out_w = static_cast<int>(output->dims()[3]);
E
eclipsess 已提交
1740 1741
  //  const int out_l = out_h;
  //  const int in_l = in_h;
1742 1743
  const int inhxw = in_h * in_w;
  const int outhxw = out_h * out_w;
E
eclipsess 已提交
1744
  /// todo : fix if_pad when w != h
E
eclipsess 已提交
1745 1746
  const int if_pad_r = in_w - 1 == (out_w - 1) * 2 ? 1 : 0;
  const int if_pad_b = in_h - 1 == (out_h - 1) * 2 ? 1 : 0;
1747 1748 1749 1750 1751
  const int batch_size = static_cast<int>(input->dims()[0]);
  const int c = static_cast<int>(input->dims()[1]);
  const int w_times = (out_w - 2) / 3;
  float32x4_t zero = vdupq_n_f32(0.0);
  for (int b = batch_size; b > 0; --b) {
1752
#pragma omp parallel for
1753 1754 1755 1756 1757 1758 1759 1760
    for (int j = 0; j < c; j++) {
      const float *input_row_ptr;
      float *output_row_ptr;
      float32x4x2_t input_buff_mid{}, input_buff_bottom[w_times + 1];
      float32x4_t elewise_res0, elewise_res1, elewise_res2, res3;
      int out2in_mid;
      float32x4_t vnewbias = vdupq_n_f32(0.0);
      float32x4_t vnewscale = vdupq_n_f32(1.0);
1761 1762 1763
      auto output_data_tmp = output_data + j * out_h * out_w;
      auto input_data_tmp = input_data + j * in_h * in_w;
      auto input_const = input_data_tmp;
1764
      const float *filter_data_tmp = filter_data + 9 * j;
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
      vnewbias = vdupq_n_f32(newbias_data[j]);
      vnewscale = vdupq_n_f32(newscale_data[j]);

      float w00 = filter_data_tmp[0];
      float w01 = filter_data_tmp[1];
      float w02 = filter_data_tmp[2];
      float w10 = filter_data_tmp[3];
      float w11 = filter_data_tmp[4];
      float w12 = filter_data_tmp[5];
      float w20 = filter_data_tmp[6];
      float w21 = filter_data_tmp[7];
      float w22 = filter_data_tmp[8];

      int h_mid = 0;

      for (; h_mid < out_h - 1; h_mid++) {
        input_row_ptr = input_data_tmp + 1 + h_mid * 2 * in_w;
        output_row_ptr = output_data_tmp + 1 + h_mid * out_w;

        for (int w4 = 0; w4 < w_times + 1; w4++) {
          if (h_mid == 0) {
            elewise_res1 = zero;
            elewise_res0 = zero;
            elewise_res2 = zero;
          } else {
            elewise_res1 = vmulq_n_f32(input_buff_bottom[w4].val[1], w01);
            elewise_res0 = vmulq_n_f32(input_buff_bottom[w4].val[0], w00);
            elewise_res2 = vmulq_n_f32(input_buff_bottom[w4].val[0], w02);
          }
          input_buff_mid = vld2q_f32(input_row_ptr);
          input_buff_bottom[w4] = vld2q_f32(input_row_ptr + in_w);

          elewise_res1 = vmlaq_n_f32(elewise_res1, input_buff_mid.val[1], w11);
          elewise_res0 = vmlaq_n_f32(elewise_res0, input_buff_mid.val[0], w10);
          elewise_res2 = vmlaq_n_f32(elewise_res2, input_buff_mid.val[0], w12);

          elewise_res1 =
              vmlaq_n_f32(elewise_res1, input_buff_bottom[w4].val[1], w21);
          elewise_res0 =
              vmlaq_n_f32(elewise_res0, input_buff_bottom[w4].val[0], w20);
          elewise_res2 =
              vmlaq_n_f32(elewise_res2, input_buff_bottom[w4].val[0], w22);

          res3 = vaddq_f32(vextq_f32(elewise_res2, zero, 1),
                           vaddq_f32(elewise_res0, elewise_res1));
          res3 = vmlaq_f32(vnewbias, vnewscale, res3);

          if (if_relu) {
            res3 = vmaxq_f32(res3, zero);
          }
1815 1816 1817
          vst1q_lane_f32(output_row_ptr, res3, 0);
          vst1q_lane_f32(output_row_ptr + 1, res3, 1);
          vst1q_lane_f32(output_row_ptr + 2, res3, 2);
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839

          input_row_ptr += 6;
          output_row_ptr += 3;
        }
      }
      clock();

      input_row_ptr = input_data_tmp + 1 + h_mid * 2 * in_w;
      output_row_ptr = output_data_tmp + 1 + h_mid * out_w;

      for (int w4 = 0; w4 < w_times + 1; w4++) {
        elewise_res1 = vmulq_n_f32(input_buff_bottom[w4].val[1], w01);
        elewise_res0 = vmulq_n_f32(input_buff_bottom[w4].val[0], w00);
        elewise_res2 = vmulq_n_f32(input_buff_bottom[w4].val[0], w02);

        input_buff_mid = vld2q_f32(input_row_ptr);
        input_buff_bottom[w4] = vld2q_f32(input_row_ptr + in_w);

        elewise_res1 = vmlaq_n_f32(elewise_res1, input_buff_mid.val[1], w11);
        elewise_res0 = vmlaq_n_f32(elewise_res0, input_buff_mid.val[0], w10);
        elewise_res2 = vmlaq_n_f32(elewise_res2, input_buff_mid.val[0], w12);

E
eclipsess 已提交
1840
        if (!if_pad_b) {
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
          elewise_res1 =
              vmlaq_n_f32(elewise_res1, input_buff_bottom[w4].val[1], w21);
          elewise_res0 =
              vmlaq_n_f32(elewise_res0, input_buff_bottom[w4].val[0], w20);
          elewise_res2 =
              vmlaq_n_f32(elewise_res2, input_buff_bottom[w4].val[0], w22);
        }
        res3 = vaddq_f32(vextq_f32(elewise_res2, zero, 1),
                         vaddq_f32(elewise_res0, elewise_res1));
        res3 = vmlaq_f32(vnewbias, vnewscale, res3);

        if (if_relu) {
          res3 = vmaxq_f32(res3, zero);
        }
        if ((w4 != w_times)) {
1856 1857 1858
          vst1q_lane_f32(output_row_ptr, res3, 0);
          vst1q_lane_f32(output_row_ptr + 1, res3, 1);
          vst1q_lane_f32(output_row_ptr + 2, res3, 2);
1859
        } else {
E
eclipsess 已提交
1860
          if (out_w - 2 - w_times * 3 == 1) {
1861
            vst1q_lane_f32(output_row_ptr, res3, 0);
E
eclipsess 已提交
1862
          } else if (out_w - 2 - w_times * 3 == 2) {
1863 1864 1865 1866 1867 1868 1869 1870 1871
            vst1q_lane_f32(output_row_ptr, res3, 0);
            vst1q_lane_f32(output_row_ptr + 1, res3, 1);
          }
        }
        input_row_ptr += 6;
        output_row_ptr += 3;
      }

      output_data_tmp[0] = input_const[0] * w11 + input_const[1] * w12 +
E
eclipsess 已提交
1872 1873
                           input_const[in_w] * w21 +
                           input_const[in_w + 1] * w22;
1874

E
eclipsess 已提交
1875
      out2in_mid = (out_w - 1) * 2;
E
eclipsess 已提交
1876
      output_data_tmp[out_w - 1] =
1877 1878 1879
          w10 * input_const[out2in_mid - 1] + w11 * input_const[out2in_mid] +
          w20 * input_const[out2in_mid + in_w - 1] +
          w21 * input_const[out2in_mid + in_w] +
E
eclipsess 已提交
1880 1881
          (1 - if_pad_r) * (w12 * input_const[out2in_mid + 1] +
                            w22 * input_const[out2in_mid + in_w + 1]);
1882

E
eclipsess 已提交
1883
      out2in_mid = (out_h - 1) * 2 * in_w;
1884

E
eclipsess 已提交
1885
      output_data_tmp[out_w * (out_h - 1)] =
1886 1887 1888
          w01 * input_const[out2in_mid - in_w] +
          w02 * input_const[out2in_mid - in_w + 1] +
          w11 * input_const[out2in_mid] + w12 * input_const[out2in_mid + 1] +
E
eclipsess 已提交
1889 1890
          (1 - if_pad_b) * (w21 * input_const[out2in_mid + in_w] +
                            w22 * input_const[out2in_mid + in_w + 1]);
E
eclipsess 已提交
1891
      out2in_mid = (out_h - 1) * 2 * in_w + (out_w - 1) * 2;
1892

E
eclipsess 已提交
1893
      output_data_tmp[out_h * out_w - 1] =
1894 1895 1896
          w00 * input_const[out2in_mid - in_w - 1] +
          w01 * input_const[out2in_mid - in_w] +
          w10 * input_const[out2in_mid - 1] + w11 * input_const[out2in_mid] +
E
eclipsess 已提交
1897 1898 1899 1900 1901 1902
          (1 - if_pad_r) * (w20 * input_const[out2in_mid + in_w - 1] +
                            w21 * input_const[out2in_mid + in_w]) +
          (1 - if_pad_b) * (w02 * input_const[out2in_mid - in_w + 1] +
                            w12 * input_const[out2in_mid + 1]) +
          (1 - if_pad_r) * (1 - if_pad_b) * w22 *
              input_const[out2in_mid + in_w + 1];
1903 1904
      output_data_tmp[0] =
          output_data_tmp[0] * newscale_data[j] + newbias_data[j];
E
eclipsess 已提交
1905 1906 1907 1908
      output_data_tmp[out_w - 1] =
          output_data_tmp[out_w - 1] * newscale_data[j] + newbias_data[j];
      output_data_tmp[out_w * (out_h - 1)] =
          output_data_tmp[out_w * (out_h - 1)] * newscale_data[j] +
1909
          newbias_data[j];
E
eclipsess 已提交
1910 1911
      output_data_tmp[out_h * out_w - 1] =
          output_data_tmp[out_h * out_w - 1] * newscale_data[j] +
1912 1913 1914
          newbias_data[j];
      if (if_relu) {
        output_data_tmp[0] = output_data_tmp[0] < 0 ? 0 : output_data_tmp[0];
E
eclipsess 已提交
1915 1916 1917 1918
        output_data_tmp[out_w - 1] =
            output_data_tmp[out_w - 1] < 0 ? 0 : output_data_tmp[out_w - 1];
        output_data_tmp[out_w * (out_h - 1)] =
            output_data_tmp[out_w * (out_h - 1)] < 0
1919
                ? 0
E
eclipsess 已提交
1920 1921 1922
                : output_data_tmp[out_w * (out_h - 1)];
        output_data_tmp[out_h * out_w - 1] =
            output_data_tmp[out_h * out_w - 1] < 0
1923
                ? 0
E
eclipsess 已提交
1924
                : output_data_tmp[out_h * out_w - 1];
1925 1926 1927
      }
      for (int i = 1; i < out_h - 1; i++) {
        out2in_mid = i * 2 * in_w;
E
eclipsess 已提交
1928
        output_data_tmp[i * out_w] = w01 * input_const[out2in_mid - in_w] +
1929 1930 1931 1932 1933
                                     w02 * input_const[out2in_mid - in_w + 1] +
                                     w11 * input_const[out2in_mid] +
                                     w12 * input_const[out2in_mid + 1] +
                                     w21 * input_const[out2in_mid + in_w] +
                                     w22 * input_const[out2in_mid + in_w + 1];
1934

E
eclipsess 已提交
1935
        out2in_mid = i * 2 * in_w + (out_w - 1) * 2;
E
eclipsess 已提交
1936
        output_data_tmp[i * out_w + out_w - 1] =
1937 1938 1939 1940 1941
            w00 * input_const[out2in_mid - in_w - 1] +
            w01 * input_const[out2in_mid - in_w] +
            w10 * input_const[out2in_mid - 1] + w11 * input_const[out2in_mid] +
            w20 * input_const[out2in_mid + in_w - 1] +
            w21 * input_const[out2in_mid + in_w] +
E
eclipsess 已提交
1942 1943 1944
            (1 - if_pad_r) * (w02 * input_const[out2in_mid - in_w + 1] +
                              w12 * input_const[out2in_mid + 1] +
                              w22 * input_const[out2in_mid + in_w + 1]);
E
eclipsess 已提交
1945 1946 1947 1948
        output_data_tmp[i * out_w] =
            output_data_tmp[i * out_w] * newscale_data[j] + newbias_data[j];
        output_data_tmp[i * out_w + out_w - 1] =
            output_data_tmp[i * out_w + out_w - 1] * newscale_data[j] +
1949 1950
            newbias_data[j];
        if (if_relu) {
E
eclipsess 已提交
1951 1952 1953 1954
          output_data_tmp[i * out_w] =
              output_data_tmp[i * out_w] < 0 ? 0 : output_data_tmp[i * out_w];
          output_data_tmp[i * out_w + out_w - 1] =
              output_data_tmp[i * out_w + out_w - 1] < 0
1955
                  ? 0
E
eclipsess 已提交
1956
                  : output_data_tmp[i * out_w + out_w - 1];
1957 1958 1959 1960 1961 1962
        }
      }
    }
    input_data += inhxw * c;
    output_data += outhxw * c;
  }
1963 1964 1965 1966
// #endif
#endif
}

H
hjchen2 已提交
1967 1968
void DepthwiseConv3x3s2p0(const framework::Tensor *input,
                          const framework::Tensor *filter,
1969
                          framework::Tensor *output, framework::Tensor *bias,
1970
                          bool if_bias, bool if_relu) {
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
#if __ARM_NEON
  const int batch_size = static_cast<int>(input->dims()[0]);
  const int input_channel = static_cast<int>(input->dims()[1]);

  const int input_height = static_cast<int>(input->dims()[2]);
  const int input_width = static_cast<int>(input->dims()[3]);
  const int output_height = static_cast<int>(output->dims()[2]);
  const int output_width = static_cast<int>(output->dims()[3]);
  const int inhxw = input_height * input_width;
  const int outhxw = output_height * output_width;
1981
  output->mutable_data<float>();
1982 1983 1984 1985 1986 1987 1988

  float32x4_t zero = vdupq_n_f32(0.0);
  for (int b = 0; b < batch_size; b++) {
#pragma omp parallel for
    for (int c = 0; c < input_channel; c++) {
      const float *filter_data = filter->data<float>() + c * 9;
      const float *input_data = input->data<float>() + c * inhxw;
1989 1990 1991 1992 1993 1994
      const float *bias_data;
      float32x4_t biasv;
      if (if_bias) {
        bias_data = bias->data<float>() + c;
        biasv = vld1q_dup_f32(bias_data);
      }
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
      float *output_data = output->data<float>() + c * outhxw;
      float w00 = filter_data[0];
      float w01 = filter_data[1];
      float w02 = filter_data[2];
      float w10 = filter_data[3];
      float w11 = filter_data[4];
      float w12 = filter_data[5];
      float w20 = filter_data[6];
      float w21 = filter_data[7];
      float w22 = filter_data[8];
      for (int i = 0; i < output_height; i += 1) {
        for (int m = 0; m < output_width - 2; m += 3) {
          float *output_ptr = output_data + i * output_width + m;
          float32x4x2_t input_buff_top{}, input_buff_mid{}, input_buff_bottom{};
          float32x4_t in0, in1, in2, in3, in4, in5, tmp0, tmp1, tmp2, tmp3,
              tmp4, tmp5, out0;
          input_buff_top =
              vld2q_f32(input_data + (2 * i) * input_width + (2 * m));
          input_buff_mid =
              vld2q_f32(input_data + (2 * i + 1) * input_width + (2 * m));
          input_buff_bottom =
              vld2q_f32(input_data + (2 * i + 2) * input_width + (2 * m));

          in0 = input_buff_top.val[0];
          tmp0 = input_buff_top.val[1];
          tmp1 = vextq_f32(in0, zero, 1);

          in2 = input_buff_mid.val[0];
          tmp2 = input_buff_mid.val[1];
          tmp3 = vextq_f32(in2, zero, 1);

          in4 = input_buff_bottom.val[0];
          tmp4 = input_buff_bottom.val[1];
          tmp5 = vextq_f32(in4, zero, 1);

          out0 = vmulq_n_f32(in0, w00);
          out0 = vmlaq_n_f32(out0, tmp0, w01);
          out0 = vmlaq_n_f32(out0, tmp1, w02);
          out0 = vmlaq_n_f32(out0, in2, w10);
          out0 = vmlaq_n_f32(out0, tmp2, w11);
          out0 = vmlaq_n_f32(out0, tmp3, w12);
          out0 = vmlaq_n_f32(out0, in4, w20);
          out0 = vmlaq_n_f32(out0, tmp4, w21);
          out0 = vmlaq_n_f32(out0, tmp5, w22);
2039 2040 2041
          if (if_bias) {
            out0 = vaddq_f32(out0, biasv);
          }
2042 2043 2044
          if (if_relu) {
            out0 = vmaxq_f32(out0, zero);
          }
2045 2046 2047 2048 2049 2050 2051 2052
          vst1q_lane_f32(output_ptr, out0, 0);
          vst1q_lane_f32(output_ptr + 1, out0, 1);
          vst1q_lane_f32(output_ptr + 2, out0, 2);
        }
        int m;
        for (m = 0; m < output_width - 2; m += 3) {
        }
        for (int j = m; j < output_width; j++) {
2053 2054
          int index = i * output_width + j;
          output_data[index] =
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
              input_data[(2 * i) * input_width + 2 * j] * w00 +
              input_data[(2 * i) * input_width + 2 * j + 1] * w01 +
              input_data[(2 * i) * input_width + 2 * j + 2] * w02 +
              input_data[(2 * i + 1) * input_width + 2 * j] * w10 +
              input_data[(2 * i + 1) * input_width + 2 * j + 1] * w11 +
              input_data[(2 * i + 1) * input_width + 2 * j + 2] * w12 +
              input_data[(2 * i + 2) * input_width + 2 * j] * w20 +
              input_data[(2 * i + 2) * input_width + 2 * j + 1] * w21 +
              input_data[(2 * i + 2) * input_width + 2 * j + 2] * w22;
          if (if_bias) {
2065 2066 2067 2068 2069
            output_data[index] += *bias_data;
          }
          if (if_relu) {
            output_data[index] =
                output_data[index] < 0 ? 0 : output_data[index];
2070
          }
2071 2072 2073 2074 2075
        }
      }
    }
  }

L
liuruilong 已提交
2076
#endif
E
eclipsess 已提交
2077 2078
}

W
wangliu 已提交
2079 2080 2081
}  // namespace math
}  // namespace operators
}  // namespace paddle_mobile