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 "clip_vulkan.h"
16
17#include "layer_shader_type.h"
18
19namespace ncnn {
20
21Clip_vulkan::Clip_vulkan()
22{
23 support_vulkan = true;
24 support_image_storage = true;
25
26 pipeline_clip = 0;
27 pipeline_clip_pack4 = 0;
28 pipeline_clip_pack8 = 0;
29}
30
31int Clip_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(2 + 5);
61 specializations[0].f = min;
62 specializations[1].f = max;
63 specializations[2 + 0].i = shape_packed.dims;
64 specializations[2 + 1].i = shape_packed.w;
65 specializations[2 + 2].i = shape_packed.h * shape_packed.d;
66 specializations[2 + 3].i = shape_packed.c;
67 specializations[2 + 4].i = shape_packed.cstep;
68
69 Mat local_size_xyz;
70 if (shape_packed.dims == 1)
71 {
72 local_size_xyz.w = std::min(64, shape_packed.w);
73 local_size_xyz.h = 1;
74 local_size_xyz.c = 1;
75 }
76 if (shape_packed.dims == 2)
77 {
78 local_size_xyz.w = std::min(8, shape_packed.w);
79 local_size_xyz.h = std::min(8, shape_packed.h);
80 local_size_xyz.c = 1;
81 }
82 if (shape_packed.dims == 3)
83 {
84 local_size_xyz.w = std::min(4, shape_packed.w);
85 local_size_xyz.h = std::min(4, shape_packed.h);
86 local_size_xyz.c = std::min(4, shape_packed.c);
87 }
88 if (shape_packed.dims == 4)
89 {
90 local_size_xyz.w = std::min(4, shape_packed.w);
91 local_size_xyz.h = std::min(4, shape_packed.h * shape_packed.d);
92 local_size_xyz.c = std::min(4, shape_packed.c);
93 }
94
95 // pack1
96 if (shape.dims == 0 || elempack == 1)
97 {
98 pipeline_clip = new Pipeline(vkdev);
99 pipeline_clip->set_optimal_local_size_xyz(local_size_xyz);
100 pipeline_clip->create(LayerShaderType::clip, opt, specializations);
101 }
102
103 // pack4
104 if (shape.dims == 0 || elempack == 4)
105 {
106 pipeline_clip_pack4 = new Pipeline(vkdev);
107 pipeline_clip_pack4->set_optimal_local_size_xyz(local_size_xyz);
108 pipeline_clip_pack4->create(LayerShaderType::clip_pack4, opt, specializations);
109 }
110
111 // pack8
112 if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8)
113 {
114 pipeline_clip_pack8 = new Pipeline(vkdev);
115 pipeline_clip_pack8->set_optimal_local_size_xyz(local_size_xyz);
116 pipeline_clip_pack8->create(LayerShaderType::clip_pack8, opt, specializations);
117 }
118
119 return 0;
120}
121
122int Clip_vulkan::destroy_pipeline(const Option& /*opt*/)
123{
124 delete pipeline_clip;
125 pipeline_clip = 0;
126
127 delete pipeline_clip_pack4;
128 pipeline_clip_pack4 = 0;
129
130 delete pipeline_clip_pack8;
131 pipeline_clip_pack8 = 0;
132
133 return 0;
134}
135
136int Clip_vulkan::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& /*opt*/) const
137{
138 int elempack = bottom_top_blob.elempack;
139
140 std::vector<VkMat> bindings(1);
141 bindings[0] = bottom_top_blob;
142
143 std::vector<vk_constant_type> constants(5);
144 constants[0].i = bottom_top_blob.dims;
145 constants[1].i = bottom_top_blob.w;
146 constants[2].i = bottom_top_blob.h * bottom_top_blob.d;
147 constants[3].i = bottom_top_blob.c;
148 constants[4].i = bottom_top_blob.cstep;
149
150 const Pipeline* pipeline = elempack == 8 ? pipeline_clip_pack8
151 : elempack == 4 ? pipeline_clip_pack4
152 : pipeline_clip;
153
154 cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
155
156 return 0;
157}
158
159int Clip_vulkan::forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& /*opt*/) const
160{
161 int elempack = bottom_top_blob.elempack;
162
163 std::vector<VkImageMat> bindings(2);
164 bindings[0] = bottom_top_blob;
165 bindings[1] = bottom_top_blob;
166
167 std::vector<vk_constant_type> constants(5);
168 constants[0].i = bottom_top_blob.dims;
169 constants[1].i = bottom_top_blob.w;
170 constants[2].i = bottom_top_blob.h * bottom_top_blob.d;
171 constants[3].i = bottom_top_blob.c;
172 constants[4].i = 0; //bottom_top_blob.cstep;
173
174 const Pipeline* pipeline = elempack == 8 ? pipeline_clip_pack8
175 : elempack == 4 ? pipeline_clip_pack4
176 : pipeline_clip;
177
178 cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
179
180 return 0;
181}
182
183} // namespace ncnn