NeonDepthwiseConv.cpp 21.0 KB
Newer Older
H
hedaoyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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. */

#include "neon_util.h"
#include "paddle/function/ConvOp.h"
#include "paddle/function/Im2Col.h"

namespace paddle {

namespace neon {

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

template <int filterSize, int stride>
struct DepthwiseConvKernel {};

inline float32_t conv3x3(float32x4_t r0,
                         float32x4_t r1,
                         float32x4_t r2,
                         float32x4_t k0,
                         float32x4_t k1,
                         float32x4_t k2) {
  float32x4_t tmp;
  tmp = vmulq_f32(r0, k0);
  tmp = vmlaq_f32(tmp, r1, k1);
  tmp = vmlaq_f32(tmp, r2, k2);
  return vaddvq_f32(tmp);
}

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
inline float32_t conv4x4(float32x4_t r0,
                         float32x4_t r1,
                         float32x4_t r2,
                         float32x4_t r3,
                         float32x4_t k0,
                         float32x4_t k1,
                         float32x4_t k2,
                         float32x4_t k3) {
  float32x4_t tmp;
  tmp = vmulq_f32(r0, k0);
  tmp = vmlaq_f32(tmp, r1, k1);
  tmp = vmlaq_f32(tmp, r2, k2);
  tmp = vmlaq_f32(tmp, r3, k3);
  return vaddvq_f32(tmp);
}

H
hedaoyuan 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
/**
 * Each step calculates four elements of the output.
 * First step:
 *   R0[0, 1, 2, 3...] * K[0][0]
 *   R0[1, 2, 3, 4...] * K[0][1]
 *   R0[2, 3, 4, 5...] * K[0][2]
 *   R1[0, 1, 2, 3...] * K[1][0]
 *   R1[1, 2, 3, 4...] * K[1][1]
 *   R1[2, 3, 4, 5...] * K[1][2]
 *   R2[0, 1, 2, 3...] * K[2][0]
 *   R2[1, 2, 3, 4...] * K[2][1]
 * + R2[2, 3, 4, 5...] * K[2][2]
 * ------------------------------
 *     Output[0, 1, 2, 3]
 */
template <>
struct DepthwiseConvKernel<3, 1> {
  static void run(const float* inputData,
                  const float* filterData,
                  int inputHeight,
                  int inputWidth,
                  int outputChannels,
                  int outputHeight,
                  int outputWidth,
                  int filterMultiplier,
                  float* outputData) {
    const int steps = outputWidth >> 2;
    const int remain = outputWidth & 3;
    for (int c = 0; c < outputChannels; c++, filterData += 9) {
      // Load the filters
      float32x4_t k[3];
      k[0] = vld1q_f32(filterData);
      k[1] = vld1q_f32(filterData + 3);
      k[2] = vld1q_f32(filterData + 6);
      k[0] = vsetq_lane_f32(0.f, k[0], 3);
      k[1] = vsetq_lane_f32(0.f, k[1], 3);
      k[2] = vsetq_lane_f32(0.f, k[2], 3);

      const float* r0 =
          inputData + (c / filterMultiplier) * (inputHeight * inputWidth);
      const float* r1 = r0 + inputWidth;
      const float* r2 = r0 + inputWidth * 2;
      float32x4_t input[3][3];
      for (int h = 0; h < outputHeight; h++) {
        for (int s = 0; s < steps; s++) {
          // Load the inputs
          float32x4_t tmp;
          input[0][0] = vld1q_f32(r0);
          tmp = vld1q_f32(r0 + 4);
          input[0][1] = vextq_f32(input[0][0], tmp, 1);
          input[0][2] = vextq_f32(input[0][0], tmp, 2);
          input[1][0] = vld1q_f32(r1);
          tmp = vld1q_f32(r1 + 4);
          input[1][1] = vextq_f32(input[1][0], tmp, 1);
          input[1][2] = vextq_f32(input[1][0], tmp, 2);
          input[2][0] = vld1q_f32(r2);
          tmp = vld1q_f32(r2 + 4);
          input[2][1] = vextq_f32(input[2][0], tmp, 1);
          input[2][2] = vextq_f32(input[2][0], tmp, 2);

          float32x4_t tmp1 = vdupq_n_f32(0.f);
          float32x4_t tmp2 = vdupq_n_f32(0.f);
          tmp1 = vmlaq_laneq_f32(tmp1, input[0][0], k[0], 0);
          tmp2 = vmlaq_laneq_f32(tmp2, input[0][1], k[0], 1);
          tmp1 = vmlaq_laneq_f32(tmp1, input[0][2], k[0], 2);
          tmp2 = vmlaq_laneq_f32(tmp2, input[1][0], k[1], 0);
          tmp1 = vmlaq_laneq_f32(tmp1, input[1][1], k[1], 1);
          tmp2 = vmlaq_laneq_f32(tmp2, input[1][2], k[1], 2);
          tmp1 = vmlaq_laneq_f32(tmp1, input[2][0], k[2], 0);
          tmp2 = vmlaq_laneq_f32(tmp2, input[2][1], k[2], 1);
          tmp1 = vmlaq_laneq_f32(tmp1, input[2][2], k[2], 2);
          tmp1 = vaddq_f32(tmp1, tmp2);

          vst1q_f32(outputData, tmp1);
          r0 += 4;
          r1 += 4;
          r2 += 4;
          outputData += 4;
        }

        for (int r = 0; r < remain; r++) {
          float32x4_t i0 = vld1q_f32(r0);
          float32x4_t i1 = vld1q_f32(r1);
          float32x4_t i2 = vld1q_f32(r2);
          *outputData = conv3x3(i0, i1, i2, k[0], k[1], k[2]);
          r0++;
          r1++;
          r2++;
          outputData++;
        }

        r0 += 2;
        r1 += 2;
        r2 += 2;
      }
    }
  }
};

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
/**
 * Each step calculates four elements of the output.
 * First step:
 *   R0[0, 2, 4, 6...] * K[0][0]
 *   R0[1, 3, 5, 7...] * K[0][1]
 *   R0[2, 4, 6, 8...] * K[0][2]
 *   R1[0, 2, 4, 6...] * K[1][0]
 *   R1[1, 3, 5, 7...] * K[1][1]
 *   R1[2, 4, 6, 8...] * K[1][2]
 *   R2[0, 2, 4, 6...] * K[2][0]
 *   R2[1, 3, 5, 7...] * K[2][1]
 *   R2[2, 4, 6, 8...] * K[2][2]
 * ------------------------------
 *     Output[0, 1, 2, 3]
 */
template <>
struct DepthwiseConvKernel<3, 2> {
  static void run(const float* inputData,
                  const float* filterData,
                  int inputHeight,
                  int inputWidth,
                  int outputChannels,
                  int outputHeight,
                  int outputWidth,
                  int filterMultiplier,
                  float* outputData) {
    const int steps = outputWidth >> 2;
    const int remain = outputWidth & 3;
    for (int c = 0; c < outputChannels; c++, filterData += 9) {
      // Load the filters
      float32x4_t k[3];
      k[0] = vld1q_f32(filterData);
      k[1] = vld1q_f32(filterData + 3);
      k[2] = vld1q_f32(filterData + 6);
      k[0] = vsetq_lane_f32(0.f, k[0], 3);
      k[1] = vsetq_lane_f32(0.f, k[1], 3);
      k[2] = vsetq_lane_f32(0.f, k[2], 3);

      const float* start =
          inputData + (c / filterMultiplier) * (inputHeight * inputWidth);
      float32x4_t input[3][3];
      for (int h = 0; h < outputHeight; h++) {
        const float* r0 = start + 2 * h * inputWidth;
        const float* r1 = start + (2 * h + 1) * inputWidth;
        const float* r2 = start + (2 * h + 2) * inputWidth;
        for (int s = 0; s < steps; s++) {
          // Load the inputs
          float32x4_t data1;
          float32x4x2_t data2;

          data2 = vld2q_f32(r0);
          input[0][0] = data2.val[0];
          input[0][1] = data2.val[1];
          data1 = vld1q_f32(r0 + 8);
          input[0][2] = vextq_f32(data2.val[0], data1, 1);

          data2 = vld2q_f32(r1);
          input[1][0] = data2.val[0];
          input[1][1] = data2.val[1];
          data1 = vld1q_f32(r1 + 8);
          input[1][2] = vextq_f32(data2.val[0], data1, 1);

          data2 = vld2q_f32(r2);
          input[2][0] = data2.val[0];
          input[2][1] = data2.val[1];
          data1 = vld1q_f32(r2 + 8);
          input[2][2] = vextq_f32(data2.val[0], data1, 1);

          float32x4_t tmp1 = vdupq_n_f32(0.f);
          float32x4_t tmp2 = vdupq_n_f32(0.f);
          tmp1 = vmlaq_laneq_f32(tmp1, input[0][0], k[0], 0);
          tmp2 = vmlaq_laneq_f32(tmp2, input[0][1], k[0], 1);
          tmp1 = vmlaq_laneq_f32(tmp1, input[0][2], k[0], 2);
          tmp2 = vmlaq_laneq_f32(tmp2, input[1][0], k[1], 0);
          tmp1 = vmlaq_laneq_f32(tmp1, input[1][1], k[1], 1);
          tmp2 = vmlaq_laneq_f32(tmp2, input[1][2], k[1], 2);
          tmp1 = vmlaq_laneq_f32(tmp1, input[2][0], k[2], 0);
          tmp2 = vmlaq_laneq_f32(tmp2, input[2][1], k[2], 1);
          tmp1 = vmlaq_laneq_f32(tmp1, input[2][2], k[2], 2);
          tmp1 = vaddq_f32(tmp1, tmp2);

          vst1q_f32(outputData, tmp1);
          r0 += 8;
          r1 += 8;
          r2 += 8;
          outputData += 4;
        }

        for (int r = 0; r < remain; r++) {
          float32x4_t i0 = vld1q_f32(r0);
          float32x4_t i1 = vld1q_f32(r1);
          float32x4_t i2 = vld1q_f32(r2);
          *outputData = conv3x3(i0, i1, i2, k[0], k[1], k[2]);
          r0 += 2;
          r1 += 2;
          r2 += 2;
          outputData++;
        }
      }
    }
  }
};

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
/**
 * Each step calculates four elements of the output.
 */
template <>
struct DepthwiseConvKernel<4, 1> {
  static void run(const float* inputData,
                  const float* filterData,
                  int inputHeight,
                  int inputWidth,
                  int outputChannels,
                  int outputHeight,
                  int outputWidth,
                  int filterMultiplier,
                  float* outputData) {
    const int steps = outputWidth >> 2;
    const int remain = outputWidth & 3;
    for (int c = 0; c < outputChannels; c++, filterData += 16) {
      // Load the filters
      float32x4_t k[4];
      k[0] = vld1q_f32(filterData);
      k[1] = vld1q_f32(filterData + 4);
      k[2] = vld1q_f32(filterData + 8);
      k[3] = vld1q_f32(filterData + 12);

      const float* r0 =
          inputData + (c / filterMultiplier) * (inputHeight * inputWidth);
      const float* r1 = r0 + inputWidth;
      const float* r2 = r0 + inputWidth * 2;
      const float* r3 = r0 + inputWidth * 3;
      float32x4_t input[4][4];
      for (int h = 0; h < outputHeight; h++) {
        for (int s = 0; s < steps; s++) {
          // Load the inputs
          float32x4_t tmp;
          input[0][0] = vld1q_f32(r0);
          tmp = vld1q_f32(r0 + 4);
          input[0][1] = vextq_f32(input[0][0], tmp, 1);
          input[0][2] = vextq_f32(input[0][0], tmp, 2);
          input[0][3] = vextq_f32(input[0][0], tmp, 3);

          input[1][0] = vld1q_f32(r1);
          tmp = vld1q_f32(r1 + 4);
          input[1][1] = vextq_f32(input[1][0], tmp, 1);
          input[1][2] = vextq_f32(input[1][0], tmp, 2);
          input[1][3] = vextq_f32(input[1][0], tmp, 3);

          input[2][0] = vld1q_f32(r2);
          tmp = vld1q_f32(r2 + 4);
          input[2][1] = vextq_f32(input[2][0], tmp, 1);
          input[2][2] = vextq_f32(input[2][0], tmp, 2);
          input[2][3] = vextq_f32(input[2][0], tmp, 3);

          input[3][0] = vld1q_f32(r3);
          tmp = vld1q_f32(r3 + 4);
          input[3][1] = vextq_f32(input[3][0], tmp, 1);
          input[3][2] = vextq_f32(input[3][0], tmp, 2);
          input[3][3] = vextq_f32(input[3][0], tmp, 3);

          float32x4_t tmp1 = vdupq_n_f32(0.f);
          float32x4_t tmp2 = vdupq_n_f32(0.f);
          tmp1 = vmlaq_laneq_f32(tmp1, input[0][0], k[0], 0);
          tmp2 = vmlaq_laneq_f32(tmp2, input[0][1], k[0], 1);
          tmp1 = vmlaq_laneq_f32(tmp1, input[0][2], k[0], 2);
          tmp2 = vmlaq_laneq_f32(tmp2, input[0][3], k[0], 3);
          tmp1 = vmlaq_laneq_f32(tmp1, input[1][0], k[1], 0);
          tmp2 = vmlaq_laneq_f32(tmp2, input[1][1], k[1], 1);
          tmp1 = vmlaq_laneq_f32(tmp1, input[1][2], k[1], 2);
          tmp2 = vmlaq_laneq_f32(tmp2, input[1][3], k[1], 3);
          tmp1 = vmlaq_laneq_f32(tmp1, input[2][0], k[2], 0);
          tmp2 = vmlaq_laneq_f32(tmp2, input[2][1], k[2], 1);
          tmp1 = vmlaq_laneq_f32(tmp1, input[2][2], k[2], 2);
          tmp2 = vmlaq_laneq_f32(tmp2, input[2][3], k[2], 3);
          tmp1 = vmlaq_laneq_f32(tmp1, input[3][0], k[3], 0);
          tmp2 = vmlaq_laneq_f32(tmp2, input[3][1], k[3], 1);
          tmp1 = vmlaq_laneq_f32(tmp1, input[3][2], k[3], 2);
          tmp2 = vmlaq_laneq_f32(tmp2, input[3][3], k[3], 3);
          tmp1 = vaddq_f32(tmp1, tmp2);

          vst1q_f32(outputData, tmp1);
          r0 += 4;
          r1 += 4;
          r2 += 4;
          r3 += 4;
          outputData += 4;
        }

        for (int r = 0; r < remain; r++) {
          float32x4_t i0 = vld1q_f32(r0);
          float32x4_t i1 = vld1q_f32(r1);
          float32x4_t i2 = vld1q_f32(r2);
          float32x4_t i3 = vld1q_f32(r3);
          *outputData = conv4x4(i0, i1, i2, i3, k[0], k[1], k[2], k[3]);
          r0++;
          r1++;
          r2++;
          r3++;
          outputData++;
        }

        r0 += 3;
        r1 += 3;
        r2 += 3;
        r3 += 3;
      }
    }
  }
};

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
/**
 * Each step calculates four elements of the output.
 */
template <>
struct DepthwiseConvKernel<4, 2> {
  static void run(const float* inputData,
                  const float* filterData,
                  int inputHeight,
                  int inputWidth,
                  int outputChannels,
                  int outputHeight,
                  int outputWidth,
                  int filterMultiplier,
                  float* outputData) {
    const int steps = outputWidth >> 2;
    const int remain = outputWidth & 3;
    for (int c = 0; c < outputChannels; c++, filterData += 16) {
      // Load the filters
      float32x4_t k[4];
      k[0] = vld1q_f32(filterData);
      k[1] = vld1q_f32(filterData + 4);
      k[2] = vld1q_f32(filterData + 8);
      k[3] = vld1q_f32(filterData + 12);

      const float* start =
          inputData + (c / filterMultiplier) * (inputHeight * inputWidth);
      float32x4_t input[4][4];
      for (int h = 0; h < outputHeight; h++) {
        const float* r0 = start + 2 * h * inputWidth;
        const float* r1 = start + (2 * h + 1) * inputWidth;
        const float* r2 = start + (2 * h + 2) * inputWidth;
        const float* r3 = start + (2 * h + 3) * inputWidth;
        for (int s = 0; s < steps; s++) {
          // Load the inputs
          float32x4x2_t data1;
          float32x4x2_t data2;

          data1 = vld2q_f32(r0);
          data2 = vld2q_f32(r0 + 8);
          input[0][0] = data1.val[0];
          input[0][1] = data1.val[1];
          input[0][2] = vextq_f32(data1.val[0], data2.val[0], 1);
          input[0][3] = vextq_f32(data1.val[1], data2.val[1], 1);

          data1 = vld2q_f32(r1);
          data2 = vld2q_f32(r1 + 8);
          input[1][0] = data1.val[0];
          input[1][1] = data1.val[1];
          input[1][2] = vextq_f32(data1.val[0], data2.val[0], 1);
          input[1][3] = vextq_f32(data1.val[1], data2.val[1], 1);

          data1 = vld2q_f32(r2);
          data2 = vld2q_f32(r2 + 8);
          input[2][0] = data1.val[0];
          input[2][1] = data1.val[1];
          input[2][2] = vextq_f32(data1.val[0], data2.val[0], 1);
          input[2][3] = vextq_f32(data1.val[1], data2.val[1], 1);

          data1 = vld2q_f32(r3);
          data2 = vld2q_f32(r3 + 8);
          input[3][0] = data1.val[0];
          input[3][1] = data1.val[1];
          input[3][2] = vextq_f32(data1.val[0], data2.val[0], 1);
          input[3][3] = vextq_f32(data1.val[1], data2.val[1], 1);

          float32x4_t tmp1 = vdupq_n_f32(0.f);
          float32x4_t tmp2 = vdupq_n_f32(0.f);
          tmp1 = vmlaq_laneq_f32(tmp1, input[0][0], k[0], 0);
          tmp2 = vmlaq_laneq_f32(tmp2, input[0][1], k[0], 1);
          tmp1 = vmlaq_laneq_f32(tmp1, input[0][2], k[0], 2);
          tmp2 = vmlaq_laneq_f32(tmp2, input[0][3], k[0], 3);
          tmp1 = vmlaq_laneq_f32(tmp1, input[1][0], k[1], 0);
          tmp2 = vmlaq_laneq_f32(tmp2, input[1][1], k[1], 1);
          tmp1 = vmlaq_laneq_f32(tmp1, input[1][2], k[1], 2);
          tmp2 = vmlaq_laneq_f32(tmp2, input[1][3], k[1], 3);
          tmp1 = vmlaq_laneq_f32(tmp1, input[2][0], k[2], 0);
          tmp2 = vmlaq_laneq_f32(tmp2, input[2][1], k[2], 1);
          tmp1 = vmlaq_laneq_f32(tmp1, input[2][2], k[2], 2);
          tmp2 = vmlaq_laneq_f32(tmp2, input[2][3], k[2], 3);
          tmp1 = vmlaq_laneq_f32(tmp1, input[3][0], k[3], 0);
          tmp2 = vmlaq_laneq_f32(tmp2, input[3][1], k[3], 1);
          tmp1 = vmlaq_laneq_f32(tmp1, input[3][2], k[3], 2);
          tmp2 = vmlaq_laneq_f32(tmp2, input[3][3], k[3], 3);
          tmp1 = vaddq_f32(tmp1, tmp2);

          vst1q_f32(outputData, tmp1);
          r0 += 8;
          r1 += 8;
          r2 += 8;
          r3 += 8;
          outputData += 4;
        }

        for (int r = 0; r < remain; r++) {
          float32x4_t i0 = vld1q_f32(r0);
          float32x4_t i1 = vld1q_f32(r1);
          float32x4_t i2 = vld1q_f32(r2);
          float32x4_t i3 = vld1q_f32(r3);
          *outputData = conv4x4(i0, i1, i2, i3, k[0], k[1], k[2], k[3]);
          r0 += 2;
          r1 += 2;
          r2 += 2;
          r3 += 2;
          outputData++;
        }
      }
    }
  }
};

H
hedaoyuan 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
template <DeviceType Device>
class NeonDepthwiseConvFunction : public ConvFunctionBase {
public:
  void init(const FuncConfig& config) override {
    ConvFunctionBase::init(config);
  }

