Elffuss BraidHub

kikocis/ncnn public

master · 8.2 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 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 w = 0;
41layout (constant_id = shape_constant_id_offset + 1) const int h = 0;
42layout (constant_id = shape_constant_id_offset + 2) const int c = 0;
43layout (constant_id = shape_constant_id_offset + 3) const int cstep = 0;
44
45layout (constant_id = shape_constant_id_offset + 4) const int outw = 0;
46layout (constant_id = shape_constant_id_offset + 5) const int outh = 0;
47layout (constant_id = shape_constant_id_offset + 6) const int outc = 0;
48layout (constant_id = shape_constant_id_offset + 7) const int outcstep = 0;
49
50#if NCNN_image_shader
51layout (binding = 0) uniform unfp sampler3D bottom_blob;
52layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob;
53layout (binding = 2) uniform unfp sampler3D weight_blob;
54layout (binding = 3) uniform unfp sampler3D bias_blob;
55#else
56layout (binding = 0) readonly buffer bottom_blob { sfpvec8 bottom_blob_data[]; };
57layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
58layout (binding = 2) readonly buffer weight_blob { sfpvec8 weight_data[]; };
59layout (binding = 3) readonly buffer bias_blob { sfpvec4 bias_data[]; };
60#endif
61
62layout (push_constant) uniform parameter
63{
64 int w;
65 int h;
66 int c;
67 int cstep;
68
69 int outw;
70 int outh;
71 int outc;
72 int outcstep;
73} p;
74
75void main()
76{
77 int gx = int(gl_GlobalInvocationID.x) * 4;
78 int gy = int(gl_GlobalInvocationID.y);
79
80 const int outsize = psc(outw) * psc(outh);
81
82 if (gx >= outsize || gy >= psc(outc))
83 return;
84
85 afpvec4 sum0;
86 afpvec4 sum1;
87 afpvec4 sum2;
88 afpvec4 sum3;
89
90 if (bias_term == 1)
91 {
92#if NCNN_image_shader
93 sum0 = image3d_ld4(bias_blob, ivec3(gy, 0, 0));
94#else
95 sum0 = buffer_ld4(bias_data, gy);
96#endif
97 sum1 = sum0;
98 sum2 = sum0;
99 sum3 = sum0;
100 }
101 else
102 {
103 sum0 = afpvec4(0.f);
104 sum1 = afpvec4(0.f);
105 sum2 = afpvec4(0.f);
106 sum3 = afpvec4(0.f);
107 }
108
109 const int maxk = kernel_w * kernel_h;
110 const int N = psc(c) * maxk;
111
112 const ivec4 gx4 = gx + ivec4(0, 1, 2, 3);
113
114 const ivec4 sy4 = gx4 / psc(outw);
115 const ivec4 sx4 = gx4 % psc(outw);
116
117 const ivec4 sxs4 = sx4 * stride_w;
118 const ivec4 sys4 = sy4 * stride_h;
119
120#if NCNN_image_shader
121 for (int z = 0; z < N; z++)
122 {
123 const int sz = z / maxk;
124 const int kk = z % maxk;
125
126 const int ky = kk / kernel_w;
127 const int kx = kk % kernel_w;
128
129 const ivec4 x4 = sxs4 + kx * dilation_w;
130 const ivec4 y4 = sys4 + ky * dilation_h;
131
132 afpvec8 v0 = image3d_ld8(bottom_blob, ivec3(x4.r, y4.r, sz));
133 afpvec8 v1 = image3d_ld8(bottom_blob, ivec3(x4.g, y4.g, sz));
134 afpvec8 v2 = image3d_ld8(bottom_blob, ivec3(x4.b, y4.b, sz));
135 afpvec8 v3 = image3d_ld8(bottom_blob, ivec3(x4.a, y4.a, sz));
136
137 afpvec8 k0 = image3d_ld8(weight_blob, ivec3(z * 4 + 0, gy, 0));
138 afpvec8 k1 = image3d_ld8(weight_blob, ivec3(z * 4 + 1, gy, 0));
139 afpvec8 k2 = image3d_ld8(weight_blob, ivec3(z * 4 + 2, gy, 0));
140 afpvec8 k3 = image3d_ld8(weight_blob, ivec3(z * 4 + 3, gy, 0));
141
142 // sum += v * k;
143 sum0.r += dot(v0[0], k0[0]) + dot(v0[1], k0[1]);
144 sum0.g += dot(v0[0], k1[0]) + dot(v0[1], k1[1]);
145 sum0.b += dot(v0[0], k2[0]) + dot(v0[1], k2[1]);
146 sum0.a += dot(v0[0], k3[0]) + dot(v0[1], k3[1]);
147
148 sum1.r += dot(v1[0], k0[0]) + dot(v1[1], k0[1]);
149 sum1.g += dot(v1[0], k1[0]) + dot(v1[1], k1[1]);
150 sum1.b += dot(v1[0], k2[0]) + dot(v1[1], k2[1]);
151 sum1.a += dot(v1[0], k3[0]) + dot(v1[1], k3[1]);
152
153 sum2.r += dot(v2[0], k0[0]) + dot(v2[1], k0[1]);
154 sum2.g += dot(v2[0], k1[0]) + dot(v2[1], k1[1]);
155 sum2.b += dot(v2[0], k2[0]) + dot(v2[1], k2[1]);
156 sum2.a += dot(v2[0], k3[0]) + dot(v2[1], k3[1]);
157
158 sum3.r += dot(v3[0], k0[0]) + dot(v3[1], k0[1]);
159 sum3.g += dot(v3[0], k1[0]) + dot(v3[1], k1[1]);
160 sum3.b += dot(v3[0], k2[0]) + dot(v3[1], k2[1]);
161 sum3.a += dot(v3[0], k3[0]) + dot(v3[1], k3[1]);
162 }
163#else
164 int w_offset = gy * N * 4;
165
166 for (int z = 0; z < N; z++)
167 {
168 const int sz = z / maxk;
169 const int kk = z % maxk;
170
171 const int ky = kk / kernel_w;
172 const int kx = kk % kernel_w;
173
174 const ivec4 v_offset = sz * psc(cstep) + (sys4 + ky * dilation_h) * psc(w) + sxs4 + kx * dilation_w;
175
176 afpvec8 v0 = buffer_ld8(bottom_blob_data, v_offset.r);
177 afpvec8 v1 = buffer_ld8(bottom_blob_data, v_offset.g);
178 afpvec8 v2 = buffer_ld8(bottom_blob_data, v_offset.b);
179 afpvec8 v3 = buffer_ld8(bottom_blob_data, v_offset.a);
180
181 afpvec8 k0 = buffer_ld8(weight_data, w_offset + 0);
182 afpvec8 k1 = buffer_ld8(weight_data, w_offset + 1);
183 afpvec8 k2 = buffer_ld8(weight_data, w_offset + 2);
184 afpvec8 k3 = buffer_ld8(weight_data, w_offset + 3);
185
186 // sum += v * k;
187 sum0.r += dot(v0[0], k0[0]) + dot(v0[1], k0[1]);
188 sum0.g += dot(v0[0], k1[0]) + dot(v0[1], k1[1]);
189 sum0.b += dot(v0[0], k2[0]) + dot(v0[1], k2[1]);
190 sum0.a += dot(v0[0], k3[0]) + dot(v0[1], k3[1]);
191
192 sum1.r += dot(v1[0], k0[0]) + dot(v1[1], k0[1]);
193 sum1.g += dot(v1[0], k1[0]) + dot(v1[1], k1[1]);
194 sum1.b += dot(v1[0], k2[0]) + dot(v1[1], k2[1]);
195 sum1.a += dot(v1[0], k3[0]) + dot(v1[1], k3[1]);
196
197 sum2.r += dot(v2[0], k0[0]) + dot(v2[1], k0[1]);
198 sum2.g += dot(v2[0], k1[0]) + dot(v2[1], k1[1]);
199 sum2.b += dot(v2[0], k2[0]) + dot(v2[1], k2[1]);
200 sum2.a += dot(v2[0], k3[0]) + dot(v2[1], k3[1]);
201
202 sum3.r += dot(v3[0], k0[0]) + dot(v3[1], k0[1]);
203 sum3.g += dot(v3[0], k1[0]) + dot(v3[1], k1[1]);
204 sum3.b += dot(v3[0], k2[0]) + dot(v3[1], k2[1]);
205 sum3.a += dot(v3[0], k3[0]) + dot(v3[1], k3[1]);
206
207 w_offset += 4;
208 }
209#endif
210
211 sum0 = activation_afpvec4(sum0, activation_type, activation_param_0, activation_param_1);
212 sum1 = activation_afpvec4(sum1, activation_type, activation_param_0, activation_param_1);
213 sum2 = activation_afpvec4(sum2, activation_type, activation_param_0, activation_param_1);
214 sum3 = activation_afpvec4(sum3, activation_type, activation_param_0, activation_param_1);
215
216#if NCNN_image_shader
217 image3d_st4(top_blob, ivec3(sx4.r, sy4.r, gy), sum0);
218 image3d_st4(top_blob, ivec3(sx4.g, sy4.g, gy), sum1);
219 image3d_st4(top_blob, ivec3(sx4.b, sy4.b, gy), sum2);
220 image3d_st4(top_blob, ivec3(sx4.a, sy4.a, gy), sum3);
221#else
222 const int gi = gy * psc(outcstep) + gx;
223
224 buffer_st4(top_blob_data, gi, sum0);
225 if (gx + 1 < outsize) buffer_st4(top_blob_data, gi + 1, sum1);
226 if (gx + 2 < outsize) buffer_st4(top_blob_data, gi + 2, sum2);
227 if (gx + 3 < outsize) buffer_st4(top_blob_data, gi + 3, sum3);
228#endif
229}