Elffuss BraidHub

kikocis/ncnn public

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