mirror of https://github.com/alibaba/MNN.git
198 lines
7.4 KiB
C++
198 lines
7.4 KiB
C++
//
|
|
// SoftmaxBufExecution.cpp
|
|
// MNN
|
|
//
|
|
// Created by MNN on 2019/02/28.
|
|
// Copyright © 2018, Alibaba Group Holding Limited
|
|
//
|
|
|
|
#ifndef MNN_OPENCL_BUFFER_CLOSED
|
|
|
|
#include "backend/opencl/execution/buffer/SoftmaxBufExecution.hpp"
|
|
#include "core/Macro.h"
|
|
#include "backend/opencl/core/OpenCLRunningUtils.hpp"
|
|
|
|
namespace MNN {
|
|
namespace OpenCL {
|
|
|
|
SoftmaxBufExecution::SoftmaxBufExecution(const std::vector<Tensor *> &inputs, int axis, Backend *backend)
|
|
: Execution(backend) {
|
|
mAxis = axis;
|
|
mOpenCLBackend = static_cast<OpenCLBackend *>(backend);
|
|
}
|
|
|
|
bool SoftmaxBufExecution::buildSoftmaxKernel() {
|
|
auto runtime = mOpenCLBackend->getOpenCLRuntime();
|
|
if (mKernel.get() == nullptr) {
|
|
std::set<std::string> buildOptions;
|
|
std::string kernelName;
|
|
if (mAxis == 1) {
|
|
mKernel = runtime->buildKernel("softmax_buf", "softmax_channel", buildOptions);
|
|
} else if (mAxis == 2) {
|
|
mKernel = runtime->buildKernel("softmax_buf", "softmax_height", buildOptions);
|
|
} else {
|
|
MNN_ASSERT(mAxis == 3);
|
|
mKernel = runtime->buildKernel("softmax_buf", "softmax_width", buildOptions);
|
|
}
|
|
mMaxWorkGroupSize = static_cast<uint32_t>(runtime->getMaxWorkGroupSize(mKernel));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
ErrorCode SoftmaxBufExecution::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
|
|
Tensor *input = inputs[0];
|
|
Tensor *output = outputs[0];
|
|
|
|
const auto dims = input->buffer().dimensions;
|
|
int inside = 1;
|
|
int outside = 1;
|
|
int channel = 1;
|
|
for (int i = 0; i < mAxis; ++i) {
|
|
outside *= input->length(i);
|
|
}
|
|
channel = input->length(mAxis);
|
|
for (int i = mAxis + 1; i < dims; ++i) {
|
|
inside *= input->length(i);
|
|
}
|
|
|
|
std::vector<int> inputShape = tensorShapeFormat(input);
|
|
std::vector<int> outputShape = tensorShapeFormat(output);
|
|
|
|
const int inputBatch = inputShape.at(0);
|
|
const int inputHeight = inputShape.at(1);
|
|
const int inputWidth = inputShape.at(2);
|
|
const int inputChannels = inputShape.at(3);
|
|
|
|
const int outputBatch = outputShape.at(0);
|
|
const int outputHeight = outputShape.at(1);
|
|
const int outputWidth = outputShape.at(2);
|
|
const int outputChannels = outputShape.at(3);
|
|
|
|
const int channelBlocks = UP_DIV(outputChannels, 4);
|
|
const int remainChannels = channelBlocks * 4 - outputChannels;
|
|
if(inputBatch == outside && channel == inputChannels && inside == inputWidth * inputHeight){
|
|
mAxis = 1;
|
|
}else if(inputBatch * inputChannels == outside && channel == inputHeight && inside == inputHeight){
|
|
mAxis = 2;
|
|
}else if(inputBatch * inputChannels * inputHeight == outside && channel == inputWidth && inside == 1){
|
|
mAxis = 3;
|
|
}
|
|
buildSoftmaxKernel();
|
|
|
|
if (mAxis == 1) {
|
|
mGlobalWorkSize = {static_cast<uint32_t>(outputWidth),
|
|
static_cast<uint32_t>(outputHeight * outputBatch), 1};
|
|
int shape[] = {outputBatch, channelBlocks, outputHeight, outputWidth};
|
|
|
|
uint32_t idx = 0;
|
|
cl_int ret = CL_SUCCESS;
|
|
ret |= mKernel.setArg(idx++, mGlobalWorkSize[0]);
|
|
ret |= mKernel.setArg(idx++, mGlobalWorkSize[1]);
|
|
ret |= mKernel.setArg(idx++, mGlobalWorkSize[2]);
|
|
|
|
ret |= mKernel.setArg(idx++, openCLBuffer(input));
|
|
ret |= mKernel.setArg(idx++, openCLBuffer(output));
|
|
ret |= mKernel.setArg(idx++, static_cast<int>(outputChannels));
|
|
ret |= mKernel.setArg(idx++, remainChannels);
|
|
ret |= mKernel.setArg(idx++, shape);
|
|
MNN_CHECK_CL_SUCCESS(ret, "setArg SoftmaxBufExecution axis_1");
|
|
|
|
std::string kernelName = "softmax_buf_channel";
|
|
mLocalWorkSize =
|
|
localWS3DDefault(mGlobalWorkSize, mMaxWorkGroupSize, mOpenCLBackend->getOpenCLRuntime(), kernelName, mKernel).first;
|
|
} else if (mAxis == 2){
|
|
mGlobalWorkSize = {(uint32_t)channelBlocks*outputWidth, (uint32_t)outputBatch, 1};
|
|
int shape[] = {outputBatch, channelBlocks, outputHeight, outputWidth};
|
|
cl_int ret = CL_SUCCESS;
|
|
ret |= mKernel.setArg(0, openCLBuffer(input));
|
|
ret |= mKernel.setArg(1, openCLBuffer(output));
|
|
ret |= mKernel.setArg(2, shape);
|
|
MNN_CHECK_CL_SUCCESS(ret, "setArg SoftmaxBufExecution axis_2");
|
|
|
|
std::string kernelName = "softmax_buf_height";
|
|
mLocalWorkSize =
|
|
localWS3DDefault(mGlobalWorkSize, mMaxWorkGroupSize, mOpenCLBackend->getOpenCLRuntime(), kernelName, mKernel).first;
|
|
} else {
|
|
MNN_ASSERT(mAxis == 3);
|
|
mGlobalWorkSize = {(uint32_t)channelBlocks, (uint32_t)outputBatch*outputHeight, 1};
|
|
int shape[] = {outputBatch, channelBlocks, outputHeight, outputWidth};
|
|
cl_int ret = CL_SUCCESS;
|
|
ret |= mKernel.setArg(0, openCLBuffer(input));
|
|
ret |= mKernel.setArg(1, openCLBuffer(output));
|
|
ret |= mKernel.setArg(2, shape);
|
|
MNN_CHECK_CL_SUCCESS(ret, "setArg SoftmaxBufExecution axis_3");
|
|
|
|
std::string kernelName = "softmax_buf_width";
|
|
mLocalWorkSize =
|
|
localWS3DDefault(mGlobalWorkSize, mMaxWorkGroupSize, mOpenCLBackend->getOpenCLRuntime(), kernelName, mKernel).first;
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
ErrorCode SoftmaxBufExecution::onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
|
|
#ifdef LOG_VERBOSE
|
|
MNN_PRINT("start SoftmaxBufExecution onExecute !\n");
|
|
#endif
|
|
|
|
#ifdef ENABLE_OPENCL_TIME_PROFILER
|
|
cl::Event event;
|
|
run3DKernelDefault(mKernel, mGlobalWorkSize, mLocalWorkSize,
|
|
mOpenCLBackend->getOpenCLRuntime(), &event);
|
|
|
|
mOpenCLBackend->getOpenCLRuntime()->pushEvent({"Softmax", event});
|
|
#else
|
|
run3DKernelDefault(mKernel, mGlobalWorkSize, mLocalWorkSize, mOpenCLBackend->getOpenCLRuntime());
|
|
#endif
|
|
|
|
#ifdef LOG_VERBOSE
|
|
MNN_PRINT("end SoftmaxBufExecution onExecute !\n");
|
|
#endif
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
class SoftmaxBufCreator : 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 {
|
|
for (int i = 0; i < inputs.size(); ++i) {
|
|
TensorUtils::setTensorSupportPack(inputs[i], false);
|
|
}
|
|
for (int i = 0; i < outputs.size(); ++i) {
|
|
TensorUtils::setTensorSupportPack(outputs[i], false);
|
|
}
|
|
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
|
|
if (1 == axis || 2 == axis || 3 == axis) {
|
|
return new SoftmaxBufExecution(inputs, axis, backend);
|
|
}
|
|
return nullptr;
|
|
} else {
|
|
auto axis = op->main_as_Axis()->axis();
|
|
if (axis < 0) {
|
|
axis = inputs[0]->dimensions() + axis;
|
|
}
|
|
|
|
if (1 == axis || 2 == axis || 3 == axis) {
|
|
return new SoftmaxBufExecution(inputs, axis, backend);
|
|
}
|
|
return nullptr;
|
|
}
|
|
}
|
|
};
|
|
OpenCLCreatorRegister<SoftmaxBufCreator> __SoftmaxBuf_op(OpType_Softmax, BUFFER);
|
|
|
|
} // namespace OpenCL
|
|
} // namespace MNN
|
|
#endif/* MNN_OPENCL_BUFFER_CLOSED */
|
|
|