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

124 lines
5.1 KiB
C++
Raw Permalink Normal View History

2019-12-27 22:16:57 +08:00
//
// MatmulExecution.cpp
2019-12-27 22:16:57 +08:00
// MNN
//
// Created by MNN on 2019/02/28.
// Copyright © 2018, Alibaba Group Holding Limited
//
#include "backend/opencl/execution/image/MatmulExecution.hpp"
2019-12-27 22:16:57 +08:00
namespace MNN {
namespace OpenCL {
MatMulExecution::MatMulExecution(const std::vector<Tensor *> &inputs, const MNN::Op *op, Backend *backend,
2024-04-19 11:58:21 +08:00
bool transposeA, bool transposeB) : CommonExecution(backend, op)
, mTransposeA(transposeA), mTransposeB(transposeB){
2019-12-27 22:16:57 +08:00
mOpenCLBackend = static_cast<OpenCLBackend *>(backend);
mAreadySetArg = false;
}
2024-04-19 11:58:21 +08:00
ErrorCode MatMulExecution::onEncode(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
mUnits.resize(1);
auto &unit = mUnits[0];
2019-12-27 22:16:57 +08:00
auto runtime = mOpenCLBackend->getOpenCLRuntime();
2024-04-19 11:58:21 +08:00
2019-12-27 22:16:57 +08:00
Tensor *input0 = inputs[0];
Tensor *input1 = inputs[1];
Tensor *output = outputs[0];
2024-04-19 11:58:21 +08:00
2019-12-27 22:16:57 +08:00
std::vector<int> input0Shape = tensorShapeFormat(input0);
std::vector<int> input1Shape = tensorShapeFormat(input1);
std::vector<int> outputShape = tensorShapeFormat(output);
2020-11-05 16:41:56 +08:00
2024-04-19 11:58:21 +08:00
std::vector<uint32_t> mGlobalWorkSize{1, 1};
std::vector<uint32_t> mLocalWorkSize{1, 1, 1, 1};
std::string kernelName;
std::set<std::string> buildOptions;
if(mTransposeA) {
kernelName = mTransposeB ? "matmul_transA_transB":"matmul_transA";
} else {
kernelName = mTransposeB ? "matmul_transB":"matmul";
2020-11-05 16:41:56 +08:00
}
2024-04-19 11:58:21 +08:00
if(inputs.size() > 2) {
buildOptions.emplace("-DBIAS");
}
unit.kernel = runtime->buildKernel("matmul", kernelName, buildOptions);
mMaxWorkGroupSize = static_cast<uint32_t>(runtime->getMaxWorkGroupSize(unit.kernel));
2020-11-05 16:41:56 +08:00
2019-12-27 22:16:57 +08:00
//处理二维矩阵相乘N C相当于H W
//二维矩阵相乘
2020-11-05 16:41:56 +08:00
if(mTransposeA) {
const int height = input0Shape.at(3);
const int outputChannel = input0Shape.at(0);
const int width = mTransposeB ? input1Shape.at(0): input1Shape.at(3);
const int outputChannelBlocks = UP_DIV(outputChannel, 4);
const int widthblocks = UP_DIV(width, 4);
const int heightblocks = UP_DIV(height, 4);
mGlobalWorkSize = {static_cast<uint32_t>(widthblocks), static_cast<uint32_t>(heightblocks)};
2023-07-31 14:24:48 +08:00
cl_int ret = CL_SUCCESS;
int idx = 0;
2024-04-19 11:58:21 +08:00
ret |= unit.kernel->get().setArg(idx++, mGlobalWorkSize[0]);
ret |= unit.kernel->get().setArg(idx++, mGlobalWorkSize[1]);
ret |= unit.kernel->get().setArg(idx++, openCLImage(input0));
ret |= unit.kernel->get().setArg(idx++, openCLImage(input1));
2023-07-31 14:24:48 +08:00
if(inputs.size() > 2) {
2024-04-19 11:58:21 +08:00
ret |= unit.kernel->get().setArg(idx++, openCLImage(inputs[2]));
2023-07-31 14:24:48 +08:00
}
2024-04-19 11:58:21 +08:00
ret |= unit.kernel->get().setArg(idx++, openCLImage(output));
ret |= unit.kernel->get().setArg(idx++, static_cast<int>(outputChannel));
ret |= unit.kernel->get().setArg(idx++, static_cast<int>(outputChannelBlocks));
ret |= unit.kernel->get().setArg(idx++, static_cast<int>(height));
2023-07-31 14:24:48 +08:00
MNN_CHECK_CL_SUCCESS(ret, "setArg MatMulExecution transposeA");
mLocalWorkSize = {mMaxWorkGroupSize / 64, 64, 0};
2020-11-05 16:41:56 +08:00
}
else {
const int height = input0Shape.at(0);
const int outputChannel = input0Shape.at(3);
const int width = mTransposeB ? input1Shape.at(0): input1Shape.at(3);
const int outputChannelBlocks = UP_DIV(outputChannel, 4);
const int widthblocks = UP_DIV(width, 4);
mGlobalWorkSize = {static_cast<uint32_t>(widthblocks), static_cast<uint32_t>(height)};
int idx = 0;
2023-07-31 14:24:48 +08:00
cl_int ret = CL_SUCCESS;
2024-04-19 11:58:21 +08:00
ret |= unit.kernel->get().setArg(idx++, mGlobalWorkSize[0]);
ret |= unit.kernel->get().setArg(idx++, mGlobalWorkSize[1]);
ret |= unit.kernel->get().setArg(idx++, openCLImage(input0));
ret |= unit.kernel->get().setArg(idx++, openCLImage(input1));
2020-11-05 16:41:56 +08:00
if(inputs.size() > 2) {
2024-04-19 11:58:21 +08:00
ret |= unit.kernel->get().setArg(idx++, openCLImage(inputs[2]));
2020-11-05 16:41:56 +08:00
}
2024-04-19 11:58:21 +08:00
ret |= unit.kernel->get().setArg(idx++, openCLImage(output));
ret |= unit.kernel->get().setArg(idx++, static_cast<int>(outputChannel));
ret |= unit.kernel->get().setArg(idx++, static_cast<int>(outputChannelBlocks));
2023-07-31 14:24:48 +08:00
MNN_CHECK_CL_SUCCESS(ret, "setArg MatMulExecution transposeA");
2020-11-05 16:41:56 +08:00
mLocalWorkSize = {mMaxWorkGroupSize / 64, 64, 0};
}
2023-06-16 09:42:45 +08:00
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-12-27 22:16:57 +08:00
return NO_ERROR;
}
class MatMulCreator : public OpenCLBackend::Creator {
public:
virtual Execution *onCreate(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs,
const MNN::Op *op, Backend *backend) const override {
auto param = op->main_as_MatMul();
return new MatMulExecution(inputs, op, backend, param->transposeA(), param->transposeB());
}
};
2023-12-27 17:26:44 +08:00
REGISTER_OPENCL_OP_CREATOR(MatMulCreator, OpType_MatMul, IMAGE);
2019-12-27 22:16:57 +08:00
} // namespace OpenCL
} // namespace MNN