Elffuss BraidHub

kikocis/ncnn public

master · 10.0 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2019 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 kernel_h = 1;
29layout (constant_id = 2) const int dilation_w = 1;
30layout (constant_id = 3) const int dilation_h = 1;
31layout (constant_id = 4) const int stride_w = 1;
32layout (constant_id = 5) const int stride_h = 1;
33layout (constant_id = 6) const int bias_term = 0;
34layout (constant_id = 7) const int activation_type = 0;
35layout (constant_id = 8) const float activation_param_0 = 0;
36layout (constant_id = 9) const float activation_param_1 = 0;
37
38#define shape_constant_id_offset 10
39layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
40layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
41layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
42layout (constant_id = shape_constant_id_offset + 3) const int c = 0;
43layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0;
44
45layout (constant_id = shape_constant_id_offset + 5) const int outdims = 0;
46layout (constant_id = shape_constant_id_offset + 6) const int outw = 0;
47layout (constant_id = shape_constant_id_offset + 7) const int outh = 0;
48layout (constant_id = shape_constant_id_offset + 8) const int outc = 0;
49layout (constant_id = shape_constant_id_offset + 9) const int outcstep = 0;
50
51#if NCNN_image_shader
52layout (binding = 0) uniform unfp sampler3D bottom_blob;
53layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob;
54layout (binding = 2) uniform unfp sampler3D weight_blob;
55layout (binding = 3) uniform unfp sampler3D bias_blob;
56#else
57layout (binding = 0) readonly buffer bottom_blob { sfpvec4 bottom_blob_data[]; };
58layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
59#if NCNN_fp16_packed || (NCNN_fp16_storage && !NCNN_fp16_arithmetic)
60// GL_EXT_shader_16bit_storage does not define f16mat4 type :(
61layout (binding = 2) readonly buffer weight_blob { sfpvec4 weight_data[]; };
62#else
63layout (binding = 2) readonly buffer weight_blob { sfpmat4 weight_data[]; };
64#endif
65layout (binding = 3) readonly buffer bias_blob { sfpvec4 bias_data[]; };
66#endif
67
68layout (push_constant) uniform parameter
69{
70 int dims;
71 int w;
72 int h;
73 int c;
74 int cstep;
75
76 int outdims;
77 int outw;
78 int outh;
79 int outc;
80 int outcstep;
81} p;
82
83void main()
84{
85 int gx = int(gl_GlobalInvocationID.x) * 2;
86 int gy = int(gl_GlobalInvocationID.y) * 2;
87 int gz = int(gl_GlobalInvocationID.z) * 2;
88
89 if (gx >= psc(outw) || gy >= psc(outh) || gz >= psc(outc))
90 return;
91
92 const ivec2 gx2 = gx + ivec2(0, 1);
93 const ivec2 gy2 = gy + ivec2(0, 1);
94 const ivec2 gz2 = gz + ivec2(0, 1);
95
96 afpvec4 sum0;
97 afpvec4 sum1;
98 afpvec4 sum2;
99 afpvec4 sum3;
100 afpvec4 sum4;
101 afpvec4 sum5;
102 afpvec4 sum6;
103 afpvec4 sum7;
104
105 if (bias_term == 1)
106 {
107#if NCNN_image_shader
108 sum0 = image3d_ld4(bias_blob, ivec3(gz2.x, 0, 0));
109 sum4 = image3d_ld4(bias_blob, ivec3(gz2.y, 0, 0));
110#else
111 sum0 = buffer_ld4(bias_data, gz2.x);
112 sum4 = buffer_ld4(bias_data, gz2.y);
113#endif
114 sum1 = sum0;
115 sum2 = sum0;
116 sum3 = sum0;
117 sum5 = sum4;
118 sum6 = sum4;
119 sum7 = sum4;
120 }
121 else
122 {
123 sum0 = afpvec4(0.f);
124 sum1 = afpvec4(0.f);
125 sum2 = afpvec4(0.f);
126 sum3 = afpvec4(0.f);
127 sum4 = afpvec4(0.f);
128 sum5 = afpvec4(0.f);
129 sum6 = afpvec4(0.f);
130 sum7 = afpvec4(0.f);
131 }
132
133#if NCNN_image_shader
134 for (int z = 0; z < psc(c); z++)
135 {
136 ivec2 sy = gy2 * stride_h;
137 int wx = 0;
138
139 for (int y = 0; y < kernel_h; y++)
140 {
141 ivec2 sx = gx2 * stride_w;
142
143 for (int x = 0; x < kernel_w; x++)
144 {
145 afpvec4 v0 = image3d_ld4(bottom_blob, ivec3(sx.x, sy.x, z));
146 afpvec4 v1 = image3d_ld4(bottom_blob, ivec3(sx.y, sy.x, z));
147 afpvec4 v2 = image3d_ld4(bottom_blob, ivec3(sx.x, sy.y, z));
148 afpvec4 v3 = image3d_ld4(bottom_blob, ivec3(sx.y, sy.y, z));
149
150 afpmat4 k0 = afpmat4(
151 image3d_ld4(weight_blob, ivec3(wx + 0, z, gz2.x)),
152 image3d_ld4(weight_blob, ivec3(wx + 1, z, gz2.x)),
153 image3d_ld4(weight_blob, ivec3(wx + 2, z, gz2.x)),
154 image3d_ld4(weight_blob, ivec3(wx + 3, z, gz2.x))
155 );
156 afpmat4 k1 = afpmat4(
157 image3d_ld4(weight_blob, ivec3(wx + 0, z, gz2.y)),
158 image3d_ld4(weight_blob, ivec3(wx + 1, z, gz2.y)),
159 image3d_ld4(weight_blob, ivec3(wx + 2, z, gz2.y)),
160 image3d_ld4(weight_blob, ivec3(wx + 3, z, gz2.y))
161 );
162
163 sum0 += v0 * k0;
164 sum1 += v1 * k0;
165 sum2 += v2 * k0;
166 sum3 += v3 * k0;
167 sum4 += v0 * k1;
168 sum5 += v1 * k1;
169 sum6 += v2 * k1;
170 sum7 += v3 * k1;
171
172 sx += dilation_w;
173 wx += 4;
174 }
175
176 sy += dilation_h;
177 }
178 }
179#else
180 ivec2 w_offset = gz2 * psc(c) * kernel_w * kernel_h;
181
182 for (int z = 0; z < psc(c); z++)
183 {
184 ivec4 v_offset;
185 v_offset.rg = z * psc(cstep) + gy2.x * stride_h * psc(w) + gx2 * stride_w;
186 v_offset.ba = z * psc(cstep) + gy2.y * stride_h * psc(w) + gx2 * stride_w;
187
188 for (int y = 0; y < kernel_h; y++)
189 {
190 for (int x = 0; x < kernel_w; x++)
191 {
192 afpvec4 v0 = buffer_ld4(bottom_blob_data, v_offset.r + x * dilation_w);
193 afpvec4 v1 = buffer_ld4(bottom_blob_data, v_offset.g + x * dilation_w);
194 afpvec4 v2 = buffer_ld4(bottom_blob_data, v_offset.b + x * dilation_w);
195 afpvec4 v3 = buffer_ld4(bottom_blob_data, v_offset.a + x * dilation_w);
196
197#if NCNN_fp16_packed || (NCNN_fp16_storage && !NCNN_fp16_arithmetic)
198 // GL_EXT_shader_16bit_storage does not define f16mat4 type :(
199 afpmat4 k0 = afpmat4(
200 buffer_ld4(weight_data, (w_offset.x + x) * 4 + 0),
201 buffer_ld4(weight_data, (w_offset.x + x) * 4 + 1),
202 buffer_ld4(weight_data, (w_offset.x + x) * 4 + 2),
203 buffer_ld4(weight_data, (w_offset.x + x) * 4 + 3)
204 );
205 afpmat4 k1 = afpmat4(
206 buffer_ld4(weight_data, (w_offset.y + x) * 4 + 0),
207 buffer_ld4(weight_data, (w_offset.y + x) * 4 + 1),
208 buffer_ld4(weight_data, (w_offset.y + x) * 4 + 2),
209 buffer_ld4(weight_data, (w_offset.y + x) * 4 + 3)
210 );
211#else
212 afpmat4 k0 = sfp2afpmat4(weight_data[w_offset.x + x]);
213 afpmat4 k1 = sfp2afpmat4(weight_data[w_offset.y + x]);
214#endif
215
216 sum0 += v0 * k0;
217 sum1 += v1 * k0;
218 sum2 += v2 * k0;
219 sum3 += v3 * k0;
220 sum4 += v0 * k1;
221 sum5 += v1 * k1;
222 sum6 += v2 * k1;
223 sum7 += v3 * k1;
224 }
225
226 v_offset += dilation_h * psc(w);
227 w_offset += kernel_w;
228 }
229 }
230#endif
231
232 sum0 = activation_afpvec4(sum0, activation_type, activation_param_0, activation_param_1);
233 sum1 = activation_afpvec4(sum1, activation_type, activation_param_0, activation_param_1);
234 sum2 = activation_afpvec4(sum2, activation_type, activation_param_0, activation_param_1);
235 sum3 = activation_afpvec4(sum3, activation_type, activation_param_0, activation_param_1);
236 sum4 = activation_afpvec4(sum4, activation_type, activation_param_0, activation_param_1);
237 sum5 = activation_afpvec4(sum5, activation_type, activation_param_0, activation_param_1);
238 sum6 = activation_afpvec4(sum6, activation_type, activation_param_0, activation_param_1);
239 sum7 = activation_afpvec4(sum7, activation_type, activation_param_0, activation_param_1);
240
241#if NCNN_image_shader
242 image3d_st4(top_blob, ivec3(gx2.x, gy2.x, gz2.x), sum0);
243 image3d_st4(top_blob, ivec3(gx2.y, gy2.x, gz2.x), sum1);
244 image3d_st4(top_blob, ivec3(gx2.x, gy2.y, gz2.x), sum2);
245 image3d_st4(top_blob, ivec3(gx2.y, gy2.y, gz2.x), sum3);
246 image3d_st4(top_blob, ivec3(gx2.x, gy2.x, gz2.y), sum4);
247 image3d_st4(top_blob, ivec3(gx2.y, gy2.x, gz2.y), sum5);
248 image3d_st4(top_blob, ivec3(gx2.x, gy2.y, gz2.y), sum6);
249 image3d_st4(top_blob, ivec3(gx2.y, gy2.y, gz2.y), sum7);
250#else
251 const ivec2 gi = gz2 * psc(outcstep) + gy * psc(outw) + gx;
252
253 buffer_st4(top_blob_data, gi.x, sum0);
254 if (gx + 1 < psc(outw)) buffer_st4(top_blob_data, gi.x + 1, sum1);
255 if (gy + 1 < psc(outh)) buffer_st4(top_blob_data, gi.x + psc(outw), sum2);
256 if (gy + 1 < psc(outh) && gx + 1 < psc(outw)) buffer_st4(top_blob_data, gi.x + psc(outw) + 1, sum3);
257 if (gz + 1 < psc(outc))
258 {
259 buffer_st4(top_blob_data, gi.y, sum4);
260 if (gx + 1 < psc(outw)) buffer_st4(top_blob_data, gi.y + 1, sum5);
261 if (gy + 1 < psc(outh)) buffer_st4(top_blob_data, gi.y + psc(outw), sum6);
262 if (gy + 1 < psc(outh) && gx + 1 < psc(outw)) buffer_st4(top_blob_data, gi.y + psc(outw) + 1, sum7);
263 }
264#endif
265}