Elffuss BraidHub

kikocis/ncnn public

master · 7.0 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 "shufflechannel_vulkan.h"
16
17#include "layer_shader_type.h"
18
19namespace ncnn {
20
21ShuffleChannel_vulkan::ShuffleChannel_vulkan()
22{
23 support_vulkan = true;
24 support_image_storage = true;
25
26 pipeline_shufflechannel = 0;
27 pipeline_shufflechannel_pack4 = 0;
28 pipeline_shufflechannel_pack8 = 0;
29}
30
31int ShuffleChannel_vulkan::create_pipeline(const Option& opt)
32{
33 const Mat& shape = bottom_shapes.empty() ? Mat() : bottom_shapes[0];
34 const Mat& out_shape = top_shapes.empty() ? Mat() : top_shapes[0];
35
36 int elempack = 1;
37 if (shape.dims == 3) elempack = opt.use_shader_pack8 && shape.c % 8 == 0 ? 8 : shape.c % 4 == 0 ? 4 : 1;
38
39 int out_elempack = 1;
40 if (out_shape.dims == 3) out_elempack = opt.use_shader_pack8 && out_shape.c % 8 == 0 ? 8 : out_shape.c % 4 == 0 ? 4 : 1;
41
42 size_t elemsize;
43 size_t out_elemsize;
44 if (opt.use_fp16_storage)
45 {
46 elemsize = elempack * 2u;
47 out_elemsize = out_elempack * 2u;
48 }
49 else if (opt.use_fp16_packed)
50 {
51 elemsize = elempack == 1 ? 4u : elempack * 2u;
52 out_elemsize = out_elempack == 1 ? 4u : out_elempack * 2u;
53 }
54 else
55 {
56 elemsize = elempack * 4u;
57 out_elemsize = out_elempack * 4u;
58 }
59
60 Mat shape_packed;
61 if (shape.dims == 3) shape_packed = Mat(shape.w, shape.h, shape.c / elempack, (void*)0, elemsize, elempack);
62
63 Mat out_shape_packed;
64 if (out_shape.dims == 3) out_shape_packed = Mat(out_shape.w, out_shape.h, out_shape.c / out_elempack, (void*)0, out_elemsize, out_elempack);
65
66 std::vector<vk_specialization_type> specializations(2 + 10);
67 specializations[0].i = reverse ? shape.c / group : group;
68 specializations[1].i = vkdev->info.bug_implicit_fp16_arithmetic();
69 specializations[2 + 0].i = shape_packed.dims;
70 specializations[2 + 1].i = shape_packed.w;
71 specializations[2 + 2].i = shape_packed.h;
72 specializations[2 + 3].i = shape_packed.c;
73 specializations[2 + 4].i = shape_packed.cstep;
74 specializations[2 + 5].i = out_shape_packed.dims;
75 specializations[2 + 6].i = out_shape_packed.w;
76 specializations[2 + 7].i = out_shape_packed.h;
77 specializations[2 + 8].i = out_shape_packed.c;
78 specializations[2 + 9].i = out_shape_packed.cstep;
79
80 Mat local_size_xyz;
81 if (out_shape_packed.dims != 0)
82 {
83 local_size_xyz.w = std::min(4, out_shape_packed.w);
84 local_size_xyz.h = std::min(4, out_shape_packed.h);
85 local_size_xyz.c = std::min(4, out_shape_packed.c);
86 }
87
88 // pack1
89 if (shape.dims == 0 || elempack == 1)
90 {
91 pipeline_shufflechannel = new Pipeline(vkdev);
92 pipeline_shufflechannel->set_optimal_local_size_xyz(local_size_xyz);
93 pipeline_shufflechannel->create(LayerShaderType::shufflechannel, opt, specializations);
94 }
95
96 // pack4
97 if (shape.dims == 0 || elempack == 4)
98 {
99 pipeline_shufflechannel_pack4 = new Pipeline(vkdev);
100 pipeline_shufflechannel_pack4->set_optimal_local_size_xyz(local_size_xyz);
101 pipeline_shufflechannel_pack4->create(LayerShaderType::shufflechannel_pack4, opt, specializations);
102 }
103
104 // pack8
105 if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8)
106 {
107 pipeline_shufflechannel_pack8 = new Pipeline(vkdev);
108 pipeline_shufflechannel_pack8->set_optimal_local_size_xyz(local_size_xyz);
109 pipeline_shufflechannel_pack8->create(LayerShaderType::shufflechannel_pack8, opt, specializations);
110 }
111
112 return 0;
113}
114
115int ShuffleChannel_vulkan::destroy_pipeline(const Option& /*opt*/)
116{
117 delete pipeline_shufflechannel;
118 pipeline_shufflechannel = 0;
119
120 delete pipeline_shufflechannel_pack4;
121 pipeline_shufflechannel_pack4 = 0;
122
123 delete pipeline_shufflechannel_pack8;
124 pipeline_shufflechannel_pack8 = 0;
125
126 return 0;
127}
128
129int ShuffleChannel_vulkan::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const
130{
131 int w = bottom_blob.w;
132 int h = bottom_blob.h;
133 int channels = bottom_blob.c;
134 size_t elemsize = bottom_blob.elemsize;
135 int elempack = bottom_blob.elempack;
136
137 top_blob.create(w, h, channels, elemsize, elempack, opt.blob_vkallocator);
138 if (top_blob.empty())
139 return -100;
140
141 std::vector<VkMat> bindings(2);
142 bindings[0] = bottom_blob;
143 bindings[1] = top_blob;
144
145 std::vector<vk_constant_type> constants(11);
146 constants[0].i = bottom_blob.dims;
147 constants[1].i = bottom_blob.w;
148 constants[2].i = bottom_blob.h;
149 constants[3].i = bottom_blob.c;
150 constants[4].i = bottom_blob.cstep;
151 constants[5].i = top_blob.dims;
152 constants[6].i = top_blob.w;
153 constants[7].i = top_blob.h;
154 constants[8].i = top_blob.c;
155 constants[9].i = top_blob.cstep;
156 constants[10].i = reverse ? channels * elempack / group : group;
157
158 const Pipeline* pipeline = elempack == 8 ? pipeline_shufflechannel_pack8
159 : elempack == 4 ? pipeline_shufflechannel_pack4
160 : pipeline_shufflechannel;
161
162 cmd.record_pipeline(pipeline, bindings, constants, top_blob);
163
164 return 0;
165}
166
167int ShuffleChannel_vulkan::forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const
168{
169 int w = bottom_blob.w;
170 int h = bottom_blob.h;
171 int channels = bottom_blob.c;
172 size_t elemsize = bottom_blob.elemsize;
173 int elempack = bottom_blob.elempack;
174
175 top_blob.create(w, h, channels, elemsize, elempack, opt.blob_vkallocator);
176 if (top_blob.empty())
177 return -100;
178
179 std::vector<VkImageMat> bindings(2);
180 bindings[0] = bottom_blob;
181 bindings[1] = top_blob;
182
183 std::vector<vk_constant_type> constants(11);
184 constants[0].i = bottom_blob.dims;
185 constants[1].i = bottom_blob.w;
186 constants[2].i = bottom_blob.h;
187 constants[3].i = bottom_blob.c;
188 constants[4].i = 0; //bottom_blob.cstep;
189 constants[5].i = top_blob.dims;
190 constants[6].i = top_blob.w;
191 constants[7].i = top_blob.h;
192 constants[8].i = top_blob.c;
193 constants[9].i = 0; //top_blob.cstep;
194 constants[10].i = reverse ? channels * elempack / group : group;
195
196 const Pipeline* pipeline = elempack == 8 ? pipeline_shufflechannel_pack8
197 : elempack == 4 ? pipeline_shufflechannel_pack4
198 : pipeline_shufflechannel;
199
200 cmd.record_pipeline(pipeline, bindings, constants, top_blob);
201
202 return 0;
203}
204
205} // namespace ncnn