Elffuss BraidHub

kikocis/ncnn public

master · 5.9 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "relu_vulkan.h"
16
17#include "layer_shader_type.h"
18
19namespace ncnn {
20
21ReLU_vulkan::ReLU_vulkan()
22{
23 support_vulkan = true;
24 support_image_storage = true;
25
26 pipeline_relu = 0;
27 pipeline_relu_pack4 = 0;
28 pipeline_relu_pack8 = 0;
29}
30
31int ReLU_vulkan::create_pipeline(const Option& opt)
32{
33 const Mat& shape = top_shapes.empty() ? Mat() : top_shapes[0];
34
35 int elempack = 1;
36 if (shape.dims == 1) elempack = opt.use_shader_pack8 && shape.w % 8 == 0 ? 8 : shape.w % 4 == 0 ? 4 : 1;
37 if (shape.dims == 2) elempack = opt.use_shader_pack8 && shape.h % 8 == 0 ? 8 : shape.h % 4 == 0 ? 4 : 1;
38 if (shape.dims == 3 || shape.dims == 4) elempack = opt.use_shader_pack8 && shape.c % 8 == 0 ? 8 : shape.c % 4 == 0 ? 4 : 1;
39
40 size_t elemsize;
41 if (opt.use_fp16_storage)
42 {
43 elemsize = elempack * 2u;
44 }
45 else if (opt.use_fp16_packed)
46 {
47 elemsize = elempack == 1 ? 4u : elempack * 2u;
48 }
49 else
50 {
51 elemsize = elempack * 4u;
52 }
53
54 Mat shape_packed;
55 if (shape.dims == 1) shape_packed = Mat(shape.w / elempack, (void*)0, elemsize, elempack);
56 if (shape.dims == 2) shape_packed = Mat(shape.w, shape.h / elempack, (void*)0, elemsize, elempack);
57 if (shape.dims == 3) shape_packed = Mat(shape.w, shape.h, shape.c / elempack, (void*)0, elemsize, elempack);
58 if (shape.dims == 4) shape_packed = Mat(shape.w, shape.h, shape.d, shape.c / elempack, (void*)0, elemsize, elempack);
59
60 std::vector<vk_specialization_type> specializations(1 + 5);
61 specializations[0].f = slope;
62 specializations[1 + 0].i = shape_packed.dims;
63 specializations[1 + 1].i = shape_packed.w;
64 specializations[1 + 2].i = shape_packed.h * shape_packed.d;
65 specializations[1 + 3].i = shape_packed.c;
66 specializations[1 + 4].i = shape_packed.cstep;
67
68 Mat local_size_xyz;
69 if (shape_packed.dims == 1)
70 {
71 local_size_xyz.w = std::min(64, shape_packed.w);
72 local_size_xyz.h = 1;
73 local_size_xyz.c = 1;
74 }
75 if (shape_packed.dims == 2)
76 {
77 local_size_xyz.w = std::min(8, shape_packed.w);
78 local_size_xyz.h = std::min(8, shape_packed.h);
79 local_size_xyz.c = 1;
80 }
81 if (shape_packed.dims == 3)
82 {
83 local_size_xyz.w = std::min(4, shape_packed.w);
84 local_size_xyz.h = std::min(4, shape_packed.h);
85 local_size_xyz.c = std::min(4, shape_packed.c);
86 }
87 if (shape_packed.dims == 4)
88 {
89 local_size_xyz.w = std::min(4, shape_packed.w);
90 local_size_xyz.h = std::min(4, shape_packed.h * shape_packed.d);
91 local_size_xyz.c = std::min(4, shape_packed.c);
92 }
93
94 // pack1
95 if (shape.dims == 0 || elempack == 1)
96 {
97 pipeline_relu = new Pipeline(vkdev);
98 pipeline_relu->set_optimal_local_size_xyz(local_size_xyz);
99 pipeline_relu->create(LayerShaderType::relu, opt, specializations);
100 }
101
102 // pack4
103 if (shape.dims == 0 || elempack == 4)
104 {
105 pipeline_relu_pack4 = new Pipeline(vkdev);
106 pipeline_relu_pack4->set_optimal_local_size_xyz(local_size_xyz);
107 pipeline_relu_pack4->create(LayerShaderType::relu_pack4, opt, specializations);
108 }
109
110 // pack8
111 if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8)
112 {
113 pipeline_relu_pack8 = new Pipeline(vkdev);
114 pipeline_relu_pack8->set_optimal_local_size_xyz(local_size_xyz);
115 pipeline_relu_pack8->create(LayerShaderType::relu_pack8, opt, specializations);
116 }
117
118 return 0;
119}
120
121int ReLU_vulkan::destroy_pipeline(const Option& /*opt*/)
122{
123 delete pipeline_relu;
124 pipeline_relu = 0;
125
126 delete pipeline_relu_pack4;
127 pipeline_relu_pack4 = 0;
128
129 delete pipeline_relu_pack8;
130 pipeline_relu_pack8 = 0;
131
132 return 0;
133}
134
135int ReLU_vulkan::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& /*opt*/) const
136{
137 int elempack = bottom_top_blob.elempack;
138
139 std::vector<VkMat> bindings(1);
140 bindings[0] = bottom_top_blob;
141
142 std::vector<vk_constant_type> constants(5);
143 constants[0].i = bottom_top_blob.dims;
144 constants[1].i = bottom_top_blob.w;
145 constants[2].i = bottom_top_blob.h * bottom_top_blob.d;
146 constants[3].i = bottom_top_blob.c;
147 constants[4].i = bottom_top_blob.cstep;
148
149 const Pipeline* pipeline = elempack == 8 ? pipeline_relu_pack8
150 : elempack == 4 ? pipeline_relu_pack4
151 : pipeline_relu;
152
153 cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
154
155 return 0;
156}
157
158int ReLU_vulkan::forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& /*opt*/) const
159{
160 int elempack = bottom_top_blob.elempack;
161
162 std::vector<VkImageMat> bindings(2);
163 bindings[0] = bottom_top_blob;
164 bindings[1] = bottom_top_blob;
165
166 std::vector<vk_constant_type> constants(5);
167 constants[0].i = bottom_top_blob.dims;
168 constants[1].i = bottom_top_blob.w;
169 constants[2].i = bottom_top_blob.h * bottom_top_blob.d;
170 constants[3].i = bottom_top_blob.c;
171 constants[4].i = 0; //bottom_top_blob.cstep;
172
173 const Pipeline* pipeline = elempack == 8 ? pipeline_relu_pack8
174 : elempack == 4 ? pipeline_relu_pack4
175 : pipeline_relu;
176
177 cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
178
179 return 0;
180}
181
182} // namespace ncnn