Elffuss BraidHub

kikocis/ncnn public

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