NNPACKConvOp.cpp 10.0 KB
Newer Older
H
hedaoyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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 "nnpack.h"
16
#include "paddle/function/ConvOp.h"
H
hedaoyuan 已提交
17 18

DEFINE_bool(nnpack_allocate_outside,
19
            true,
H
hedaoyuan 已提交
20 21 22 23 24 25 26 27 28 29 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
            "Allocate and free workspace memory outside the NNPACK interface.");
DEFINE_int32(nnpack_num_threads,
             0,
             "The number of nnpack threads"
             "default: 0; 0 to disable threadpool.");

namespace paddle {

nnp_convolution_algorithm get_nnp_convolution_algorithm(
    const std::string& algorithm) {
  if (algorithm == "auto") {
    return nnp_convolution_algorithm_auto;
  } else if (algorithm == "ft8x8") {
    return nnp_convolution_algorithm_ft8x8;
  } else if (algorithm == "ft16x16") {
    return nnp_convolution_algorithm_ft16x16;
  } else if (algorithm == "wt8x8") {
    return nnp_convolution_algorithm_wt8x8;
  } else if (algorithm == "implicit-gemm") {
    return nnp_convolution_algorithm_implicit_gemm;
  } else if (algorithm == "direct") {
    return nnp_convolution_algorithm_direct;
  } else {
    return nnp_convolution_algorithm_auto;
  }
}

template <DeviceType Device>
class NNPACKConvFunction : public ConvFunctionBase {
public:
  void init(const FuncConfig& config) override {
    ConvFunctionBase::init(config);
    algorithm_ = get_nnp_convolution_algorithm(config.get<std::string>("algo"));
    transform_strategy_ = nnp_convolution_transform_strategy_compute;
    nnp_status status = nnp_initialize();
    CHECK_EQ(status, nnp_status_success);
    workspaceBuffer_ = nullptr;
    workspaceSize_ = 0;

H
hedaoyuan 已提交
59
    create_nnpack_threadpool();
H
hedaoyuan 已提交
60 61 62
  }

  ~NNPACKConvFunction() {
63 64 65
    if (workspaceBuffer_) {
      free(workspaceBuffer_);
    }
H
hedaoyuan 已提交
66 67
  }

H
hedaoyuan 已提交
68
  void check(const BufferArgs& inputs, const BufferArgs& outputs) override {
H
hedaoyuan 已提交
69
    const TensorShape& input = inputs[0].shape();
70
    const TensorShape& filter = inputs[1].shape();
H
hedaoyuan 已提交
71
    const TensorShape& output = outputs[0].shape();
72 73 74
    checkShape(input, filter, output);
  }

H
hedaoyuan 已提交
75 76 77 78
  void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
    CHECK_EQ(numInputs_, inputs.size());
    CHECK_EQ(numOutputs_, outputs.size());
    CHECK_EQ(outputs[0].getArgType(), ASSIGN_TO);
79
    check(inputs, outputs);
H
hedaoyuan 已提交
80 81 82 83 84 85 86 87 88 89 90
    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];
H
hedaoyuan 已提交
91 92
    size_t outputHeight = output[2];
    size_t outputWidth = output[3];
H
hedaoyuan 已提交
93 94

    nnp_size inputSize = {.width = inputWidth, .height = inputHeight};
95 96 97 98
    nnp_padding padding = {.top = (size_t)paddingH(),
                           .right = (size_t)paddingW(),
                           .bottom = (size_t)paddingH(),
                           .left = (size_t)paddingW()};
H
hedaoyuan 已提交
99
    nnp_size kernelSize = {.width = filterWidth, .height = filterHeight};
100 101
    nnp_size outputSubsampling = {.width = (size_t)strideW(),
                                  .height = (size_t)strideH()};
H
hedaoyuan 已提交
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

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

