Elffuss BraidHub

kikocis/ncnn public

master · 6.8 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2023 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#version 450
16
17#if NCNN_fp16_storage
18#extension GL_EXT_shader_16bit_storage: require
19#endif
20#if NCNN_fp16_arithmetic
21#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
22#endif
23
24#extension GL_GOOGLE_include_directive: enable
25#include "vulkan_activation.comp"
26
27layout (constant_id = 0) const int kernel_w = 1;
28layout (constant_id = 1) const int dilation_w = 1;
29layout (constant_id = 2) const int stride_w = 1;
30layout (constant_id = 3) const int bias_term = 0;
31layout (constant_id = 4) const int activation_type = 0;
32layout (constant_id = 5) const float activation_param_0 = 0;
33layout (constant_id = 6) const float activation_param_1 = 0;
34
35#define shape_constant_id_offset 7
36layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
37layout (constant_id = shape_constant_id_offset + 1) const int h = 0;
38
39layout (constant_id = shape_constant_id_offset + 2) const int outw = 0;
40layout (constant_id = shape_constant_id_offset + 3) const int outh = 0;
41
42#if NCNN_image_shader
43layout (binding = 0) uniform unfp sampler3D bottom_blob;
44layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob;
45layout (binding = 2) uniform unfp sampler3D weight_blob;
46layout (binding = 3) uniform unfp sampler3D bias_blob;
47#else
48layout (binding = 0) readonly buffer bottom_blob { sfpvec4 bottom_blob_data[]; };
49layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
50#if NCNN_fp16_packed || (NCNN_fp16_storage && !NCNN_fp16_arithmetic)
51// GL_EXT_shader_16bit_storage does not define f16mat4 type :(
52layout (binding = 2) readonly buffer weight_blob { sfpvec4 weight_data[]; };
53#else
54layout (binding = 2) readonly buffer weight_blob { sfpmat4 weight_data[]; };
55#endif
56layout (binding = 3) readonly buffer bias_blob { sfpvec4 bias_data[]; };
57#endif
58
59layout (push_constant) uniform parameter
60{
61 int w;
62 int h;
63
64 int outw;
65 int outh;
66} p;
67
68void main()
69{
70 int gx = int(gl_GlobalInvocationID.x) * 2;
71 int gy = int(gl_GlobalInvocationID.y) * 2;
72
73 if (gx >= psc(outw) || gy >= psc(outh))
74 return;
75
76 const ivec2 gx2 = gx + ivec2(0, 1);
77 const ivec2 gy2 = gy + ivec2(0, 1);
78
79 afpvec4 sum0 = afpvec4(0.0f);
80 afpvec4 sum1 = afpvec4(0.0f);
81 afpvec4 sum2 = afpvec4(0.0f);
82 afpvec4 sum3 = afpvec4(0.0f);
83
84 if (bias_term == 1)
85 {
86#if NCNN_image_shader
87 sum0 = image3d_ld4(bias_blob, ivec3(gy2.x, 0, 0));
88 sum2 = image3d_ld4(bias_blob, ivec3(gy2.y, 0, 0));
89#else
90 sum0 = buffer_ld4(bias_data, gy2.x);
91 sum2 = buffer_ld4(bias_data, gy2.y);
92#endif
93 sum1 = sum0;
94 sum3 = sum2;
95 }
96
97#if NCNN_image_shader
98
99 ivec2 v_offset = gx2 * stride_w;
100
101 for (int y = 0; y < psc(h); y++)
102 {
103 int wx = 0;
104
105 for (int x = 0; x < kernel_w; x++)
106 {
107 afpvec4 v0 = image3d_ld4(bottom_blob, ivec3(v_offset.x + x * dilation_w, y, 0));
108 afpvec4 v1 = image3d_ld4(bottom_blob, ivec3(v_offset.y + x * dilation_w, y, 0));
109
110 afpmat4 k0 = afpmat4(
111 image3d_ld4(weight_blob, ivec3(wx + 0, y, gy2.x)),
112 image3d_ld4(weight_blob, ivec3(wx + 1, y, gy2.x)),
113 image3d_ld4(weight_blob, ivec3(wx + 2, y, gy2.x)),
114 image3d_ld4(weight_blob, ivec3(wx + 3, y, gy2.x))
115 );
116 afpmat4 k1 = afpmat4(
117 image3d_ld4(weight_blob, ivec3(wx + 0, y, gy2.y)),
118 image3d_ld4(weight_blob, ivec3(wx + 1, y, gy2.y)),
119 image3d_ld4(weight_blob, ivec3(wx + 2, y, gy2.y)),
120 image3d_ld4(weight_blob, ivec3(wx + 3, y, gy2.y))
121 );
122
123 sum0 += v0 * k0;
124 sum1 += v1 * k0;
125 sum2 += v0 * k1;
126 sum3 += v1 * k1;
127
128 wx += 4;
129 }
130 }
131
132#else
133
134 ivec2 v_offset = gx2 * stride_w;
135 ivec2 w_offset = gy2 * psc(h) * kernel_w;
136
137 for (int y = 0; y < psc(h); y++)
138 {
139 for (int x = 0; x < kernel_w; x++)
140 {
141 afpvec4 v0 = buffer_ld4(bottom_blob_data, v_offset.x + x * dilation_w);
142 afpvec4 v1 = buffer_ld4(bottom_blob_data, v_offset.y + x * dilation_w);
143
144#if NCNN_fp16_packed || (NCNN_fp16_storage && !NCNN_fp16_arithmetic)
145 // GL_EXT_shader_16bit_storage does not define f16mat4 type :(
146 afpmat4 k0 = afpmat4(
147 buffer_ld4(weight_data, (w_offset.x + x) * 4 + 0),
148 buffer_ld4(weight_data, (w_offset.x + x) * 4 + 1),
149 buffer_ld4(weight_data, (w_offset.x + x) * 4 + 2),
150 buffer_ld4(weight_data, (w_offset.x + x) * 4 + 3)
151 );
152 afpmat4 k1 = afpmat4(
153 buffer_ld4(weight_data, (w_offset.y + x) * 4 + 0),
154 buffer_ld4(weight_data, (w_offset.y + x) * 4 + 1),
155 buffer_ld4(weight_data, (w_offset.y + x) * 4 + 2),
156 buffer_ld4(weight_data, (w_offset.y + x) * 4 + 3)
157 );
158#else
159 afpmat4 k0 = sfp2afpmat4(weight_data[w_offset.x + x]);
160 afpmat4 k1 = sfp2afpmat4(weight_data[w_offset.y + x]);
161#endif
162
163 sum0 += v0 * k0;
164 sum1 += v1 * k0;
165 sum2 += v0 * k1;
166 sum3 += v1 * k1;
167 }
168 v_offset += psc(w);
169 w_offset += kernel_w;
170 }
171
172#endif
173
174 sum0 = activation_afpvec4(sum0, activation_type, activation_param_0, activation_param_1);
175 sum1 = activation_afpvec4(sum1, activation_type, activation_param_0, activation_param_1);
176 sum2 = activation_afpvec4(sum2, activation_type, activation_param_0, activation_param_1);
177 sum3 = activation_afpvec4(sum3, activation_type, activation_param_0, activation_param_1);
178
179#if NCNN_image_shader
180
181 image3d_st4(top_blob, ivec3(gx2.x, gy2.x, 0), sum0);
182 image3d_st4(top_blob, ivec3(gx2.y, gy2.x, 0), sum1);
183 image3d_st4(top_blob, ivec3(gx2.x, gy2.y, 0), sum2);
184 image3d_st4(top_blob, ivec3(gx2.y, gy2.y, 0), sum3);
185
186#else
187
188 const int gi = gy * psc(outw) + gx;
189
190 buffer_st4(top_blob_data, gi, sum0);
191 if (gx + 1 < psc(outw)) buffer_st4(top_blob_data, gi + 1, sum1);
192 if (gy + 1 < psc(outh)) buffer_st4(top_blob_data, gi + psc(outw), sum2);
193 if (gy + 1 < psc(outh) && gx + 1 < psc(outw)) buffer_st4(top_blob_data, gi + psc(outw) + 1, sum3);
194
195#endif
196}