  void check(const BufferArgs& inputs, const BufferArgs& outputs) override {
    const TensorShape& input = inputs[0].shape();
    const TensorShape& filter = inputs[1].shape();
    const TensorShape& output = outputs[0].shape();
    checkShape(input, filter, output);
  }

  void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
    CHECK_EQ(numInputs_, inputs.size());
    CHECK_EQ(numOutputs_, outputs.size());
    check(inputs, outputs);

    const TensorShape& input = inputs[0].shape();
    const TensorShape& filter = inputs[1].shape();
    const TensorShape& output = outputs[0].shape();

    size_t batchSize = input[0];
    size_t inputChannels = input[1];
    size_t inputHeight = input[2];
    size_t inputWidth = input[3];
    size_t filterHeight = getFilterHeight(filter);
    size_t filterWidth = getFilterWidth(filter);
    size_t outputChannels = output[1];
    size_t outputHeight = output[2];
    size_t outputWidth = output[3];
    size_t filterMultiplier = outputChannels / groups_;
    CHECK_EQ(inputChannels, groups_);

    // only support
    CHECK_EQ(strideH(), strideW());
    CHECK_EQ(filterHeight, filterWidth);
    CHECK_LT(strideH(), size_t(3));

    float* inputData = inputs[0].data<float>();
    float* filterData = inputs[1].data<float>();
    float* outputData = outputs[0].data<float>();