    void* bufferPtr = nullptr;
    size_t* sizePtr = nullptr;
    size_t needSize;
    if (FLAGS_nnpack_allocate_outside) {
      if (batchSize == 1) {
        nnp_status status = nnp_convolution_inference(algorithm_,
                                                      transform_strategy_,
                                                      inputChannels,
                                                      outputChannels,
                                                      inputSize,
                                                      padding,
                                                      kernelSize,
                                                      outputSubsampling,
                                                      nullptr,
                                                      nullptr,
                                                      nullptr,
                                                      nullptr,
                                                      nullptr,
                                                      &needSize,
                                                      nnp_activation_identity,
                                                      nullptr,
                                                      nullptr,
                                                      nullptr);
        CHECK_EQ(status, nnp_status_success);
      } else {
        // only supports stride = 1
133 134
        CHECK_EQ(strideH(), 1);
        CHECK_EQ(strideW(), 1);
H
hedaoyuan 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        nnp_status status = nnp_convolution_output(algorithm_,
                                                   batchSize,
                                                   inputChannels,
                                                   outputChannels,
                                                   inputSize,
                                                   padding,
                                                   kernelSize,
                                                   nullptr,
                                                   nullptr,
                                                   nullptr,
                                                   nullptr,
                                                   nullptr,
                                                   &needSize,
                                                   nnp_activation_identity,
                                                   nullptr,
                                                   nullptr,
                                                   nullptr);
        CHECK_EQ(status, nnp_status_success);
      }

155
      VLOG(3) << "workspace size is " << needSize;
H
hedaoyuan 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
      if (needSize > workspaceSize_) {
        workspaceSize_ = needSize;
        if (workspaceBuffer_) {
          free(workspaceBuffer_);
        } else {
          posix_memalign(&workspaceBuffer_, 64, needSize);
        }
      }

      if (needSize) {
        bufferPtr = workspaceBuffer_;
        sizePtr = &needSize;
      }
    }

H
hedaoyuan 已提交
171 172 173 174
    size_t inputOffset = inputChannels / groups_ * inputHeight * inputWidth;
    size_t outputOffset = outputChannels / groups_ * outputHeight * outputWidth;
    size_t filterOffset = filter.getElements() / groups_;

H
hedaoyuan 已提交
175
    if (batchSize == 1) {
H
hedaoyuan 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
      for (size_t g = 0; g < groups_; g++) {
        nnp_status status =
            nnp_convolution_inference(algorithm_,
                                      transform_strategy_,
                                      inputChannels / groups_,
                                      outputChannels / groups_,
                                      inputSize,
                                      padding,
                                      kernelSize,
                                      outputSubsampling,
                                      inputData + inputOffset * g,
                                      filterData + filterOffset * g,
                                      nullptr, /* bias */
                                      outputData + outputOffset * g,
                                      bufferPtr,
                                      sizePtr,
                                      nnp_activation_identity,
                                      nullptr,
                                      threadpool_, /* threadpool */
                                      nullptr);
        CHECK_EQ(status, nnp_status_success);
      }
H
hedaoyuan 已提交
198
    } else {
199 200 201 202 203 204
      // only supports stride = 1
      CHECK_EQ(strideH(), 1);
      CHECK_EQ(strideW(), 1);

      // TODO(hedaoyuan): There has some bug when batchSize > 1 and groups_ > 1.
      CHECK_EQ(groups_, (size_t)1);
H
hedaoyuan 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
      for (size_t g = 0; g < groups_; g++) {
        nnp_status status =
            nnp_convolution_output(algorithm_,
                                   batchSize,
                                   inputChannels / groups_,
                                   outputChannels / groups_,
                                   inputSize,
                                   padding,
                                   kernelSize,
                                   inputData + inputOffset * g,
                                   filterData + filterOffset * g,
                                   nullptr, /* bias */
                                   outputData + outputOffset * g,
                                   bufferPtr,
                                   sizePtr,
                                   nnp_activation_identity,
                                   nullptr,
                                   threadpool_, /* threadpool */
                                   nullptr);
        CHECK_EQ(status, nnp_status_success);
      }
H
hedaoyuan 已提交
226 227 228
    }
  }

H
hedaoyuan 已提交
229 230 231 232 233 234 235 236
  static void create_nnpack_threadpool() {
    if (FLAGS_nnpack_num_threads && threadpool_ == nullptr) {
      threadpool_ = pthreadpool_create(FLAGS_nnpack_num_threads);
      VLOG(3) << "Number of threads "
              << pthreadpool_get_threads_count(threadpool_);
    }
  }

H
hedaoyuan 已提交
237 238 239 240 241
private:
  nnp_convolution_algorithm algorithm_;
  nnp_convolution_transform_strategy transform_strategy_;
  void* workspaceBuffer_;
  size_t workspaceSize_;
H
hedaoyuan 已提交
242
  static pthreadpool_t threadpool_;
H
hedaoyuan 已提交
243 244
};

H
hedaoyuan 已提交
245 246 247
template <DeviceType Device>
pthreadpool_t NNPACKConvFunction<Device>::threadpool_ = nullptr;

H
hedaoyuan 已提交
248 249 250
REGISTER_TYPED_FUNC(NNPACKConv, CPU, NNPACKConvFunction);

}  // namespace paddle