NeonDepthwiseConv.cpp 4.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
H
hedaoyuan 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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

15
#include "NeonDepthwiseConv.h"
H
hedaoyuan 已提交
16 17 18 19 20 21 22 23
#include "paddle/function/ConvOp.h"

namespace paddle {

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

template <DeviceType Device>
class NeonDepthwiseConvFunction : public ConvFunctionBase {
W
Wu Yi 已提交
24
 public:
H
hedaoyuan 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  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();

H
hedaoyuan 已提交
45 46 47 48 49 50 51 52 53 54
    int batchSize = input[0];
    int inputChannels = input[1];
    int inputHeight = input[2];
    int inputWidth = input[3];
    int filterHeight = getFilterHeight(filter);
    int filterWidth = getFilterWidth(filter);
    int outputChannels = output[1];
    int outputHeight = output[2];
    int outputWidth = output[3];
    int filterMultiplier = outputChannels / groups_;
55
    CHECK_EQ(static_cast<size_t>(inputChannels), groups_);
H
hedaoyuan 已提交
56

H
hedaoyuan 已提交
57
    // only support strideH() == strideW() and filterHeight == filterWidth.
H
hedaoyuan 已提交
58 59 60 61 62 63 64 65 66
    CHECK_EQ(strideH(), strideW());
    CHECK_EQ(filterHeight, filterWidth);

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

    // padding the input
    float* inputPadding = inputData;
67 68
    int padInputHeight = inputHeight + 2 * paddingH();
    int padInputWidth = inputWidth + 2 * paddingW();
Z
zlx 已提交
69 70 71 72
    int newSize = batchSize * (inputChannels + 1) * padInputHeight * padInputWidth;
    resizeBuffer<Device>(newSize);
    inputPadding = reinterpret_cast<float*>(memory_->getBuf());
    neon::Padding<float>::run(inputData,
73 74 75 76
                                inputPadding,
                                batchSize * inputChannels,
                                inputHeight,
                                inputWidth,
77 78
                                padInputHeight,
                                padInputWidth);
H
hedaoyuan 已提交
79

H
hedaoyuan 已提交
80 81 82 83 84
    std::function<void(
        const float*, const float*, int, int, int, int, int, int, float*)>
        DepthWiseConv;

    if (filterWidth == 3 && strideW() == 1) {
H
hedaoyuan 已提交
85
      DepthWiseConv = neon::DepthwiseConvKernel<3, 1>::run;
H
hedaoyuan 已提交
86
    } else if (filterWidth == 3 && strideW() == 2) {
H
hedaoyuan 已提交
87
      DepthWiseConv = neon::DepthwiseConvKernel<3, 2>::run;
H
hedaoyuan 已提交
88
    } else if (filterWidth == 4 && strideW() == 1) {
H
hedaoyuan 已提交
89
      DepthWiseConv = neon::DepthwiseConvKernel<4, 1>::run;
H
hedaoyuan 已提交
90
    } else if (filterWidth == 4 && strideW() == 2) {
H
hedaoyuan 已提交
91
      DepthWiseConv = neon::DepthwiseConvKernel<4, 2>::run;
H
hedaoyuan 已提交
92 93 94
    } else {
      LOG(FATAL) << "Not supported";
    }
H
hedaoyuan 已提交
95

H
hedaoyuan 已提交
96
    for (int i = 0; i < batchSize; i++) {
H
hedaoyuan 已提交
97 98
      DepthWiseConv(inputPadding,
                    filterData,
99 100
                    padInputHeight,
                    padInputWidth,
H
hedaoyuan 已提交
101 102 103 104 105
                    outputChannels,
                    outputHeight,
                    outputWidth,
                    filterMultiplier,
                    outputData);
106
      inputPadding += inputChannels * padInputHeight * padInputWidth;
H
hedaoyuan 已提交
107 108 109 110 111
      outputData += outputChannels * outputHeight * outputWidth;
    }
  }
};

H
hedaoyuan 已提交
112
#ifndef PADDLE_TYPE_DOUBLE
H
hedaoyuan 已提交
113
REGISTER_TYPED_FUNC(NeonDepthwiseConv, CPU, NeonDepthwiseConvFunction);
H
hedaoyuan 已提交
114
#endif
H
hedaoyuan 已提交
115 116 117 118

#endif

}  // namespace paddle