MNN/source/backend/opencl/execution/image/DepthwiseConvExecution.cpp

185 lines
8.8 KiB
C++
Raw Normal View History

2019-04-17 10:49:11 +08:00
//
// DepthwiseConvExecution.cpp
// MNN
//
// Created by MNN on 2019/02/28.
// Copyright © 2018, Alibaba Group Holding Limited
//
#include "backend/opencl/execution/image/DepthwiseConvExecution.hpp"
#include "backend/opencl/execution/image/MultiInputDWConvExecution.hpp"
2019-12-27 22:16:57 +08:00
#include "core/Macro.h"
2019-04-17 10:49:11 +08:00
#include <string.h>
2019-12-27 22:16:57 +08:00
#include "core/TensorUtils.hpp"
#include "backend/opencl/core/OpenCLRunningUtils.hpp"
2020-11-05 16:41:56 +08:00
#include "core/ConvolutionCommon.hpp"
2019-04-17 10:49:11 +08:00
namespace MNN {
namespace OpenCL {
2019-04-17 10:49:11 +08:00
DepthwiseConvExecution::DepthwiseConvExecution(const std::vector<Tensor *> &inputs, const MNN::Op *op, Backend *backend)
2024-04-19 11:58:21 +08:00
: ConvCommonExecution(op->main_as_Convolution2D(), backend), CommonExecution(backend, op) {
2019-04-17 10:49:11 +08:00
mOpenCLBackend = static_cast<OpenCLBackend *>(backend);
2024-04-19 11:58:21 +08:00
mResource->mConv2dParams = op->main_as_Convolution2D();
mResource->mConv2dCommonParams = mResource->mConv2dParams->common();
mResource->mStrides = {mResource->mConv2dCommonParams->strideY(), mResource->mConv2dCommonParams->strideX()};
mResource->mDilations = {mResource->mConv2dCommonParams->dilateY(), mResource->mConv2dCommonParams->dilateX()};
2019-04-17 10:49:11 +08:00
2024-04-19 11:58:21 +08:00
int kernelWidth = mResource->mConv2dCommonParams->kernelX();
int kernelHeight = mResource->mConv2dCommonParams->kernelY();
int outputChannel = mResource->mConv2dCommonParams->outputCount();
2019-04-17 10:49:11 +08:00
std::vector<int> filterShape{1, outputChannel, kernelHeight, kernelWidth};
std::vector<int> filterImageShape{(int)kernelHeight * kernelWidth, (int)UP_DIV(outputChannel, 4)};
2020-11-05 16:41:56 +08:00
const float* filterDataPtr = nullptr;
int filterDataSize = 0;
std::shared_ptr<ConvolutionCommon::Int8Common> quanCommon;
2024-08-24 15:46:21 +08:00
ConvolutionCommon::getConvParameters(&quanCommon, backend, op, &filterDataPtr, &filterDataSize);
2020-11-05 16:41:56 +08:00
2024-04-19 11:58:21 +08:00
mResource->mFilter.reset(Tensor::createDevice<float>({1, filterImageShape[1], 1, 4 * filterImageShape[0]}));
2019-04-17 10:49:11 +08:00
std::shared_ptr<Tensor> filterBuffer(Tensor::createDevice<float>(filterShape));
2020-11-05 16:41:56 +08:00
2024-05-11 19:17:02 +08:00
size_t buffer_size = filterBuffer->elementSize() * sizeof(float);
2020-11-05 16:41:56 +08:00
cl::Buffer filterBufferCL(mOpenCLBackend->getOpenCLRuntime()->context(), CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, buffer_size);
2019-04-17 10:49:11 +08:00
filterBuffer->buffer().device = (uint64_t)(&filterBufferCL);
cl_int error;
2020-11-05 16:41:56 +08:00
auto ptrCL = mOpenCLBackend->getOpenCLRuntime()->commandQueue().enqueueMapBuffer(filterBufferCL, true, CL_MAP_WRITE, 0, buffer_size, nullptr, nullptr, &error);
if(ptrCL != nullptr && error == CL_SUCCESS){
2024-05-11 19:17:02 +08:00
::memcpy(ptrCL, filterDataPtr, filterBuffer->size());
}else{
MNN_ERROR("Map error ptrCL == nullptr \n");
}
2019-04-17 10:49:11 +08:00
mOpenCLBackend->getOpenCLRuntime()->commandQueue().enqueueUnmapMemObject(filterBufferCL, ptrCL);
2024-04-19 11:58:21 +08:00
mOpenCLBackend->onAcquireBuffer(mResource->mFilter.get(), Backend::STATIC);
2019-04-17 10:49:11 +08:00
MNN::OpenCL::ImageBufferConvertor imageBufferConvertor{mOpenCLBackend->getOpenCLRuntime()};
2024-05-11 19:17:02 +08:00
std::string buildOption = "-DBUFFER_INP_FP32";
2025-04-28 11:38:44 +08:00
imageBufferConvertor.convertBufferToImage(filterBuffer.get(), MNN::OpenCL::DW_CONV2D_FILTER, mResource->mFilter.get(), mOpenCLBackend->getPrecision(), false, buildOption);
2019-04-17 10:49:11 +08:00
2024-04-19 11:58:21 +08:00
if (mResource->mConv2dCommonParams->relu() == true) {
mResource->mBuildOptions.emplace("-DRELU");
} else if (mResource->mConv2dCommonParams->relu6() == true) {
mResource->mBuildOptions.emplace("-DRELU6");
2019-04-17 10:49:11 +08:00
}
}
DepthwiseConvExecution::~DepthwiseConvExecution() {
2024-04-19 11:58:21 +08:00
// Do nothing
}
DepthwiseConvExecution::DepthwiseConvExecution(std::shared_ptr<ConvResource> resource, const MNN::Op* op, Backend *backend)
: ConvCommonExecution(backend), CommonExecution(backend, op) {
mResource = resource;
const auto *conv2dParams = op->main_as_Convolution2D();
const auto *conv2dCommonParams = conv2dParams->common();
mResource->mConv2dParams = conv2dParams;
mResource->mConv2dCommonParams = conv2dCommonParams;
}
bool DepthwiseConvExecution::onClone(Backend* bn, const Op* op, Execution** dst) {
if (!mValid) {
return false;
}
if (nullptr == dst) {
return true;
}
*dst = new DepthwiseConvExecution(mResource, op, bn);
return true;
2019-04-17 10:49:11 +08:00
}
2024-04-19 11:58:21 +08:00
ErrorCode DepthwiseConvExecution::onEncode(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
mUnits.resize(1);
auto &unit = mUnits[0];
auto runtime = mOpenCLBackend->getOpenCLRuntime();
2019-04-17 10:49:11 +08:00
auto input = inputs[0];
auto output = outputs[0];
std::vector<int> inputShape = tensorShapeFormat(input);
std::vector<int> outputShape = tensorShapeFormat(output);
2024-04-19 11:58:21 +08:00
std::string kernelName = "depthwise_conv2d";
bool S1D1 = false;
if (mResource->mConv2dCommonParams->strideX() == 1 && mResource->mConv2dCommonParams->strideY() == 1 &&
mResource->mConv2dCommonParams->dilateX() == 1 && mResource->mConv2dCommonParams->dilateY() == 1) {
kernelName = "depthwise_conv2d_s1";
S1D1 = true;
}
2025-04-28 11:38:44 +08:00
unit.kernel = runtime->buildKernel("depthwise_conv2d", kernelName, mResource->mBuildOptions, mOpenCLBackend->getPrecision());
2024-04-19 11:58:21 +08:00
mMaxWorkGroupSize = static_cast<uint32_t>(runtime->getMaxWorkGroupSize(unit.kernel));
2019-04-17 10:49:11 +08:00
mGlobalWorkSize = {static_cast<uint32_t>(UP_DIV(outputShape.at(3), 4) * UP_DIV(outputShape.at(2), 4)),
static_cast<uint32_t>(outputShape.at(0) * outputShape.at(1))};
2024-04-19 11:58:21 +08:00
auto padding = ConvolutionCommon::convolutionPad(input, output, mResource->mConv2dCommonParams);
2021-06-23 17:20:53 +08:00
mPaddings[0] = padding.second;//padY
mPaddings[1] = padding.first;//padX
2019-04-17 10:49:11 +08:00
const int outputHeight = outputShape.at(1);
const int outputWidth = outputShape.at(2);
2024-04-19 11:58:21 +08:00
const int outputChannels = outputShape.at(3);
2019-04-17 10:49:11 +08:00
const int inputHeight = inputShape.at(1);
const int inputWidth = inputShape.at(2);
const int inputChannels = inputShape.at(3);
const int inputChannelBlocks = UP_DIV(inputChannels, 4);
2024-04-19 11:58:21 +08:00
const int filterHeight = mResource->mConv2dParams->common()->kernelY();
const int filterWidth = mResource->mConv2dParams->common()->kernelX();
2019-04-17 10:49:11 +08:00
uint32_t idx = 0;
int inputImageShape[2] = {inputHeight, inputWidth};
int outputImageShape[2] = {outputHeight, outputWidth};
2024-04-19 11:58:21 +08:00
int strideShape[2] = {mResource->mStrides[0], mResource->mStrides[1]};
2021-06-23 17:20:53 +08:00
int paddingShape[2] = {mPaddings[0], mPaddings[1]};
2019-04-17 10:49:11 +08:00
int kernelShape[2] = {filterHeight, filterWidth};
2024-04-19 11:58:21 +08:00
int dilationShape[2] = {mResource->mDilations[0], mResource->mDilations[1]};
2024-04-19 11:58:21 +08:00
std::string info = std::to_string(inputChannels) + "_" + std::to_string(outputChannels) + "_" + std::to_string(filterHeight) + "_" + std::to_string(filterWidth) + "_" + std::to_string(mResource->mStrides[0]) + "_" + std::to_string(mResource->mStrides[1]) + std::to_string(mResource->mDilations[0]) + "_" + std::to_string(mResource->mDilations[1]);
unit.kernel->get().setArg(idx++, mGlobalWorkSize[0]);
unit.kernel->get().setArg(idx++, mGlobalWorkSize[1]);
unit.kernel->get().setArg(idx++, openCLImage(input));
unit.kernel->get().setArg(idx++, openCLImage(mResource->mFilter.get()));
unit.kernel->get().setArg(idx++, openCLImage(mResource->mBias.get()));
unit.kernel->get().setArg(idx++, openCLImage(output));
unit.kernel->get().setArg(idx++, sizeof(inputImageShape), inputImageShape);
unit.kernel->get().setArg(idx++, static_cast<int>(inputChannelBlocks));
unit.kernel->get().setArg(idx++, sizeof(outputImageShape), outputImageShape);
unit.kernel->get().setArg(idx++, sizeof(kernelShape), kernelShape);
unit.kernel->get().setArg(idx++, sizeof(paddingShape), paddingShape);
if (!S1D1) {
unit.kernel->get().setArg(idx++, sizeof(dilationShape), dilationShape);
unit.kernel->get().setArg(idx++, sizeof(strideShape), strideShape);
2023-06-16 09:42:45 +08:00
}
2024-04-19 11:58:21 +08:00
2025-04-28 11:38:44 +08:00
mLocalWorkSize = localWS2DDefault(mGlobalWorkSize, mMaxWorkGroupSize, mOpenCLBackend->getOpenCLRuntime(), kernelName + info, unit.kernel, mOpenCLBackend->getCLTuneLevel()).first;
2024-04-19 11:58:21 +08:00
mOpenCLBackend->recordKernel2d(unit.kernel, mGlobalWorkSize, mLocalWorkSize);
unit.globalWorkSize = {mGlobalWorkSize[0], mGlobalWorkSize[1]};
unit.localWorkSize = {mLocalWorkSize[0], mLocalWorkSize[1]};
2019-04-17 10:49:11 +08:00
return NO_ERROR;
}
2020-11-05 16:41:56 +08:00
class DepthwiseConvolutionCreator : public OpenCLBackend::Creator {
public:
virtual ~DepthwiseConvolutionCreator() = default;
virtual Execution *onCreate(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs,
const MNN::Op *op, Backend *backend) const override {
MNN_ASSERT(inputs.size() <= 3);
if (inputs.size() == 2 || inputs.size() == 3) {
return new MultiInputDWConvExecution(op, backend);
2020-11-05 16:41:56 +08:00
}
MNN_ASSERT(inputs.size() == 1);
return new DepthwiseConvExecution(inputs, op, backend);
}
};
2023-12-27 17:26:44 +08:00
REGISTER_OPENCL_OP_CREATOR(DepthwiseConvolutionCreator, OpType_ConvolutionDepthwise, IMAGE);
2019-04-17 10:49:11 +08:00
} // namespace OpenCL
} // namespace MNN