forked from alibaba/MNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVulkanRelu.cpp
More file actions
196 lines (166 loc) · 8.63 KB
/
Copy pathVulkanRelu.cpp
File metadata and controls
196 lines (166 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// VulkanRelu.cpp
// MNN
//
// Created by MNN on 2019/01/31.
// Copyright © 2018, Alibaba Group Holding Limited
//
#include "VulkanRelu.hpp"
#include "Macro.h"
#include "TensorUtils.hpp"
namespace MNN {
struct GpuReluParam {
ivec4 imgSize;
float slope;
};
//--------------------------relu--------------------------//
VulkanRelu::VulkanRelu(Backend *bn, float slope) : VulkanBasicExecution(bn), mSlope(slope) {
std::vector<VkDescriptorType> types{VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER};
auto vulkanBn = static_cast<VulkanBackend *>(bn);
mReluPipeline = vulkanBn->getPipeline("glsl_relu_comp", /*glsl_relu_comp, glsl_relu_comp_len,*/ types);
mGpuReluParam.reset(new VulkanBuffer(vulkanBn->getMemoryPool(), false, sizeof(GpuReluParam), nullptr,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT));
}
VulkanRelu::~VulkanRelu() {
}
ErrorCode VulkanRelu::onEncode(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs,
const VulkanCommandPool::Buffer *cmdBuffer) {
auto input = inputs[0];
auto output = outputs[0];
auto reluParam = reinterpret_cast<GpuReluParam *>(mGpuReluParam->map());
::memset(reluParam, 0, sizeof(GpuReluParam));
const int channelDiv4 = UP_DIV(input->channel(), 4);
reluParam->imgSize[0] = input->width();
reluParam->imgSize[1] = input->height();
reluParam->imgSize[2] = channelDiv4 * input->batch();
reluParam->imgSize[3] = 0;
reluParam->slope = mSlope;
mGpuReluParam->flush(true, 0, sizeof(GpuReluParam));
mGpuReluParam->unmap();
auto vkBn = (VulkanBackend *)backend();
mDescriptorSet.reset(mReluPipeline->createSet());
mDescriptorSet->writeImage((VkImageView)output->deviceId(), vkBn->getCommonSampler()->get(),
VK_IMAGE_LAYOUT_GENERAL, 0);
mDescriptorSet->writeImage((VkImageView)input->deviceId(), vkBn->getCommonSampler()->get(),
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 1);
mDescriptorSet->writeBuffer(mGpuReluParam->buffer(), 2, mGpuReluParam->size());
mReluPipeline->bind(cmdBuffer->get(), mDescriptorSet->get());
vkCmdDispatch(cmdBuffer->get(), UP_DIV(input->width(), 16), UP_DIV(input->height(), 16),
channelDiv4 * input->batch());
return NO_ERROR;
}
//--------------------------relu6--------------------------//
VulkanRelu6::VulkanRelu6(Backend *bn) : VulkanBasicExecution(bn) {
std::vector<VkDescriptorType> types{VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER};
auto vulkanBn = static_cast<VulkanBackend *>(bn);
mRelu6Pipeline = vulkanBn->getPipeline("glsl_relu6_comp", /*glsl_relu6_comp, glsl_relu6_comp_len,*/ types);
mGpuRelu6Param.reset(new VulkanBuffer(vulkanBn->getMemoryPool(), false, sizeof(GpuReluParam), nullptr,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT));
}
VulkanRelu6::~VulkanRelu6() {
}
ErrorCode VulkanRelu6::onEncode(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs,
const VulkanCommandPool::Buffer *cmdBuffer) {
auto input = inputs[0];
auto output = outputs[0];
auto reluParam = reinterpret_cast<GpuReluParam *>(mGpuRelu6Param->map());
::memset(reluParam, 0, sizeof(GpuReluParam));
const int channelDiv4 = UP_DIV(input->channel(), 4);
reluParam->imgSize[0] = input->width();
reluParam->imgSize[1] = input->height();
reluParam->imgSize[2] = channelDiv4 * input->batch();
reluParam->imgSize[3] = 0;
reluParam->slope = 0;
mGpuRelu6Param->flush(true, 0, sizeof(GpuReluParam));
mGpuRelu6Param->unmap();
auto vkBn = (VulkanBackend *)backend();
mDescriptorSet.reset(mRelu6Pipeline->createSet());
mDescriptorSet->writeImage((VkImageView)output->deviceId(), vkBn->getCommonSampler()->get(),
VK_IMAGE_LAYOUT_GENERAL, 0);
mDescriptorSet->writeImage((VkImageView)input->deviceId(), vkBn->getCommonSampler()->get(),
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 1);
mDescriptorSet->writeBuffer(mGpuRelu6Param->buffer(), 2, mGpuRelu6Param->size());
mRelu6Pipeline->bind(cmdBuffer->get(), mDescriptorSet->get());
vkCmdDispatch(cmdBuffer->get(), UP_DIV(input->width(), 16), UP_DIV(input->height(), 16),
channelDiv4 * input->batch());
return NO_ERROR;
}
//--------------------------Prelu--------------------------//
VulkanPrelu::VulkanPrelu(Backend *bn, const Op *op) : VulkanBasicExecution(bn) {
std::vector<VkDescriptorType> types{VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER};
auto vulkanBn = static_cast<VulkanBackend *>(bn);
mPreluPipeline = vulkanBn->getPipeline("glsl_preluWithChannel_comp",
/*glsl_preluWithChannel_comp, glsl_preluWithChannel_comp_len,*/ types);
const auto prelu = op->main_as_PRelu();
mGpuPreluParam.reset(new VulkanBuffer(vulkanBn->getMemoryPool(), false, sizeof(GpuReluParam), nullptr,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT));
int count = ALIGN_UP4(prelu->slope()->size());
mSlope.reset(new VulkanImage(vulkanBn->getMemoryPool(), false, std::vector<int>{count / 4, 1}));
{
std::shared_ptr<VulkanBuffer> slopeBuffer(new VulkanBuffer(
vulkanBn->getMemoryPool(), false, sizeof(float) * count, nullptr, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT));
auto slope = slopeBuffer->map();
::memset(slope, 0, count * sizeof(float));
::memcpy(slope, prelu->slope()->data(), prelu->slope()->size() * sizeof(float));
slopeBuffer->unmap();
vulkanBn->copyBufferToImage(slopeBuffer.get(), mSlope.get());
}
}
VulkanPrelu::~VulkanPrelu() {
}
ErrorCode VulkanPrelu::onEncode(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs,
const VulkanCommandPool::Buffer *cmdBuffer) {
auto input = inputs[0];
auto output = outputs[0];
auto preluParam = reinterpret_cast<GpuReluParam *>(mGpuPreluParam->map());
::memset(preluParam, 0, sizeof(GpuReluParam));
auto vkBn = static_cast<VulkanBackend *>(backend());
const int channelDiv4 = UP_DIV(input->channel(), 4);
preluParam->imgSize[0] = input->width();
preluParam->imgSize[1] = input->height();
preluParam->imgSize[2] = channelDiv4;
preluParam->imgSize[3] = 0;
mGpuPreluParam->flush(true, 0, sizeof(GpuReluParam));
mGpuPreluParam->unmap();
mDescriptorSet.reset(mPreluPipeline->createSet());
mDescriptorSet->writeImage((VkImageView)output->deviceId(), vkBn->getCommonSampler()->get(),
VK_IMAGE_LAYOUT_GENERAL, 0);
mDescriptorSet->writeImage((VkImageView)input->deviceId(), vkBn->getCommonSampler()->get(),
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 1);
mDescriptorSet->writeImage((VkImageView)mSlope->view(), vkBn->getCommonSampler()->get(),
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 2);
mDescriptorSet->writeBuffer(mGpuPreluParam->buffer(), 3, mGpuPreluParam->size());
mPreluPipeline->bind(cmdBuffer->get(), mDescriptorSet->get());
vkCmdDispatch(cmdBuffer->get(), UP_DIV(input->width(), 16), UP_DIV(input->height(), 16), channelDiv4);
return NO_ERROR;
}
class VulkanReluCreator : public VulkanBackend::Creator {
public:
virtual Execution *onCreate(const std::vector<Tensor *> &inputs, const MNN::Op *op, Backend *bn) const override {
auto type = op->type();
auto input = inputs[0];
if (TensorUtils::getDescribe(input)->dimensionFormat != MNN_DATA_FORMAT_NC4HW4) {
return nullptr;
}
if (OpType_ReLU6 == type) {
return new VulkanRelu6(bn);
}
if (OpType_ReLU == type) {
return new VulkanRelu(bn, op->main_as_Relu()->slope());
} else if (1 == op->main_as_PRelu()->slopeCount()) {
return new VulkanRelu(bn, op->main_as_PRelu()->slope()->data()[0]);
} else {
return new VulkanPrelu(bn, op);
}
}
};
static bool gr = []() {
VulkanBackend::addCreator(OpType_ReLU, new VulkanReluCreator);
VulkanBackend::addCreator(OpType_PReLU, new VulkanReluCreator);
VulkanBackend::addCreator(OpType_ReLU6, new VulkanReluCreator);
return true;
}();
} // namespace MNN