Elffuss BraidHub

kikocis/ncnn public

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