2019-04-17 10:49:11 +08:00
|
|
|
//
|
|
|
|
// SoftmaxExecution.cpp
|
|
|
|
// 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/SoftmaxExecution.hpp"
|
2019-12-27 22:16:57 +08:00
|
|
|
#include "core/Macro.h"
|
2020-07-03 10:27:58 +08:00
|
|
|
#include "backend/opencl/core/OpenCLRunningUtils.hpp"
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
namespace MNN {
|
|
|
|
namespace OpenCL {
|
|
|
|
|
|
|
|
SoftmaxExecution::SoftmaxExecution(const std::vector<Tensor *> &inputs, int axis, Backend *backend)
|
|
|
|
: Execution(backend) {
|
|
|
|
mAxis = axis;
|
|
|
|
mOpenCLBackend = static_cast<OpenCLBackend *>(backend);
|
|
|
|
buildSoftmaxKernel();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SoftmaxExecution::buildSoftmaxKernel() {
|
|
|
|
auto runtime = mOpenCLBackend->getOpenCLRuntime();
|
|
|
|
if (mKernel.get() == nullptr) {
|
|
|
|
std::set<std::string> buildOptions;
|
|
|
|
std::string kernelName;
|
|
|
|
if (mAxis == 1) {
|
|
|
|
mKernel = runtime->buildKernel("softmax", "softmax_channel", buildOptions);
|
2020-11-05 10:14:01 +08:00
|
|
|
} else if (mAxis == 2) {
|
2021-03-12 18:41:50 +08:00
|
|
|
mKernel = runtime->buildKernel("softmax", "softmax_height", buildOptions);
|
2020-11-05 10:14:01 +08:00
|
|
|
} else {
|
|
|
|
MNN_ASSERT(mAxis == 3);
|
2021-03-12 18:41:50 +08:00
|
|
|
mKernel = runtime->buildKernel("softmax", "softmax_width", buildOptions);
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
mMaxWorkGroupSize = static_cast<uint32_t>(runtime->getMaxWorkGroupSize(mKernel));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorCode SoftmaxExecution::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
|
2023-06-16 09:42:45 +08:00
|
|
|
startRecord(mOpenCLBackend->getOpenCLRuntime(), mRecording);
|
2019-04-17 10:49:11 +08:00
|
|
|
Tensor *input = inputs[0];
|
|
|
|
Tensor *output = outputs[0];
|
2019-12-27 22:16:57 +08:00
|
|
|
|
2019-04-17 10:49:11 +08:00
|
|
|
std::vector<int> inputShape = tensorShapeFormat(input);
|
|
|
|
std::vector<int> outputShape = tensorShapeFormat(output);
|
2019-12-27 22:16:57 +08:00
|
|
|
|
2019-04-17 10:49:11 +08:00
|
|
|
const int outputBatch = outputShape.at(0);
|
|
|
|
const int outputHeight = outputShape.at(1);
|
|
|
|
const int outputWidth = outputShape.at(2);
|
|
|
|
const int outputChannels = outputShape.at(3);
|
2019-12-27 22:16:57 +08:00
|
|
|
|
2019-04-17 10:49:11 +08:00
|
|
|
const int channelBlocks = UP_DIV(outputChannels, 4);
|
|
|
|
const int remainChannels = channelBlocks * 4 - outputChannels;
|
2023-07-31 14:24:48 +08:00
|
|
|
cl_int ret = CL_SUCCESS;
|
2020-11-05 10:14:01 +08:00
|
|
|
if (mAxis == 1) {
|
2019-04-17 10:49:11 +08:00
|
|
|
mGlobalWorkSize = {static_cast<uint32_t>(channelBlocks), static_cast<uint32_t>(outputWidth),
|
|
|
|
static_cast<uint32_t>(outputHeight * outputBatch)};
|
2020-06-16 17:11:54 +08:00
|
|
|
|
2019-04-17 10:49:11 +08:00
|
|
|
uint32_t idx = 0;
|
2023-07-31 14:24:48 +08:00
|
|
|
ret |= mKernel.setArg(idx++, mGlobalWorkSize[0]);
|
|
|
|
ret |= mKernel.setArg(idx++, mGlobalWorkSize[1]);
|
|
|
|
ret |= mKernel.setArg(idx++, mGlobalWorkSize[2]);
|
|
|
|
|
|
|
|
ret |= mKernel.setArg(idx++, openCLImage(input));
|
|
|
|
ret |= mKernel.setArg(idx++, openCLImage(output));
|
|
|
|
ret |= mKernel.setArg(idx++, static_cast<int>(outputChannels));
|
|
|
|
ret |= mKernel.setArg(idx++, remainChannels);
|
|
|
|
MNN_CHECK_CL_SUCCESS(ret, "setArg SoftmaxExecution Axis_1");
|
2019-12-27 22:16:57 +08:00
|
|
|
|
2021-03-12 18:41:50 +08:00
|
|
|
std::string kernelName = "softmax_channel";
|
|
|
|
mLocalWorkSize = localWS3DDefault(mGlobalWorkSize, mMaxWorkGroupSize, mOpenCLBackend->getOpenCLRuntime(), kernelName, mKernel).first;
|
|
|
|
|
2020-11-05 10:14:01 +08:00
|
|
|
} else if (mAxis == 2){
|
2019-04-17 10:49:11 +08:00
|
|
|
if (mMaxWorkGroupSize > 256) {
|
|
|
|
mLocalWorkSize = {16, 16, 1};
|
|
|
|
} else {
|
|
|
|
mLocalWorkSize = {8, 8, 1};
|
|
|
|
}
|
|
|
|
mGlobalWorkSize = {(uint32_t)channelBlocks*outputWidth, (uint32_t)outputBatch, 1};
|
|
|
|
int shape[] = {outputBatch, channelBlocks, outputHeight, outputWidth};
|
2023-07-31 14:24:48 +08:00
|
|
|
ret |= mKernel.setArg(0, openCLImage(input));
|
|
|
|
ret |= mKernel.setArg(1, openCLImage(output));
|
|
|
|
ret |= mKernel.setArg(2, shape);
|
|
|
|
MNN_CHECK_CL_SUCCESS(ret, "setArg SoftmaxExecution Axis_2");
|
2020-11-05 10:14:01 +08:00
|
|
|
} else {
|
|
|
|
MNN_ASSERT(mAxis == 3);
|
|
|
|
if (mMaxWorkGroupSize > 256) {
|
|
|
|
mLocalWorkSize = {16, 16, 1};
|
|
|
|
} else {
|
|
|
|
mLocalWorkSize = {8, 8, 1};
|
|
|
|
}
|
|
|
|
mGlobalWorkSize = {(uint32_t)channelBlocks, (uint32_t)outputBatch*outputHeight, 1};
|
|
|
|
int shape[] = {outputBatch, channelBlocks, outputHeight, outputWidth};
|
2023-07-31 14:24:48 +08:00
|
|
|
ret |= mKernel.setArg(0, openCLImage(input));
|
|
|
|
ret |= mKernel.setArg(1, openCLImage(output));
|
|
|
|
ret |= mKernel.setArg(2, shape);
|
|
|
|
MNN_CHECK_CL_SUCCESS(ret, "setArg SoftmaxExecution Axis_3");
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
2023-06-16 09:42:45 +08:00
|
|
|
recordKernel3d(mKernel, mGlobalWorkSize, mLocalWorkSize, mOpenCLBackend->getOpenCLRuntime());
|
|
|
|
endRecord(mOpenCLBackend->getOpenCLRuntime(), mRecording);
|
2019-04-17 10:49:11 +08:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorCode SoftmaxExecution::onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
|
|
|
|
#ifdef LOG_VERBOSE
|
|
|
|
MNN_PRINT("start SoftmaxExecution onExecute !\n");
|
|
|
|
#endif
|
2020-05-28 19:04:27 +08:00
|
|
|
|
|
|
|
#ifdef ENABLE_OPENCL_TIME_PROFILER
|
|
|
|
cl::Event event;
|
|
|
|
run3DKernelDefault(mKernel, mGlobalWorkSize, mLocalWorkSize,
|
|
|
|
mOpenCLBackend->getOpenCLRuntime(), &event);
|
|
|
|
|
|
|
|
int costTime = (int)mOpenCLBackend->getOpenCLRuntime()->getCostTime(&event);
|
|
|
|
MNN_PRINT("kernel cost:%d us Softmax\n",costTime);
|
|
|
|
#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 SoftmaxExecution onExecute... \n");
|
|
|
|
#endif
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
2019-04-17 10:49:11 +08:00
|
|
|
run3DKernelDefault(mKernel, mGlobalWorkSize, mLocalWorkSize, mOpenCLBackend->getOpenCLRuntime());
|
2020-05-28 19:04:27 +08:00
|
|
|
#endif
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
#ifdef LOG_VERBOSE
|
|
|
|
MNN_PRINT("end SoftmaxExecution onExecute !\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
class SoftmaxCreator : 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 {
|
2019-09-01 19:25:26 +08:00
|
|
|
if(inputs[0]->dimensions() == 3 || outputs[0]->dimensions() == 3){
|
|
|
|
MNN_PRINT("softmax not support dimensions == 3 \n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-12-27 22:16:57 +08:00
|
|
|
auto dimType = inputs[0]->getDimensionType();
|
|
|
|
if (dimType == Tensor::TENSORFLOW && inputs[0]->dimensions() == 4) {
|
|
|
|
int index[4] = {0, 2, 3, 1};
|
|
|
|
auto axis = op->main_as_Axis()->axis();
|
|
|
|
if (axis < 0) {
|
|
|
|
axis = inputs[0]->dimensions() + axis;
|
|
|
|
}
|
|
|
|
|
|
|
|
axis = index[axis];
|
|
|
|
//1 : channel //2 : height
|
2020-11-05 10:14:01 +08:00
|
|
|
if (1 == axis || 2 == axis || 3 == axis) {
|
2019-12-27 22:16:57 +08:00
|
|
|
return new SoftmaxExecution(inputs, axis, backend);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
auto axis = op->main_as_Axis()->axis();
|
|
|
|
if (axis < 0) {
|
|
|
|
axis = inputs[0]->dimensions() + axis;
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:14:01 +08:00
|
|
|
if (1 == axis || 2 == axis || 3 == axis) {
|
2019-12-27 22:16:57 +08:00
|
|
|
return new SoftmaxExecution(inputs, axis, backend);
|
|
|
|
}
|
|
|
|
return nullptr;
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-03-12 18:41:50 +08:00
|
|
|
OpenCLCreatorRegister<SoftmaxCreator> __Softmax_op(OpType_Softmax, IMAGE);
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
} // namespace OpenCL
|
|
|
|
} // namespace MNN
|