Elffuss BraidHub

kikocis/ncnn public

master · 6.5 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 { sfp bottom_blob_data[]; };
57layout (binding = 1) writeonly buffer top_blob { sfpvec8 top_blob_data[]; };
58layout (binding = 2) readonly buffer weight_blob { sfpvec8 weight_data[]; };
59layout (binding = 3) readonly buffer bias_blob { sfpvec8 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 afpvec8 sum0;
86 afpvec8 sum1;
87 afpvec8 sum2;
88 afpvec8 sum3;
89
90 if (bias_term == 1)
91 {
92#if NCNN_image_shader
93 sum0 = image3d_ld8(bias_blob, ivec3(gy, 0, 0));
94#else
95 sum0 = buffer_ld8(bias_data, gy);
96#endif
97 sum1 = sum0;
98 sum2 = sum0;
99 sum3 = sum0;
100 }
101 else
102 {
103 sum0 = afpvec8(afpvec4(0.f), afpvec4(0.f));
104 sum1 = afpvec8(afpvec4(0.f), afpvec4(0.f));
105 sum2 = afpvec8(afpvec4(0.f), afpvec4(0.f));
106 sum3 = afpvec8(afpvec4(0.f), 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 afp v0 = image3d_ld1(bottom_blob, ivec3(x4.r, y4.r, sz));
133 afp v1 = image3d_ld1(bottom_blob, ivec3(x4.g, y4.g, sz));
134 afp v2 = image3d_ld1(bottom_blob, ivec3(x4.b, y4.b, sz));
135 afp v3 = image3d_ld1(bottom_blob, ivec3(x4.a, y4.a, sz));
136
137 afpvec8 k = image3d_ld8(weight_blob, ivec3(z, gy, 0));
138
139 // sum += v * k;
140 sum0[0] += v0 * k[0];
141 sum0[1] += v0 * k[1];
142
143 sum1[0] += v1 * k[0];
144 sum1[1] += v1 * k[1];
145
146 sum2[0] += v2 * k[0];
147 sum2[1] += v2 * k[1];
148
149 sum3[0] += v3 * k[0];
150 sum3[1] += v3 * k[1];
151 }
152#else
153 int w_offset = gy * N;
154
155 for (int z = 0; z < N; z++)
156 {
157 const int sz = z / maxk;
158 const int kk = z % maxk;
159
160 const int ky = kk / kernel_w;
161 const int kx = kk % kernel_w;
162
163 const ivec4 v_offset = sz * psc(cstep) + (sys4 + ky * dilation_h) * psc(w) + sxs4 + kx * dilation_w;
164
165 afp v0 = buffer_ld1(bottom_blob_data, v_offset.r);
166 afp v1 = buffer_ld1(bottom_blob_data, v_offset.g);
167 afp v2 = buffer_ld1(bottom_blob_data, v_offset.b);
168 afp v3 = buffer_ld1(bottom_blob_data, v_offset.a);
169
170 afpvec8 k = buffer_ld8(weight_data, w_offset);
171
172 // sum += v * k;
173 sum0[0] += v0 * k[0];
174 sum0[1] += v0 * k[1];
175
176 sum1[0] += v1 * k[0];
177 sum1[1] += v1 * k[1];
178
179 sum2[0] += v2 * k[0];
180 sum2[1] += v2 * k[1];
181
182 sum3[0] += v3 * k[0];
183 sum3[1] += v3 * k[1];
184
185 w_offset += 1;
186 }
187#endif
188
189 sum0 = activation_afpvec8(sum0, activation_type, activation_param_0, activation_param_1);
190 sum1 = activation_afpvec8(sum1, activation_type, activation_param_0, activation_param_1);
191 sum2 = activation_afpvec8(sum2, activation_type, activation_param_0, activation_param_1);
192 sum3 = activation_afpvec8(sum3, activation_type, activation_param_0, activation_param_1);
193
194#if NCNN_image_shader
195 image3d_st8(top_blob, ivec3(sx4.r, sy4.r, gy), sum0);
196 image3d_st8(top_blob, ivec3(sx4.g, sy4.g, gy), sum1);
197 image3d_st8(top_blob, ivec3(sx4.b, sy4.b, gy), sum2);
198 image3d_st8(top_blob, ivec3(sx4.a, sy4.a, gy), sum3);
199#else
200 const int gi = gy * psc(outcstep) + gx;
201
202 buffer_st8(top_blob_data, gi, sum0);
203 if (gx + 1 < outsize) buffer_st8(top_blob_data, gi + 1, sum1);
204 if (gx + 2 < outsize) buffer_st8(top_blob_data, gi + 2, sum2);
205 if (gx + 3 < outsize) buffer_st8(top_blob_data, gi + 3, sum3);
206#endif
207}