Elffuss BraidHub

kikocis/ncnn public

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