    // padding the input
    float* inputPadding = inputData;
    if (paddingH() > 0 || paddingW() > 0) {
      int newSize = batchSize * inputChannels * (inputHeight + 2 * paddingH()) *
                    (inputWidth + 2 * paddingW());
      resizeBuffer<Device>(newSize);
      inputPadding = reinterpret_cast<float*>(memory_->getBuf());
      Padding<float>::run(inputData,
                          inputPadding,
                          batchSize * inputChannels,
                          inputHeight,
                          inputWidth,
                          paddingH(),
                          paddingW());

      // height and width of padding data
      inputHeight += 2 * paddingH();
      inputWidth += 2 * paddingW();
    }

    for (size_t i = 0; i < batchSize; i++) {
542
      if (filterWidth == 3 && strideH() == 1) {
543 544 545 546 547
        DepthwiseConvKernel<3, 1>::run(inputPadding,
                                       filterData,
                                       inputHeight,
                                       inputWidth,
                                       outputChannels,
548 549 550 551 552 553 554 555 556 557
                                       outputHeight,
                                       outputWidth,
                                       filterMultiplier,
                                       outputData);
      } else if (filterWidth == 3 && strideH() == 2) {
        DepthwiseConvKernel<3, 2>::run(inputPadding,
                                       filterData,
                                       inputHeight,
                                       inputWidth,
                                       outputChannels,
558 559 560 561
                                       outputHeight,
                                       outputWidth,
                                       filterMultiplier,
                                       outputData);
562
      } else if (filterWidth == 4 && strideH() == 1) {
563 564 565 566 567 568 569 570 571
        DepthwiseConvKernel<4, 1>::run(inputPadding,
                                       filterData,
                                       inputHeight,
                                       inputWidth,
                                       outputChannels,
                                       outputHeight,
                                       outputWidth,
                                       filterMultiplier,
                                       outputData);
572 573 574 575 576 577 578 579 580 581
      } else if (filterWidth == 4 && strideH() == 2) {
        DepthwiseConvKernel<4, 2>::run(inputPadding,
                                       filterData,
                                       inputHeight,
                                       inputWidth,
                                       outputChannels,
                                       outputHeight,
                                       outputWidth,
                                       filterMultiplier,
                                       outputData);
582
      }
H
hedaoyuan 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595

      inputPadding += inputChannels * inputHeight * inputWidth;
      outputData += outputChannels * outputHeight * outputWidth;
    }
  }
};

REGISTER_TYPED_FUNC(NeonDepthwiseConv, CPU, NeonDepthwiseConvFunction);

#endif

}  // namespace neon
}  // namespace paddle