2019-04-17 10:49:11 +08:00
|
|
|
//
|
|
|
|
// VulkanImage.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/component/VulkanImage.hpp"
|
2019-04-17 10:49:11 +08:00
|
|
|
#include <string.h>
|
|
|
|
namespace MNN {
|
|
|
|
VulkanSampler::VulkanSampler(const VulkanDevice& dev, VkFilter filter, VkSamplerAddressMode mode) : mDevice(dev) {
|
|
|
|
// Finally, create a sampler.
|
|
|
|
CALL_VK(mDevice.createSampler(mSampler, filter, mode));
|
|
|
|
}
|
|
|
|
VulkanSampler::~VulkanSampler() {
|
|
|
|
mDevice.destroySampler(mSampler);
|
|
|
|
}
|
|
|
|
void VulkanImage::release() {
|
2020-12-15 14:12:35 +08:00
|
|
|
if (nullptr == mMemory.first) {
|
2019-04-17 10:49:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const_cast<VulkanMemoryPool&>(mPool).returnMemory(mMemory);
|
2020-12-15 14:12:35 +08:00
|
|
|
mMemory.first = nullptr;
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static VkFormat _getFormat(halide_type_t type) {
|
|
|
|
switch (type.code) {
|
|
|
|
case halide_type_float:
|
2020-03-02 00:15:28 +08:00
|
|
|
return VK_FORMAT_R32G32B32A32_SFLOAT;
|
2019-04-17 10:49:11 +08:00
|
|
|
case halide_type_int: {
|
|
|
|
if (8 == type.bits) {
|
|
|
|
return VK_FORMAT_R8G8B8A8_SINT;
|
|
|
|
} else if (type.bits == 16) {
|
|
|
|
return VK_FORMAT_R16G16B16A16_SINT;
|
|
|
|
}
|
|
|
|
return VK_FORMAT_R32G32B32A32_SINT;
|
|
|
|
}
|
|
|
|
case halide_type_uint: {
|
|
|
|
if (8 == type.bits) {
|
|
|
|
return VK_FORMAT_R8G8B8A8_UINT;
|
|
|
|
} else if (type.bits == 16) {
|
|
|
|
return VK_FORMAT_R16G16B16A16_UINT;
|
|
|
|
}
|
|
|
|
return VK_FORMAT_R32G32B32A32_UINT;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-03-02 00:15:28 +08:00
|
|
|
return VK_FORMAT_R32G32B32A32_SFLOAT;
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VulkanImage::VulkanImage(const VulkanMemoryPool& pool, bool seperate, const std::vector<int>& dims, halide_type_t type)
|
- 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
|
|
|
: mDevice(pool.device()), mPool(pool) {
|
2019-04-17 10:49:11 +08:00
|
|
|
MNN_ASSERT(dims.size() >= 1 && dims.size() <= 3);
|
|
|
|
auto imageType = VK_IMAGE_TYPE_1D;
|
|
|
|
auto viewType = VK_IMAGE_VIEW_TYPE_1D;
|
|
|
|
mDims = dims;
|
2020-11-05 16:41:56 +08:00
|
|
|
auto mWidth = dims[0];
|
|
|
|
auto mHeight = 1;
|
|
|
|
auto mDepth = 1;
|
2019-04-17 10:49:11 +08:00
|
|
|
if (dims.size() > 1) {
|
|
|
|
mHeight = dims[1];
|
|
|
|
imageType = VK_IMAGE_TYPE_2D;
|
|
|
|
viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
|
|
}
|
|
|
|
if (dims.size() > 2) {
|
|
|
|
mDepth = dims[2];
|
|
|
|
imageType = VK_IMAGE_TYPE_3D;
|
|
|
|
viewType = VK_IMAGE_VIEW_TYPE_3D;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto format = _getFormat(type);
|
2020-03-02 00:15:28 +08:00
|
|
|
if (pool.permitFp16() && format == VK_FORMAT_R32G32B32A32_SFLOAT) {
|
|
|
|
// Use fp16 instead of fp32
|
|
|
|
format = VK_FORMAT_R16G16B16A16_SFLOAT;
|
|
|
|
}
|
2020-11-05 16:41:56 +08:00
|
|
|
auto mFormat = format;
|
|
|
|
mInfo = std::make_tuple(imageType, mWidth, mHeight, mDepth, mFormat);
|
2019-04-17 10:49:11 +08:00
|
|
|
// FUNC_PRINT(format);
|
2020-11-05 16:41:56 +08:00
|
|
|
mImage.first = const_cast<VulkanMemoryPool&>(mPool).allocImage(mInfo);
|
2020-08-11 17:44:19 +08:00
|
|
|
mLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
2019-04-17 10:49:11 +08:00
|
|
|
VkMemoryRequirements memRequirements;
|
2020-11-05 16:41:56 +08:00
|
|
|
mDevice.getImageMemoryRequirements(mImage.first, memRequirements);
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
mMemory = const_cast<VulkanMemoryPool&>(mPool).allocMemory(memRequirements, 0, seperate);
|
|
|
|
// FUNC_PRINT(mMemory->type());
|
2020-12-15 14:12:35 +08:00
|
|
|
auto realMem = (VulkanMemory*)mMemory.first;
|
|
|
|
mDevice.bindImageMemory(mImage.first, realMem->get(), mMemory.second);
|
2020-11-05 16:41:56 +08:00
|
|
|
CALL_VK(mDevice.createImageView(mImage.second, mImage.first, viewType, format));
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
VulkanImage::~VulkanImage() {
|
2020-11-05 16:41:56 +08:00
|
|
|
mDevice.destroyImageView(mImage.second, nullptr);
|
|
|
|
const_cast<VulkanMemoryPool&>(mPool).returnImage(std::move(mImage.first), std::move(mInfo));
|
2020-12-15 14:12:35 +08:00
|
|
|
if (nullptr != mMemory.first) {
|
2020-11-05 16:41:56 +08:00
|
|
|
const_cast<VulkanMemoryPool&>(mPool).returnMemory(mMemory);
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace MNN
|