2019-04-17 10:49:11 +08:00
|
|
|
//
|
|
|
|
// VulkanSoftmax.cpp
|
|
|
|
// MNN
|
|
|
|
//
|
|
|
|
// Created by MNN on 2019/01/31.
|
|
|
|
// Copyright © 2018, Alibaba Group Holding Limited
|
|
|
|
//
|
|
|
|
|
2022-12-30 15:18:58 +08:00
|
|
|
#include "VulkanSoftmax.hpp"
|
2019-12-27 22:16:57 +08:00
|
|
|
#include "core/Macro.h"
|
|
|
|
#include "core/TensorUtils.hpp"
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
namespace MNN {
|
|
|
|
|
2024-11-18 14:37:45 +08:00
|
|
|
struct SoftmaxConstBuffer {
|
|
|
|
uint N;
|
|
|
|
uint H;
|
|
|
|
uint W;
|
|
|
|
uint C4;
|
|
|
|
uint CLeft;
|
2019-04-17 10:49:11 +08:00
|
|
|
};
|
|
|
|
|
2024-11-18 14:37:45 +08:00
|
|
|
VulkanSoftmax::VulkanSoftmax(const Op* op, Backend* bn, const uint axisIndex) : VulkanBasicExecution(bn) {
|
|
|
|
mAxisIndex = axisIndex;
|
2020-11-05 16:41:56 +08:00
|
|
|
auto vkBn = (VulkanBackend*)backend();
|
2024-11-18 14:37:45 +08:00
|
|
|
std::string shaderName = "glsl_softmaxImage_";
|
|
|
|
std::string macro = "";
|
|
|
|
std::string suffix = "comp";
|
|
|
|
switch (axisIndex) {
|
|
|
|
case 0:
|
|
|
|
macro = "AXIS_N_"; break;
|
|
|
|
case 1:
|
|
|
|
macro = "AXIS_H_"; break;
|
|
|
|
case 2:
|
|
|
|
macro = "AXIS_W_"; break;
|
|
|
|
case 3:
|
|
|
|
macro = "AXIS_C_"; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<VkDescriptorType> types {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
|
|
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER};
|
|
|
|
|
|
|
|
mSoftmaxPipeline = vkBn->getPipeline(shaderName + macro + suffix, types);
|
2020-11-05 16:41:56 +08:00
|
|
|
mDescriptorSet.reset(mSoftmaxPipeline->createSet());
|
2024-11-18 14:37:45 +08:00
|
|
|
mSoftmaxConstBuffer = std::make_shared<VulkanBuffer>(vkBn->getMemoryPool(), false, sizeof(SoftmaxConstBuffer), nullptr, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VulkanSoftmax::~VulkanSoftmax() {
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorCode VulkanSoftmax::onEncode(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs,
|
|
|
|
const VulkanCommandPool::Buffer* cmdBuffer) {
|
2024-11-18 14:37:45 +08:00
|
|
|
auto vkBn = static_cast<VulkanBackend *>(backend());
|
2019-04-17 10:49:11 +08:00
|
|
|
auto input = inputs[0];
|
|
|
|
auto output = outputs[0];
|
2024-11-18 14:37:45 +08:00
|
|
|
auto inputShapeNHWC = VulkanTensor::tensorShapeFormat(input);
|
|
|
|
std::vector<uint> cpuSoftmaxConstBuffer = {(uint)inputShapeNHWC[0], (uint)inputShapeNHWC[1], (uint)inputShapeNHWC[2], (uint)UP_DIV(inputShapeNHWC[3], 4), (uint)ROUND_UP(inputShapeNHWC[3], 4) - inputShapeNHWC[3]};
|
2019-04-17 10:49:11 +08:00
|
|
|
|
2020-11-05 16:41:56 +08:00
|
|
|
{
|
2024-11-18 14:37:45 +08:00
|
|
|
auto softmaxConst = reinterpret_cast<SoftmaxConstBuffer*>(mSoftmaxConstBuffer->map());
|
|
|
|
::memset(softmaxConst, 0, sizeof(SoftmaxConstBuffer));
|
|
|
|
softmaxConst->N = cpuSoftmaxConstBuffer[0];
|
|
|
|
softmaxConst->H = cpuSoftmaxConstBuffer[1];
|
|
|
|
softmaxConst->W = cpuSoftmaxConstBuffer[2];
|
|
|
|
softmaxConst->C4 = cpuSoftmaxConstBuffer[3];
|
|
|
|
softmaxConst->CLeft = cpuSoftmaxConstBuffer[4];
|
|
|
|
mSoftmaxConstBuffer->unmap();
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
|
2024-11-18 14:37:45 +08:00
|
|
|
// N * H * W * C4
|
|
|
|
uint numTotal = cpuSoftmaxConstBuffer[0] * cpuSoftmaxConstBuffer[1] * cpuSoftmaxConstBuffer[2] * cpuSoftmaxConstBuffer[3];
|
|
|
|
uint numY = numTotal / cpuSoftmaxConstBuffer[mAxisIndex];
|
|
|
|
|
|
|
|
auto vkOutput = (VulkanTensor*)output->deviceId();
|
|
|
|
auto vkInput = (VulkanTensor*)input->deviceId();
|
|
|
|
|
|
|
|
mDescriptorSet.reset(mSoftmaxPipeline->createSet());
|
|
|
|
mDescriptorSet->writeImage(vkOutput->image()->view(), vkBn->getCommonSampler()->get(),
|
|
|
|
VK_IMAGE_LAYOUT_GENERAL, 0);
|
|
|
|
mDescriptorSet->writeImage(vkInput->image()->view(), vkBn->getCommonSampler()->get(),
|
|
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 1);
|
|
|
|
mDescriptorSet->writeBuffer(mSoftmaxConstBuffer->buffer(), 2, mSoftmaxConstBuffer->size());
|
|
|
|
|
|
|
|
vkOutput->image()->barrierWrite(cmdBuffer->get());
|
|
|
|
vkInput->image()->barrierRead(cmdBuffer->get());
|
2020-11-05 16:41:56 +08:00
|
|
|
|
|
|
|
mSoftmaxPipeline->bind(cmdBuffer->get(), mDescriptorSet->get());
|
2024-11-18 14:37:45 +08:00
|
|
|
|
|
|
|
vkCmdDispatch(cmdBuffer->get(), 1, numY, 1);
|
|
|
|
|
2019-04-17 10:49:11 +08:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
class VulkanSoftmaxCreator : 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,
|
2019-04-17 10:49:11 +08:00
|
|
|
Backend* backend) const override {
|
2024-11-18 14:37:45 +08:00
|
|
|
auto input = inputs[0];
|
|
|
|
|
|
|
|
uint dimension = input->dimensions();
|
|
|
|
if (dimension > 4) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Work out the reduce axis, taking various formats and dimensions into account.
|
|
|
|
MNN_DATA_FORMAT format = VulkanImageConverter::getTensorLinearFormat(input);
|
|
|
|
int axis = op->main_as_Axis()->axis();
|
|
|
|
if (axis < 0) {
|
|
|
|
axis = input->dimensions() + axis;
|
|
|
|
}
|
|
|
|
std::vector<uint> axisMap;
|
|
|
|
|
|
|
|
if (dimension == 4) {
|
|
|
|
if (format == MNN_DATA_FORMAT_NCHW) {
|
|
|
|
axisMap.assign({0, 3, 1, 2});
|
|
|
|
} else {
|
|
|
|
axisMap.assign({0, 1, 2, 3});
|
|
|
|
}
|
|
|
|
} else if (dimension == 3) {
|
|
|
|
if (format == MNN_DATA_FORMAT_NCHW) {
|
|
|
|
axisMap.assign({0, 3, 1});
|
|
|
|
} else {
|
|
|
|
axisMap.assign({0, 1, 3});
|
|
|
|
}
|
|
|
|
} else if (dimension == 2) {
|
|
|
|
axisMap.assign({0, 3});
|
|
|
|
} else if (dimension == 1) {
|
|
|
|
axisMap.assign({3});
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
uint axisIndex = axisMap[axis];
|
|
|
|
|
|
|
|
return new VulkanSoftmax(op, backend, axisIndex);
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool gResistor = []() {
|
|
|
|
VulkanBackend::addCreator(OpType_Softmax, new VulkanSoftmaxCreator);
|
|
|
|
return true;
|
|
|
|
}();
|
|
|
|
|
|
|
|
} // namespace MNN
|