2019-04-17 10:49:11 +08:00
|
|
|
//
|
|
|
|
// VulkanUnary.cpp
|
|
|
|
// MNN
|
|
|
|
//
|
|
|
|
// Created by MNN on 2019/01/31.
|
|
|
|
// Copyright © 2018, Alibaba Group Holding Limited
|
|
|
|
//
|
|
|
|
|
2019-12-27 22:16:57 +08:00
|
|
|
#include "backend/vulkan/execution/VulkanUnary.hpp"
|
|
|
|
#include "core/Macro.h"
|
|
|
|
#include "core/TensorUtils.hpp"
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
namespace MNN {
|
|
|
|
|
|
|
|
struct Param {
|
2020-03-08 08:57:33 +08:00
|
|
|
ivec4 size;
|
|
|
|
ivec4 stride;
|
2019-04-17 10:49:11 +08:00
|
|
|
};
|
|
|
|
|
2020-03-08 11:38:28 +08:00
|
|
|
VulkanUnary::VulkanUnary(const std::string& midType, Backend* bn, bool image) : VulkanBasicExecution(bn) {
|
2019-04-17 10:49:11 +08:00
|
|
|
auto vkbackend = static_cast<VulkanBackend*>(bn);
|
|
|
|
mParam = std::make_shared<VulkanBuffer>(vkbackend->getMemoryPool(), false, sizeof(Param), nullptr,
|
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
|
2020-03-08 08:57:33 +08:00
|
|
|
std::string prefix = "glsl_unaryBuffer_";
|
2019-04-17 10:49:11 +08:00
|
|
|
std::vector<VkDescriptorType> types{
|
|
|
|
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
|
|
|
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
|
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
|
|
};
|
2020-03-08 11:38:28 +08:00
|
|
|
if (image) {
|
|
|
|
prefix = "glsl_unaryImage_";
|
|
|
|
types = {
|
|
|
|
VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
|
|
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
std::string posfix = "_comp";
|
|
|
|
// get pipeline
|
2020-03-08 08:57:33 +08:00
|
|
|
mUnaryPipeline = vkbackend->getPipeline(prefix + midType + posfix, types);
|
2020-03-08 11:38:28 +08:00
|
|
|
mDesSet.reset(mUnaryPipeline->createSet());
|
2020-03-08 08:57:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VulkanUnary::~VulkanUnary() {
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string _getMidType(const Op* op) {
|
|
|
|
std::string midType = "";
|
|
|
|
if (op->type() == OpType_TanH) {
|
|
|
|
midType = "TANH";
|
2020-03-08 11:38:28 +08:00
|
|
|
}
|
|
|
|
else if (op->type() == OpType_Sigmoid) {
|
|
|
|
midType = "SIGMOID";
|
2019-04-17 10:49:11 +08:00
|
|
|
} else {
|
|
|
|
// unary op
|
2020-03-08 08:57:33 +08:00
|
|
|
auto unaryType = op->main_as_UnaryOp()->opType();
|
|
|
|
#define SETTYPE(type, name) if (unaryType == type) {midType = name; break;}
|
|
|
|
do {
|
|
|
|
SETTYPE(UnaryOpOperation_RSQRT, "RSQRT");
|
|
|
|
SETTYPE(UnaryOpOperation_SIGN, "SIGN");
|
|
|
|
SETTYPE(UnaryOpOperation_ABS, "ABS");
|
|
|
|
SETTYPE(UnaryOpOperation_NEG, "NEG");
|
|
|
|
SETTYPE(UnaryOpOperation_EXP, "EXP");
|
|
|
|
SETTYPE(UnaryOpOperation_SQRT, "SQRT");
|
|
|
|
SETTYPE(UnaryOpOperation_SQUARE, "SQUARE");
|
|
|
|
SETTYPE(UnaryOpOperation_LOG, "LOG");
|
|
|
|
} while(false);
|
|
|
|
#undef SETTYPE
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
2020-03-08 08:57:33 +08:00
|
|
|
return midType;
|
|
|
|
}
|
2019-04-17 10:49:11 +08:00
|
|
|
|
2020-03-08 08:57:33 +08:00
|
|
|
ErrorCode VulkanUnary::onEncode(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs,
|
|
|
|
const VulkanCommandPool::Buffer* cmdBuffer) {
|
2019-04-17 10:49:11 +08:00
|
|
|
// set param
|
2020-03-08 08:57:33 +08:00
|
|
|
auto size = inputs[0]->elementSize();
|
|
|
|
auto sizeC4 = UP_DIV(size, 4);
|
2020-03-08 11:38:28 +08:00
|
|
|
bool image = MNN_DATA_FORMAT_NC4HW4 == TensorUtils::getDescribe(inputs[0])->dimensionFormat;
|
2019-04-17 10:49:11 +08:00
|
|
|
auto paramPtr = reinterpret_cast<Param*>(mParam->map());
|
2020-03-08 08:57:33 +08:00
|
|
|
paramPtr->size[0] = sizeC4;
|
2020-03-08 11:38:28 +08:00
|
|
|
if (image) {
|
|
|
|
paramPtr->size[1] = inputs[0]->batch() * UP_DIV(inputs[0]->channel(), 4);
|
|
|
|
paramPtr->size[2] = inputs[0]->height();
|
|
|
|
paramPtr->size[3] = inputs[0]->width();
|
|
|
|
}
|
2019-04-17 10:49:11 +08:00
|
|
|
mParam->unmap();
|
2020-03-08 11:38:28 +08:00
|
|
|
if (image) {
|
|
|
|
auto vkBn = (VulkanBackend*)backend();
|
|
|
|
auto inputTensor = vkBn->findTensor(inputs[0]->deviceId());
|
|
|
|
cmdBuffer->barrierImage(inputTensor->image()->get(), VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
|
|
|
mDesSet->writeImage((VkImageView)(outputs[0])->deviceId(), vkBn->getCommonSampler()->get(), VK_IMAGE_LAYOUT_GENERAL, 0);
|
|
|
|
mDesSet->writeImage((VkImageView)(inputs[0])->deviceId(), vkBn->getCommonSampler()->get(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 1);
|
|
|
|
mDesSet->writeBuffer(mParam->buffer(), 2, mParam->size());
|
|
|
|
} else {
|
|
|
|
cmdBuffer->barrierSource(reinterpret_cast<VkBuffer>(inputs[0]->deviceId()), 0, inputs[0]->size());
|
|
|
|
mDesSet->writeBuffer(reinterpret_cast<VkBuffer>(outputs[0]->deviceId()), 0, outputs[0]->size());
|
|
|
|
mDesSet->writeBuffer(reinterpret_cast<VkBuffer>(inputs[0]->deviceId()), 1, inputs[0]->size());
|
|
|
|
mDesSet->writeBuffer(mParam->buffer(), 2, mParam->size());
|
|
|
|
}
|
2019-04-17 10:49:11 +08:00
|
|
|
mUnaryPipeline->bind(cmdBuffer->get(), mDesSet->get());
|
2020-03-08 08:57:33 +08:00
|
|
|
vkCmdDispatch(cmdBuffer->get(), UP_DIV(sizeC4, 256), 1, 1);
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
class VulkanUnaryCreator : public VulkanBackend::Creator {
|
|
|
|
public:
|
- dynamic computation graph (beta)
- add supports (/express)
- add tests
- add benchmarks with it (/benchmark/exprModels)
- Python
- MNN engine and tools were submitted to pip
- available on Windows/macOS/Linux
- Engine/Converter
- add supports for each op benchmarking
- refactor optimizer by separating steps
- CPU
- add supports for Conv3D, Pool3D, ELU, ReverseSequence
- fix ArgMax, Permute, Scale, BinaryOp, Slice, SliceTf
- OpenCL
- add half transform in CPU
- add broadcast supports for binary
- optimize Conv2D, Reshape, Eltwise, Gemm, etc.
- OpenGL
- add sub, real div supports for binary
- add supports for unary
- optimize Conv2D, Reshape
- Vulkan
- add max supports for eltwise
- Metal
- fix metallib missing problem
- Train/Quantization
- use express to refactor training codes
2019-09-26 21:02:07 +08:00
|
|
|
virtual VulkanBasicExecution* onCreate(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs, const MNN::Op* op, Backend* bn) const override {
|
2020-03-08 11:38:28 +08:00
|
|
|
bool image = MNN_DATA_FORMAT_NC4HW4 == TensorUtils::getDescribe(inputs[0])->dimensionFormat;
|
2020-03-08 08:57:33 +08:00
|
|
|
if (inputs[0]->buffer().type.code != halide_type_float) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto midType = _getMidType(op);
|
|
|
|
if (midType.empty()) {
|
|
|
|
return nullptr;
|
2020-03-01 23:56:15 +08:00
|
|
|
}
|
2020-03-08 11:38:28 +08:00
|
|
|
return new VulkanUnary(midType, bn, image);
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool gResistor = []() {
|
|
|
|
VulkanBackend::addCreator(OpType_UnaryOp, new VulkanUnaryCreator);
|
|
|
|
VulkanBackend::addCreator(OpType_TanH, new VulkanUnaryCreator);
|
2020-03-08 11:38:28 +08:00
|
|
|
VulkanBackend::addCreator(OpType_Sigmoid, new VulkanUnaryCreator);
|
2019-04-17 10:49:11 +08:00
|
|
|
return true;
|
|
|
|
}();
|
|
|
|
|
|
|
|
} // namespace MNN
|