2019-12-27 22:16:57 +08:00
|
|
|
|
//
|
2021-03-12 18:41:50 +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
|
|
|
|
|
//
|
|
|
|
|
|
2021-03-12 18:41:50 +08:00
|
|
|
|
#include "backend/opencl/execution/image/MatmulExecution.hpp"
|
2019-12-27 22:16:57 +08:00
|
|
|
|
|
|
|
|
|
namespace MNN {
|
|
|
|
|
namespace OpenCL {
|
|
|
|
|
|
2020-04-29 12:10:16 +08:00
|
|
|
|
MatMulExecution::MatMulExecution(const std::vector<Tensor *> &inputs, const MNN::Op *op, Backend *backend,
|
|
|
|
|
bool transposeA, bool transposeB) : Execution(backend)
|
|
|
|
|
, mTransposeA(transposeA), mTransposeB(transposeB){
|
2019-12-27 22:16:57 +08:00
|
|
|
|
mOpenCLBackend = static_cast<OpenCLBackend *>(backend);
|
|
|
|
|
mAreadySetArg = false;
|
|
|
|
|
}
|
|
|
|
|
ErrorCode MatMulExecution::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
|
|
|
|
|
auto runtime = mOpenCLBackend->getOpenCLRuntime();
|
2023-06-16 09:42:45 +08:00
|
|
|
|
startRecord(runtime, mRecording);
|
2019-12-27 22:16:57 +08:00
|
|
|
|
|
|
|
|
|
Tensor *input0 = inputs[0];
|
|
|
|
|
Tensor *input1 = inputs[1];
|
|
|
|
|
Tensor *output = outputs[0];
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
if (mKernel.get() == nullptr) {
|
|
|
|
|
|
|
|
|
|
std::string kernelName;
|
|
|
|
|
std::set<std::string> buildOptions;
|
|
|
|
|
if(mTransposeA) {
|
|
|
|
|
kernelName = mTransposeB ? "matmul_transA_transB":"matmul_transA";
|
|
|
|
|
} else {
|
|
|
|
|
kernelName = mTransposeB ? "matmul_transB":"matmul";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(inputs.size() > 2) {
|
|
|
|
|
buildOptions.emplace("-DBIAS");
|
|
|
|
|
}
|
|
|
|
|
mKernel = runtime->buildKernel("matmul", kernelName, buildOptions);
|
|
|
|
|
mMaxWorkGroupSize = static_cast<uint32_t>(runtime->getMaxWorkGroupSize(mKernel));
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
ret |= mKernel.setArg(idx++, mGlobalWorkSize[0]);
|
|
|
|
|
ret |= mKernel.setArg(idx++, mGlobalWorkSize[1]);
|
|
|
|
|
ret |= mKernel.setArg(idx++, openCLImage(input0));
|
|
|
|
|
ret |= mKernel.setArg(idx++, openCLImage(input1));
|
|
|
|
|
if(inputs.size() > 2) {
|
|
|
|
|
ret |= mKernel.setArg(idx++, openCLImage(inputs[2]));
|
|
|
|
|
}
|
|
|
|
|
ret |= mKernel.setArg(idx++, openCLImage(output));
|
|
|
|
|
ret |= mKernel.setArg(idx++, static_cast<int>(outputChannel));
|
|
|
|
|
ret |= mKernel.setArg(idx++, static_cast<int>(outputChannelBlocks));
|
|
|
|
|
ret |= mKernel.setArg(idx++, static_cast<int>(height));
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
ret |= mKernel.setArg(idx++, mGlobalWorkSize[0]);
|
|
|
|
|
ret |= mKernel.setArg(idx++, mGlobalWorkSize[1]);
|
|
|
|
|
ret |= mKernel.setArg(idx++, openCLImage(input0));
|
|
|
|
|
ret |= mKernel.setArg(idx++, openCLImage(input1));
|
2020-11-05 16:41:56 +08:00
|
|
|
|
if(inputs.size() > 2) {
|
2023-07-31 14:24:48 +08:00
|
|
|
|
ret |= mKernel.setArg(idx++, openCLImage(inputs[2]));
|
2020-11-05 16:41:56 +08:00
|
|
|
|
}
|
2023-07-31 14:24:48 +08:00
|
|
|
|
ret |= mKernel.setArg(idx++, openCLImage(output));
|
|
|
|
|
ret |= mKernel.setArg(idx++, static_cast<int>(outputChannel));
|
|
|
|
|
ret |= mKernel.setArg(idx++, static_cast<int>(outputChannelBlocks));
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
recordKernel2d(mKernel, mGlobalWorkSize, mLocalWorkSize, mOpenCLBackend->getOpenCLRuntime());
|
|
|
|
|
endRecord(runtime, mRecording);
|
2019-12-27 22:16:57 +08:00
|
|
|
|
return NO_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorCode MatMulExecution::onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
|
|
|
|
|
#ifdef LOG_VERBOSE
|
|
|
|
|
MNN_PRINT("Start MatMulExecution onExecute... \n");
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
auto runtime = mOpenCLBackend->getOpenCLRuntime();
|
|
|
|
|
|
2020-11-05 16:41:56 +08:00
|
|
|
|
#ifdef ENABLE_OPENCL_TIME_PROFILER
|
|
|
|
|
cl::Event event;
|
|
|
|
|
runKernel2D(mKernel, mGlobalWorkSize, mLocalWorkSize, runtime, &event);
|
2023-08-31 15:31:45 +08:00
|
|
|
|
|
|
|
|
|
mOpenCLBackend->getOpenCLRuntime()->pushEvent({"Matmul", event});
|
2020-11-05 16:41:56 +08:00
|
|
|
|
#else
|
2023-06-16 09:42:45 +08:00
|
|
|
|
if(mOpenCLBackend->getOpenCLRuntime()->isUseRecordQueue()){
|
2023-08-21 14:51:54 +08:00
|
|
|
|
if(mOpenCLBackend->getOpenCLRuntime()->isDevideOpRecord())
|
|
|
|
|
mOpenCLBackend->getOpenCLRuntime()->getRecordings()->emplace_back(mRecording);
|
2023-06-16 09:42:45 +08:00
|
|
|
|
#ifdef LOG_VERBOSE
|
|
|
|
|
MNN_PRINT("End MatMulExecution onExecute... \n");
|
|
|
|
|
#endif
|
|
|
|
|
return NO_ERROR;
|
|
|
|
|
}
|
2020-11-05 16:41:56 +08:00
|
|
|
|
runKernel2D(mKernel, mGlobalWorkSize, mLocalWorkSize, runtime, nullptr);
|
|
|
|
|
#endif
|
2020-05-28 19:04:27 +08:00
|
|
|
|
|
2019-12-27 22:16:57 +08:00
|
|
|
|
#ifdef LOG_VERBOSE
|
|
|
|
|
MNN_PRINT("End MatMulExecution onExecute... \n");
|
|
|
|
|
#endif
|
|
|
|
|
return NO_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 12:10:16 +08:00
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-12 18:41:50 +08:00
|
|
|
|
OpenCLCreatorRegister<MatMulCreator> __matmul_op(OpType_MatMul, IMAGE);
|
2019-12-27 22:16:57 +08:00
|
|
|
|
|
|
|
|
|
} // namespace OpenCL
|
|
|
|
|
} // namespace MNN
|