Elffuss BraidHub

kikocis/ncnn public

master · 9.7 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
19struct sfpvec8 { f16vec4 abcd; f16vec4 efgh; };
20#endif
21#if NCNN_fp16_arithmetic
22#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
23#endif
24
25#extension GL_GOOGLE_include_directive: enable
26#include "vulkan_activation.comp"
27
28layout (constant_id = 0) const int kernel_w = 1;
29layout (constant_id = 1) const int dilation_w = 1;
30layout (constant_id = 2) const int stride_w = 1;
31layout (constant_id = 3) const int bias_term = 0;
32layout (constant_id = 4) const int activation_type = 0;
33layout (constant_id = 5) const float activation_param_0 = 0;
34layout (constant_id = 6) const float activation_param_1 = 0;
35
36#define shape_constant_id_offset 7
37layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
38layout (constant_id = shape_constant_id_offset + 1) const int h = 0;
39
40layout (constant_id = shape_constant_id_offset + 2) const int outw = 0;
41layout (constant_id = shape_constant_id_offset + 3) const int outh = 0;
42
43#if NCNN_image_shader
44layout (binding = 0) uniform unfp sampler3D bottom_blob;
45layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob;
46layout (binding = 2) uniform unfp sampler3D weight_blob;
47layout (binding = 3) uniform unfp sampler3D bias_blob;
48#else
49layout (binding = 0) readonly buffer bottom_blob { sfpvec4 bottom_blob_data[]; };
50layout (binding = 1) writeonly buffer top_blob { sfpvec8 top_blob_data[]; };
51layout (binding = 2) readonly buffer weight_blob { sfpvec4 weight_data[]; };
52layout (binding = 3) readonly buffer bias_blob { sfpvec8 bias_data[]; };
53#endif
54
55layout (push_constant) uniform parameter
56{
57 int w;
58 int h;
59
60 int outw;
61 int outh;
62} p;
63
64void main()
65{
66 int gx = int(gl_GlobalInvocationID.x) * 2;
67 int gy = int(gl_GlobalInvocationID.y) * 2;
68
69 if (gx >= psc(outw) || gy >= psc(outh))
70 return;
71
72 const ivec2 gx2 = gx + ivec2(0, 1);
73 const ivec2 gy2 = gy + ivec2(0, 1);
74
75 afpvec8 sum0 = afpvec8(afpvec4(0.0f), afpvec4(0.0f));
76 afpvec8 sum1 = afpvec8(afpvec4(0.0f), afpvec4(0.0f));
77 afpvec8 sum2 = afpvec8(afpvec4(0.0f), afpvec4(0.0f));
78 afpvec8 sum3 = afpvec8(afpvec4(0.0f), afpvec4(0.0f));
79
80 if (bias_term == 1)
81 {
82#if NCNN_image_shader
83 sum0 = image3d_ld8(bias_blob, ivec3(gy2.x, 0, 0));
84 sum2 = image3d_ld8(bias_blob, ivec3(gy2.y, 0, 0));
85#else
86 sum0 = buffer_ld8(bias_data, gy2.x);
87 sum2 = buffer_ld8(bias_data, gy2.y);
88#endif
89 sum1 = sum0;
90 sum3 = sum2;
91 }
92
93#if NCNN_image_shader
94
95 ivec2 v_offset = gx2 * stride_w;
96
97 for (int y = 0; y < psc(h); y++)
98 {
99 int wx = 0;
100
101 for (int x = 0; x < kernel_w; x++)
102 {
103 afpvec4 v0 = image3d_ld4(bottom_blob, ivec3(v_offset.x + x * dilation_w, y, 0));
104 afpvec4 v1 = image3d_ld4(bottom_blob, ivec3(v_offset.y + x * dilation_w, y, 0));
105
106 afpvec4 k0 = image3d_ld4(weight_blob, ivec3(wx + 0, y, gy2.x));
107 afpvec4 k1 = image3d_ld4(weight_blob, ivec3(wx + 1, y, gy2.x));
108 afpvec4 k2 = image3d_ld4(weight_blob, ivec3(wx + 2, y, gy2.x));
109 afpvec4 k3 = image3d_ld4(weight_blob, ivec3(wx + 3, y, gy2.x));
110 afpvec4 k4 = image3d_ld4(weight_blob, ivec3(wx + 4, y, gy2.x));
111 afpvec4 k5 = image3d_ld4(weight_blob, ivec3(wx + 5, y, gy2.x));
112 afpvec4 k6 = image3d_ld4(weight_blob, ivec3(wx + 6, y, gy2.x));
113 afpvec4 k7 = image3d_ld4(weight_blob, ivec3(wx + 7, y, gy2.x));
114
115 afpvec4 k8 = image3d_ld4(weight_blob, ivec3(wx + 0, y, gy2.y));
116 afpvec4 k9 = image3d_ld4(weight_blob, ivec3(wx + 1, y, gy2.y));
117 afpvec4 ka = image3d_ld4(weight_blob, ivec3(wx + 2, y, gy2.y));
118 afpvec4 kb = image3d_ld4(weight_blob, ivec3(wx + 3, y, gy2.y));
119 afpvec4 kc = image3d_ld4(weight_blob, ivec3(wx + 4, y, gy2.y));
120 afpvec4 kd = image3d_ld4(weight_blob, ivec3(wx + 5, y, gy2.y));
121 afpvec4 ke = image3d_ld4(weight_blob, ivec3(wx + 6, y, gy2.y));
122 afpvec4 kf = image3d_ld4(weight_blob, ivec3(wx + 7, y, gy2.y));
123
124 sum0[0].r += dot(v0, k0);
125 sum0[0].g += dot(v0, k1);
126 sum0[0].b += dot(v0, k2);
127 sum0[0].a += dot(v0, k3);
128 sum0[1].r += dot(v0, k4);
129 sum0[1].g += dot(v0, k5);
130 sum0[1].b += dot(v0, k6);
131 sum0[1].a += dot(v0, k7);
132
133 sum1[0].r += dot(v1, k0);
134 sum1[0].g += dot(v1, k1);
135 sum1[0].b += dot(v1, k2);
136 sum1[0].a += dot(v1, k3);
137 sum1[1].r += dot(v1, k4);
138 sum1[1].g += dot(v1, k5);
139 sum1[1].b += dot(v1, k6);
140 sum1[1].a += dot(v1, k7);
141
142 sum2[0].r += dot(v0, k8);
143 sum2[0].g += dot(v0, k9);
144 sum2[0].b += dot(v0, ka);
145 sum2[0].a += dot(v0, kb);
146 sum2[1].r += dot(v0, kc);
147 sum2[1].g += dot(v0, kd);
148 sum2[1].b += dot(v0, ke);
149 sum2[1].a += dot(v0, kf);
150
151 sum3[0].r += dot(v1, k8);
152 sum3[0].g += dot(v1, k9);
153 sum3[0].b += dot(v1, ka);
154 sum3[0].a += dot(v1, kb);
155 sum3[1].r += dot(v1, kc);
156 sum3[1].g += dot(v1, kd);
157 sum3[1].b += dot(v1, ke);
158 sum3[1].a += dot(v1, kf);
159
160 wx += 8;
161 }
162 }
163
164#else
165
166 ivec2 v_offset = gx2 * stride_w;
167 ivec2 w_offset = gy2 * psc(h) * kernel_w;
168
169 for (int y = 0; y < psc(h); y++)
170 {
171 for (int x = 0; x < kernel_w; x++)
172 {
173 afpvec4 v0 = buffer_ld4(bottom_blob_data, v_offset.x + x * dilation_w);
174 afpvec4 v1 = buffer_ld4(bottom_blob_data, v_offset.y + x * dilation_w);
175
176 afpvec4 k0 = buffer_ld4(weight_data, (w_offset.x + x) * 8 + 0);
177 afpvec4 k1 = buffer_ld4(weight_data, (w_offset.x + x) * 8 + 1);
178 afpvec4 k2 = buffer_ld4(weight_data, (w_offset.x + x) * 8 + 2);
179 afpvec4 k3 = buffer_ld4(weight_data, (w_offset.x + x) * 8 + 3);
180 afpvec4 k4 = buffer_ld4(weight_data, (w_offset.x + x) * 8 + 4);
181 afpvec4 k5 = buffer_ld4(weight_data, (w_offset.x + x) * 8 + 5);
182 afpvec4 k6 = buffer_ld4(weight_data, (w_offset.x + x) * 8 + 6);
183 afpvec4 k7 = buffer_ld4(weight_data, (w_offset.x + x) * 8 + 7);
184
185 afpvec4 k8 = buffer_ld4(weight_data, (w_offset.y + x) * 8 + 0);
186 afpvec4 k9 = buffer_ld4(weight_data, (w_offset.y + x) * 8 + 1);
187 afpvec4 ka = buffer_ld4(weight_data, (w_offset.y + x) * 8 + 2);
188 afpvec4 kb = buffer_ld4(weight_data, (w_offset.y + x) * 8 + 3);
189 afpvec4 kc = buffer_ld4(weight_data, (w_offset.y + x) * 8 + 4);
190 afpvec4 kd = buffer_ld4(weight_data, (w_offset.y + x) * 8 + 5);
191 afpvec4 ke = buffer_ld4(weight_data, (w_offset.y + x) * 8 + 6);
192 afpvec4 kf = buffer_ld4(weight_data, (w_offset.y + x) * 8 + 7);
193
194 sum0[0].r += dot(v0, k0);
195 sum0[0].g += dot(v0, k1);
196 sum0[0].b += dot(v0, k2);
197 sum0[0].a += dot(v0, k3);
198 sum0[1].r += dot(v0, k4);
199 sum0[1].g += dot(v0, k5);
200 sum0[1].b += dot(v0, k6);
201 sum0[1].a += dot(v0, k7);
202
203 sum1[0].r += dot(v1, k0);
204 sum1[0].g += dot(v1, k1);
205 sum1[0].b += dot(v1, k2);
206 sum1[0].a += dot(v1, k3);
207 sum1[1].r += dot(v1, k4);
208 sum1[1].g += dot(v1, k5);
209 sum1[1].b += dot(v1, k6);
210 sum1[1].a += dot(v1, k7);
211
212 sum2[0].r += dot(v0, k8);
213 sum2[0].g += dot(v0, k9);
214 sum2[0].b += dot(v0, ka);
215 sum2[0].a += dot(v0, kb);
216 sum2[1].r += dot(v0, kc);
217 sum2[1].g += dot(v0, kd);
218 sum2[1].b += dot(v0, ke);
219 sum2[1].a += dot(v0, kf);
220
221 sum3[0].r += dot(v1, k8);
222 sum3[0].g += dot(v1, k9);
223 sum3[0].b += dot(v1, ka);
224 sum3[0].a += dot(v1, kb);
225 sum3[1].r += dot(v1, kc);
226 sum3[1].g += dot(v1, kd);
227 sum3[1].b += dot(v1, ke);
228 sum3[1].a += dot(v1, kf);
229 }
230 v_offset += psc(w);
231 w_offset += kernel_w;
232 }
233
234#endif
235
236 sum0 = activation_afpvec8(sum0, activation_type, activation_param_0, activation_param_1);
237 sum1 = activation_afpvec8(sum1, activation_type, activation_param_0, activation_param_1);
238 sum2 = activation_afpvec8(sum2, activation_type, activation_param_0, activation_param_1);
239 sum3 = activation_afpvec8(sum3, activation_type, activation_param_0, activation_param_1);
240
241#if NCNN_image_shader
242
243 image3d_st8(top_blob, ivec3(gx2.x, gy2.x, 0), sum0);
244 image3d_st8(top_blob, ivec3(gx2.y, gy2.x, 0), sum1);
245 image3d_st8(top_blob, ivec3(gx2.x, gy2.y, 0), sum2);
246 image3d_st8(top_blob, ivec3(gx2.y, gy2.y, 0), sum3);
247
248#else
249
250 const int gi = gy * psc(outw) + gx;
251
252 buffer_st8(top_blob_data, gi, sum0);
253 if (gx + 1 < psc(outw)) buffer_st8(top_blob_data, gi + 1, sum1);
254 if (gy + 1 < psc(outh)) buffer_st8(top_blob_data, gi + psc(outw), sum2);
255 if (gy + 1 < psc(outh) && gx + 1 < psc(outw)) buffer_st8(top_blob_data, gi + psc(outw) + 1, sum3);
256
257#endif
258}