2019-04-17 10:49:11 +08:00
|
|
|
//
|
2021-03-12 18:41:50 +08:00
|
|
|
// UnaryBufExecution.cpp
|
2019-04-17 10:49:11 +08:00
|
|
|
// MNN
|
|
|
|
//
|
|
|
|
// Created by MNN on 2019/02/28.
|
|
|
|
// Copyright © 2018, Alibaba Group Holding Limited
|
|
|
|
//
|
|
|
|
|
2021-03-12 18:41:50 +08:00
|
|
|
#ifndef MNN_OPENCL_BUFFER_CLOSED
|
|
|
|
#include "backend/opencl/execution/buffer/UnaryBufExecution.hpp"
|
2019-12-27 22:16:57 +08:00
|
|
|
#include "core/Macro.h"
|
|
|
|
#include "core/TensorUtils.hpp"
|
|
|
|
#include "backend/opencl/core/OpenCLBackend.hpp"
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
namespace MNN {
|
|
|
|
namespace OpenCL {
|
|
|
|
|
2021-03-12 18:41:50 +08:00
|
|
|
UnaryBufExecution::UnaryBufExecution(const std::string& compute, Backend* backend) : Execution(backend) {
|
2019-04-17 10:49:11 +08:00
|
|
|
auto openCLBackend = static_cast<OpenCLBackend*>(backend);
|
|
|
|
std::set<std::string> buildOptions;
|
|
|
|
buildOptions.emplace(" -DOPERATOR=" + compute);
|
|
|
|
// FUNC_PRINT_ALL(buildOptions.begin()->c_str(), s);
|
|
|
|
auto runtime = openCLBackend->getOpenCLRuntime();
|
2021-03-12 18:41:50 +08:00
|
|
|
mKernel = runtime->buildKernel("unary_buf", "unary_buf", buildOptions);
|
2019-04-17 10:49:11 +08:00
|
|
|
mMaxWorkGroupSize = static_cast<uint32_t>(runtime->getMaxWorkGroupSize(mKernel));
|
|
|
|
}
|
2021-03-12 18:41:50 +08:00
|
|
|
ErrorCode UnaryBufExecution::onResize(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs) {
|
2019-04-17 10:49:11 +08:00
|
|
|
Tensor* input = inputs[0];
|
|
|
|
Tensor* output = outputs[0];
|
|
|
|
auto openCLBackend = static_cast<OpenCLBackend*>(backend());
|
|
|
|
|
|
|
|
std::vector<int> inputShape = tensorShapeFormat(input);
|
|
|
|
std::vector<int> outputShape = tensorShapeFormat(output);
|
|
|
|
|
2019-12-27 22:16:57 +08:00
|
|
|
int batch = outputShape.at(0);
|
|
|
|
int outputHeight = outputShape.at(1);
|
|
|
|
int outputWidth = outputShape.at(2);
|
|
|
|
int channels = outputShape.at(3);
|
2019-04-17 10:49:11 +08:00
|
|
|
|
2019-12-27 22:16:57 +08:00
|
|
|
int channelBlocks = (channels + 3) / 4;
|
2019-04-17 10:49:11 +08:00
|
|
|
|
2019-12-27 22:16:57 +08:00
|
|
|
mGlobalWorkSize = {
|
|
|
|
static_cast<uint32_t>(channelBlocks),
|
|
|
|
static_cast<uint32_t>(outputWidth),
|
|
|
|
static_cast<uint32_t>(batch * outputHeight),
|
|
|
|
};
|
2019-04-17 10:49:11 +08:00
|
|
|
|
2019-12-27 22:16:57 +08:00
|
|
|
uint32_t idx = 0;
|
|
|
|
mKernel.setArg(idx++, mGlobalWorkSize[0]);
|
|
|
|
mKernel.setArg(idx++, mGlobalWorkSize[1]);
|
|
|
|
mKernel.setArg(idx++, mGlobalWorkSize[2]);
|
2021-03-12 18:41:50 +08:00
|
|
|
mKernel.setArg(idx++, openCLBuffer(input));
|
|
|
|
mKernel.setArg(idx++, openCLBuffer(output));
|
|
|
|
mKernel.setArg(idx++, outputHeight);
|
2019-04-17 10:49:11 +08:00
|
|
|
|
2021-03-12 18:41:50 +08:00
|
|
|
std::string kernelName = "unary_buf";
|
|
|
|
mLocalSize =
|
|
|
|
localWS3DDefault(mGlobalWorkSize, mMaxWorkGroupSize, openCLBackend->getOpenCLRuntime(), kernelName, mKernel).first;
|
2019-12-27 22:16:57 +08:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2021-03-12 18:41:50 +08:00
|
|
|
ErrorCode UnaryBufExecution::onExecute(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs) {
|
2019-12-27 22:16:57 +08:00
|
|
|
#ifdef LOG_VERBOSE
|
2021-03-12 18:41:50 +08:00
|
|
|
MNN_PRINT("start UnaryBufExecution onExecute...");
|
2019-12-27 22:16:57 +08:00
|
|
|
#endif
|
2020-05-28 19:04:27 +08:00
|
|
|
auto mOpenCLBackend = static_cast<OpenCLBackend*>(backend());
|
|
|
|
|
|
|
|
#ifdef ENABLE_OPENCL_TIME_PROFILER
|
|
|
|
cl::Event event;
|
|
|
|
run3DKernelDefault(mKernel, mGlobalWorkSize, mLocalSize,
|
|
|
|
mOpenCLBackend->getOpenCLRuntime(), &event);
|
|
|
|
|
|
|
|
int costTime = (int)mOpenCLBackend->getOpenCLRuntime()->getCostTime(&event);
|
|
|
|
MNN_PRINT("kernel cost:%d us Unary\n",costTime);
|
|
|
|
#else
|
|
|
|
run3DKernelDefault(mKernel, mGlobalWorkSize, mLocalSize,
|
|
|
|
mOpenCLBackend->getOpenCLRuntime());
|
|
|
|
#endif
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
#ifdef LOG_VERBOSE
|
2021-03-12 18:41:50 +08:00
|
|
|
MNN_PRINT("end UnaryBufExecution onExecute...");
|
2019-04-17 10:49:11 +08:00
|
|
|
#endif
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
2019-12-27 22:16:57 +08:00
|
|
|
|
2021-03-12 18:41:50 +08:00
|
|
|
class UnaryBufCreator : public OpenCLBackend::Creator {
|
2019-04-17 10:49:11 +08:00
|
|
|
public:
|
|
|
|
virtual Execution* onCreate(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs,
|
|
|
|
const MNN::Op* op, Backend* backend) const override {
|
|
|
|
if (op->type() == OpType_UnaryOp) {
|
|
|
|
switch (op->main_as_UnaryOp()->opType()) {
|
2021-04-08 15:34:23 +08:00
|
|
|
case UnaryOpOperation_ABS:
|
|
|
|
return new UnaryBufExecution("fabs(convert_float4(in))", backend);
|
2020-02-21 18:42:39 +08:00
|
|
|
case UnaryOpOperation_SQUARE:
|
2021-03-12 18:41:50 +08:00
|
|
|
return new UnaryBufExecution("in*in", backend);
|
2019-04-17 10:49:11 +08:00
|
|
|
case UnaryOpOperation_RSQRT:
|
2021-03-12 18:41:50 +08:00
|
|
|
return new UnaryBufExecution("rsqrt(convert_float4(in))", backend);
|
2020-02-21 17:59:28 +08:00
|
|
|
case UnaryOpOperation_NEG:
|
2021-03-12 18:41:50 +08:00
|
|
|
return new UnaryBufExecution("-(in)", backend);
|
2021-04-08 15:34:23 +08:00
|
|
|
case UnaryOpOperation_EXP:
|
|
|
|
return new UnaryBufExecution("exp(convert_float4(in))", backend);
|
|
|
|
case UnaryOpOperation_COS:
|
|
|
|
return new UnaryBufExecution("cos(convert_float4(in))", backend);
|
|
|
|
case UnaryOpOperation_SIN:
|
|
|
|
return new UnaryBufExecution("sin(convert_float4(in))", backend);
|
2020-02-21 17:59:28 +08:00
|
|
|
case UnaryOpOperation_TAN:
|
2021-03-12 18:41:50 +08:00
|
|
|
return new UnaryBufExecution("tan(convert_float4(in))", backend);
|
2021-04-08 15:34:23 +08:00
|
|
|
case UnaryOpOperation_ATAN:
|
|
|
|
return new UnaryBufExecution("atan(convert_float4(in))", backend);
|
|
|
|
case UnaryOpOperation_SQRT:
|
|
|
|
return new UnaryBufExecution("sqrt(convert_float4(in))", backend);
|
2020-02-21 17:59:28 +08:00
|
|
|
case UnaryOpOperation_CEIL:
|
2021-03-12 18:41:50 +08:00
|
|
|
return new UnaryBufExecution("ceil(convert_float4(in))", backend);
|
2021-04-08 15:34:23 +08:00
|
|
|
case UnaryOpOperation_RECIPROCAL:
|
|
|
|
return new UnaryBufExecution("native_recip(convert_float4(in))", backend);
|
2020-02-21 18:42:39 +08:00
|
|
|
case UnaryOpOperation_LOG1P:
|
2021-03-12 18:41:50 +08:00
|
|
|
return new UnaryBufExecution("log1p(convert_float4(in))", backend);
|
2021-04-08 15:34:23 +08:00
|
|
|
case UnaryOpOperation_LOG:
|
|
|
|
return new UnaryBufExecution("native_log(convert_float4(in)>(float4)(0.0000001)?convert_float4(in):(float4)(0.0000001))", backend);
|
2020-02-21 17:59:28 +08:00
|
|
|
case UnaryOpOperation_FLOOR:
|
2021-03-12 18:41:50 +08:00
|
|
|
return new UnaryBufExecution("floor(convert_float4(in))", backend);
|
2021-04-08 15:34:23 +08:00
|
|
|
case UnaryOpOperation_BNLL:
|
|
|
|
return new UnaryBufExecution("in>(FLOAT4)((FLOAT)0)?(in+native_log(exp(convert_float4(-(in)))+(float4)(1.0))):(native_log(exp(convert_float4(in))+(float4)(1.0)))", backend);
|
|
|
|
case UnaryOpOperation_ACOSH:
|
|
|
|
return new UnaryBufExecution("acosh(convert_float4(in))", backend);
|
|
|
|
case UnaryOpOperation_SINH:
|
|
|
|
return new UnaryBufExecution("sinh(convert_float4(in))", backend);
|
|
|
|
case UnaryOpOperation_ASINH:
|
|
|
|
return new UnaryBufExecution("asinh(convert_float4(in))", backend);
|
|
|
|
case UnaryOpOperation_ATANH:
|
|
|
|
return new UnaryBufExecution("atanh(convert_float4(in))", backend);
|
|
|
|
case UnaryOpOperation_SIGN:
|
|
|
|
return new UnaryBufExecution("sign(convert_float4(in))", backend);
|
2020-02-21 17:59:28 +08:00
|
|
|
case UnaryOpOperation_ROUND:
|
2021-03-12 18:41:50 +08:00
|
|
|
return new UnaryBufExecution("round(convert_float4(in))", backend);
|
2021-04-08 15:34:23 +08:00
|
|
|
case UnaryOpOperation_COSH:
|
|
|
|
return new UnaryBufExecution("cosh(convert_float4(in))", backend);
|
|
|
|
case UnaryOpOperation_ERF:
|
|
|
|
return new UnaryBufExecution("erf(convert_float4(in))", backend);
|
|
|
|
case UnaryOpOperation_ERFC:
|
|
|
|
return new UnaryBufExecution("erfc(convert_float4(in))", backend);
|
|
|
|
case UnaryOpOperation_EXPM1:
|
|
|
|
return new UnaryBufExecution("expm1(convert_float4(in))", backend);
|
2020-11-05 16:41:56 +08:00
|
|
|
case UnaryOpOperation_SIGMOID:
|
2021-03-12 18:41:50 +08:00
|
|
|
return new UnaryBufExecution("native_recip((float4)1+native_exp(convert_float4(-in)))", backend);
|
2020-11-05 16:41:56 +08:00
|
|
|
case UnaryOpOperation_TANH:
|
2021-03-12 18:41:50 +08:00
|
|
|
return new UnaryBufExecution("tanh(convert_float4(in))", backend);
|
2021-04-08 15:34:23 +08:00
|
|
|
case UnaryOpOperation_HARDSWISH:
|
2021-04-21 12:09:14 +08:00
|
|
|
return new UnaryBufExecution("convert_float4(in)>(float4)(-3.0f)?(convert_float4(in)<(float4)(3.0f)?((convert_float4(in)*(convert_float4(in)+(float4)3.0f))/(float4)6.0f):convert_float4(in)):(float4)(0.0f)", backend);
|
|
|
|
default:
|
2019-04-17 10:49:11 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (op->type() == OpType_Sigmoid) {
|
2021-04-08 15:34:23 +08:00
|
|
|
return new UnaryBufExecution("native_recip((float4)(1.0)+native_exp(convert_float4(-(in))))", backend);
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
if (op->type() == OpType_TanH) {
|
2021-03-12 18:41:50 +08:00
|
|
|
return new UnaryBufExecution("tanh(convert_float4(in))", backend);
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-12 18:41:50 +08:00
|
|
|
OpenCLCreatorRegister<UnaryBufCreator> __UnaryBuf__(OpType_UnaryOp, BUFFER);
|
|
|
|
OpenCLCreatorRegister<UnaryBufCreator> __SigmoidBuf__(OpType_Sigmoid, BUFFER);
|
|
|
|
OpenCLCreatorRegister<UnaryBufCreator> __TanhBuf__(OpType_TanH, BUFFER);
|
2019-04-17 10:49:11 +08:00
|
|
|
} // namespace OpenCL
|
|
|
|
} // namespace MNN
|
2021-03-12 18:41:50 +08:00
|
|
|
#endif /* MNN_OPENCL_BUFFER_CLOSED */
|