|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "nnpack.h" |
| 16 | +#include "paddle/function/ConvOp.h" |
| 17 | + |
| 18 | +DEFINE_bool(nnpack_allocate_outside, |
| 19 | + false, |
| 20 | + "Allocate and free workspace memory outside the NNPACK interface."); |
| 21 | +DEFINE_int32(nnpack_num_threads, |
| 22 | + 0, |
| 23 | + "The number of nnpack threads" |
| 24 | + "default: 0; 0 to disable threadpool."); |
| 25 | + |
| 26 | +namespace paddle { |
| 27 | + |
| 28 | +nnp_convolution_algorithm get_nnp_convolution_algorithm( |
| 29 | + const std::string& algorithm) { |
| 30 | + if (algorithm == "auto") { |
| 31 | + return nnp_convolution_algorithm_auto; |
| 32 | + } else if (algorithm == "ft8x8") { |
| 33 | + return nnp_convolution_algorithm_ft8x8; |
| 34 | + } else if (algorithm == "ft16x16") { |
| 35 | + return nnp_convolution_algorithm_ft16x16; |
| 36 | + } else if (algorithm == "wt8x8") { |
| 37 | + return nnp_convolution_algorithm_wt8x8; |
| 38 | + } else if (algorithm == "implicit-gemm") { |
| 39 | + return nnp_convolution_algorithm_implicit_gemm; |
| 40 | + } else if (algorithm == "direct") { |
| 41 | + return nnp_convolution_algorithm_direct; |
| 42 | + } else { |
| 43 | + return nnp_convolution_algorithm_auto; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +template <DeviceType Device> |
| 48 | +class NNPACKConvFunction : public ConvFunctionBase { |
| 49 | +public: |
| 50 | + void init(const FuncConfig& config) override { |
| 51 | + ConvFunctionBase::init(config); |
| 52 | + CHECK_EQ(groups_, (size_t)1); |
| 53 | + algorithm_ = get_nnp_convolution_algorithm(config.get<std::string>("algo")); |
| 54 | + // algorithm_ = nnp_convolution_algorithm_auto; |
| 55 | + transform_strategy_ = nnp_convolution_transform_strategy_compute; |
| 56 | + nnp_status status = nnp_initialize(); |
| 57 | + CHECK_EQ(status, nnp_status_success); |
| 58 | + workspaceBuffer_ = nullptr; |
| 59 | + workspaceSize_ = 0; |
| 60 | + |
| 61 | + threadpool_ = nullptr; |
| 62 | + if (FLAGS_nnpack_num_threads) { |
| 63 | + threadpool_ = pthreadpool_create(FLAGS_nnpack_num_threads); |
| 64 | + VLOG(3) << "Number of threads " |
| 65 | + << pthreadpool_get_threads_count(threadpool_); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + ~NNPACKConvFunction() { |
| 70 | + if (threadpool_) { |
| 71 | + pthreadpool_destroy(threadpool_); |
| 72 | + } |
| 73 | + if (workspaceBuffer_) { |
| 74 | + free(workspaceBuffer_); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + virtual void check(const BufferArgs& inputs, |
| 79 | + const BufferArgs& outputs) override { |
| 80 | + const TensorShape& input = inputs[0].shape(); |
| 81 | + const TensorShape& filter = inputs[1].shape(); |
| 82 | + const TensorShape& output = outputs[0].shape(); |
| 83 | + checkShape(input, filter, output); |
| 84 | + } |
| 85 | + |
| 86 | + void calc(const BufferArgs& inputs, const BufferArgs& outputs) override { |
| 87 | + CHECK_EQ(numInputs_, inputs.size()); |
| 88 | + CHECK_EQ(numOutputs_, outputs.size()); |
| 89 | + CHECK_EQ(outputs[0].getArgType(), ASSIGN_TO); |
| 90 | + check(inputs, outputs); |
| 91 | + const TensorShape& input = inputs[0].shape(); |
| 92 | + const TensorShape& filter = inputs[1].shape(); |
| 93 | + const TensorShape& output = outputs[0].shape(); |
| 94 | + |
| 95 | + size_t batchSize = input[0]; |
| 96 | + size_t inputChannels = input[1]; |
| 97 | + size_t inputHeight = input[2]; |
| 98 | + size_t inputWidth = input[3]; |
| 99 | + size_t filterHeight = getFilterHeight(filter); |
| 100 | + size_t filterWidth = getFilterWidth(filter); |
| 101 | + size_t outputChannels = output[1]; |
| 102 | + // size_t outputHeight = output[2]; |
| 103 | + // size_t outputWidth = output[3]; |
| 104 | + |
| 105 | + nnp_size inputSize = {.width = inputWidth, .height = inputHeight}; |
| 106 | + nnp_padding padding = {.top = (size_t)paddingH(), |
| 107 | + .right = (size_t)paddingW(), |
| 108 | + .bottom = (size_t)paddingH(), |
| 109 | + .left = (size_t)paddingW()}; |
| 110 | + nnp_size kernelSize = {.width = filterWidth, .height = filterHeight}; |
| 111 | + nnp_size outputSubsampling = {.width = (size_t)strideW(), |
| 112 | + .height = (size_t)strideH()}; |
| 113 | + |
| 114 | + float* inputData = inputs[0].data<float>(); |
| 115 | + float* filterData = inputs[1].data<float>(); |
| 116 | + float* outputData = outputs[0].data<float>(); |
| 117 | + |
| 118 | + void* bufferPtr = nullptr; |
| 119 | + size_t* sizePtr = nullptr; |
| 120 | + size_t needSize; |
| 121 | + if (FLAGS_nnpack_allocate_outside) { |
| 122 | + if (batchSize == 1) { |
| 123 | + nnp_status status = nnp_convolution_inference(algorithm_, |
| 124 | + transform_strategy_, |
| 125 | + inputChannels, |
| 126 | + outputChannels, |
| 127 | + inputSize, |
| 128 | + padding, |
| 129 | + kernelSize, |
| 130 | + outputSubsampling, |
| 131 | + nullptr, |
| 132 | + nullptr, |
| 133 | + nullptr, |
| 134 | + nullptr, |
| 135 | + nullptr, |
| 136 | + &needSize, |
| 137 | + nnp_activation_identity, |
| 138 | + nullptr, |
| 139 | + nullptr, |
| 140 | + nullptr); |
| 141 | + CHECK_EQ(status, nnp_status_success); |
| 142 | + } else { |
| 143 | + // only supports stride = 1 |
| 144 | + CHECK_EQ(strideH(), 1); |
| 145 | + CHECK_EQ(strideW(), 1); |
| 146 | + nnp_status status = nnp_convolution_output(algorithm_, |
| 147 | + batchSize, |
| 148 | + inputChannels, |
| 149 | + outputChannels, |
| 150 | + inputSize, |
| 151 | + padding, |
| 152 | + kernelSize, |
| 153 | + nullptr, |
| 154 | + nullptr, |
| 155 | + nullptr, |
| 156 | + nullptr, |
| 157 | + nullptr, |
| 158 | + &needSize, |
| 159 | + nnp_activation_identity, |
| 160 | + nullptr, |
| 161 | + nullptr, |
| 162 | + nullptr); |
| 163 | + CHECK_EQ(status, nnp_status_success); |
| 164 | + } |
| 165 | + |
| 166 | + VLOG(3) << "workspace size is " << needSize; |
| 167 | + if (needSize > workspaceSize_) { |
| 168 | + workspaceSize_ = needSize; |
| 169 | + if (workspaceBuffer_) { |
| 170 | + free(workspaceBuffer_); |
| 171 | + } else { |
| 172 | + posix_memalign(&workspaceBuffer_, 64, needSize); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + if (needSize) { |
| 177 | + bufferPtr = workspaceBuffer_; |
| 178 | + sizePtr = &needSize; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + if (batchSize == 1) { |
| 183 | + nnp_status status = |
| 184 | + nnp_convolution_inference(algorithm_, |
| 185 | + transform_strategy_, |
| 186 | + inputChannels, |
| 187 | + outputChannels, |
| 188 | + inputSize, |
| 189 | + padding, |
| 190 | + kernelSize, |
| 191 | + outputSubsampling, |
| 192 | + inputData, |
| 193 | + filterData, |
| 194 | + nullptr, /* bias */ |
| 195 | + outputData, |
| 196 | + bufferPtr, |
| 197 | + sizePtr, |
| 198 | + nnp_activation_identity, |
| 199 | + nullptr, |
| 200 | + threadpool_, /* threadpool */ |
| 201 | + nullptr); |
| 202 | + CHECK_EQ(status, nnp_status_success); |
| 203 | + } else { |
| 204 | + // only supports stride = 1 |
| 205 | + CHECK_EQ(strideH(), 1); |
| 206 | + CHECK_EQ(strideW(), 1); |
| 207 | + nnp_status status = nnp_convolution_output(algorithm_, |
| 208 | + batchSize, |
| 209 | + inputChannels, |
| 210 | + outputChannels, |
| 211 | + inputSize, |
| 212 | + padding, |
| 213 | + kernelSize, |
| 214 | + inputData, |
| 215 | + filterData, |
| 216 | + nullptr, /* bias */ |
| 217 | + outputData, |
| 218 | + bufferPtr, |
| 219 | + sizePtr, |
| 220 | + nnp_activation_identity, |
| 221 | + nullptr, |
| 222 | + threadpool_, /* threadpool */ |
| 223 | + nullptr); |
| 224 | + CHECK_EQ(status, nnp_status_success); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | +private: |
| 229 | + nnp_convolution_algorithm algorithm_; |
| 230 | + nnp_convolution_transform_strategy transform_strategy_; |
| 231 | + void* workspaceBuffer_; |
| 232 | + size_t workspaceSize_; |
| 233 | + pthreadpool_t threadpool_; |
| 234 | +}; |
| 235 | + |
| 236 | +REGISTER_TYPED_FUNC(NNPACKConv, CPU, NNPACKConvFunction); |
| 237 | + |
| 238 | +} // namespace paddle |
0 commit comments