Elffuss BraidHub

kikocis/ncnn public

master · 3.8 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 "memorydata_vulkan.h"
16
17#include "layer_shader_type.h"
18
19namespace ncnn {
20
21MemoryData_vulkan::MemoryData_vulkan()
22{
23 support_vulkan = true;
24 support_image_storage = true;
25}
26
27int MemoryData_vulkan::create_pipeline(const Option& opt)
28{
29 const Mat& out_shape = top_shapes.empty() ? data.shape() : top_shapes[0];
30
31 int out_elempack = 1;
32 if (out_shape.dims == 1) out_elempack = opt.use_shader_pack8 && out_shape.w % 8 == 0 ? 8 : out_shape.w % 4 == 0 ? 4 : 1;
33 if (out_shape.dims == 2) out_elempack = opt.use_shader_pack8 && out_shape.h % 8 == 0 ? 8 : out_shape.h % 4 == 0 ? 4 : 1;
34 if (out_shape.dims == 3 || out_shape.dims == 4) out_elempack = opt.use_shader_pack8 && out_shape.c % 8 == 0 ? 8 : out_shape.c % 4 == 0 ? 4 : 1;
35
36 size_t out_elemsize;
37 if (opt.use_fp16_storage)
38 {
39 out_elemsize = out_elempack * 2u;
40 }
41 else if (opt.use_fp16_packed)
42 {
43 out_elemsize = out_elempack == 1 ? 4u : out_elempack * 2u;
44 }
45 else
46 {
47 out_elemsize = out_elempack * 4u;
48 }
49
50 Mat out_shape_packed;
51 if (out_shape.dims == 1) out_shape_packed = Mat(out_shape.w / out_elempack, (void*)0, out_elemsize, out_elempack);
52 if (out_shape.dims == 2) out_shape_packed = Mat(out_shape.w, out_shape.h / out_elempack, (void*)0, out_elemsize, out_elempack);
53 if (out_shape.dims == 3 || out_shape.dims == 4) out_shape_packed = Mat(out_shape.w, out_shape.h, out_shape.c / out_elempack, (void*)0, out_elemsize, out_elempack);
54
55 // check blob shape
56 if (!vkdev->shape_support_image_storage(out_shape_packed))
57 {
58 support_image_storage = false;
59 }
60
61 return 0;
62}
63
64int MemoryData_vulkan::upload_model(VkTransfer& cmd, const Option& opt)
65{
66 const Mat& shape = data.shape();
67
68 int elempack = 1;
69 if (shape.dims == 1) elempack = opt.use_shader_pack8 && shape.w % 8 == 0 ? 8 : shape.w % 4 == 0 ? 4 : 1;
70 if (shape.dims == 2) elempack = opt.use_shader_pack8 && shape.h % 8 == 0 ? 8 : shape.h % 4 == 0 ? 4 : 1;
71 if (shape.dims == 3 || shape.dims == 4) elempack = opt.use_shader_pack8 && shape.c % 8 == 0 ? 8 : shape.c % 4 == 0 ? 4 : 1;
72
73 Mat data_packed;
74 convert_packing(data, data_packed, elempack, opt);
75
76 if (support_image_storage && opt.use_image_storage)
77 {
78 cmd.record_upload(data_packed, data_gpu_image, opt);
79 }
80 else
81 {
82 cmd.record_upload(data_packed, data_gpu, opt, /*bool flatten*/ false);
83 }
84
85 if (opt.lightmode)
86 {
87 data.release();
88 }
89
90 return 0;
91}
92
93int MemoryData_vulkan::forward(const std::vector<VkMat>& /*bottom_blobs*/, std::vector<VkMat>& top_blobs, VkCompute& cmd, const Option& opt) const
94{
95 VkMat& top_blob = top_blobs[0];
96
97 cmd.record_clone(data_gpu, top_blob, opt);
98 if (top_blob.empty())
99 return -100;
100
101 return 0;
102}
103
104int MemoryData_vulkan::forward(const std::vector<VkImageMat>& /*bottom_blobs*/, std::vector<VkImageMat>& top_blobs, VkCompute& cmd, const Option& opt) const
105{
106 VkImageMat& top_blob = top_blobs[0];
107
108 cmd.record_clone(data_gpu_image, top_blob, opt);
109 if (top_blob.empty())
110 return -100;
111
112 return 0;
113}
114
115} // namespace ncnn