Elffuss BraidHub

kikocis/ncnn public

master · 6.3 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
19#endif
20#if NCNN_fp16_arithmetic
21#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
22#endif
23
24#define LOCAL_MEMORY_UNROLL_INCH 8
25
26layout (constant_id = 0) const int maxk = 1;
27
28#define shape_constant_id_offset 1
29layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
30layout (constant_id = shape_constant_id_offset + 1) const int h = 0;
31layout (constant_id = shape_constant_id_offset + 2) const int c = 0;
32layout (constant_id = shape_constant_id_offset + 3) const int cstep = 0;
33
34layout (constant_id = shape_constant_id_offset + 4) const int outw = 0;
35layout (constant_id = shape_constant_id_offset + 5) const int outh = 0;
36
37#if NCNN_image_shader
38layout (binding = 0) uniform unfp sampler3D bottom_blob;
39layout (binding = 1, imfmtc1) writeonly uniform unfp image3D col_blob;
40layout (binding = 2) uniform unfp sampler3D weight_blob;
41#else
42layout (binding = 0) readonly buffer bottom_blob { sfp bottom_blob_data[]; };
43layout (binding = 1) writeonly buffer col_blob { sfp col_blob_data[]; };
44layout (binding = 2) readonly buffer weight_blob { sfp weight_data[]; };
45#endif
46
47layout (push_constant) uniform parameter
48{
49 int w;
50 int h;
51 int c;
52 int cstep;
53
54 int outw;
55 int outh;
56} p;
57
58#if NCNN_shader_local_memory
59shared lfp tmp_v[8][LOCAL_MEMORY_UNROLL_INCH][4];
60shared lfp tmp_k[8][LOCAL_MEMORY_UNROLL_INCH];
61#endif
62
63void main()
64{
65 int gx = int(gl_GlobalInvocationID.x) * 4;
66 int gy = int(gl_GlobalInvocationID.y);
67
68#if !NCNN_shader_local_memory
69 if (gx >= psc(outw) || gy >= psc(outh))
70 return;
71#endif
72
73 afp sum0 = afp(0.f);
74 afp sum1 = afp(0.f);
75 afp sum2 = afp(0.f);
76 afp sum3 = afp(0.f);
77
78#if NCNN_image_shader
79 ivec4 gx4 = gx + ivec4(0, 1, 2, 3);
80
81 ivec4 sy4 = gx4 / psc(w);
82 ivec4 sx4 = gx4 % psc(w);
83
84 for (int z = 0; z < psc(c); z++)
85 {
86 afp v0 = image3d_ld1(bottom_blob, ivec3(sx4.r, sy4.r, z));
87 afp v1 = image3d_ld1(bottom_blob, ivec3(sx4.g, sy4.g, z));
88 afp v2 = image3d_ld1(bottom_blob, ivec3(sx4.b, sy4.b, z));
89 afp v3 = image3d_ld1(bottom_blob, ivec3(sx4.a, sy4.a, z));
90
91 afp k = image3d_ld1(weight_blob, ivec3(z, gy, 0));
92
93 sum0 += v0 * k;
94 sum1 += v1 * k;
95 sum2 += v2 * k;
96 sum3 += v3 * k;
97 }
98#else
99 int v_offset = gx;
100 int w_offset = gy * psc(c);
101
102#if NCNN_shader_local_memory
103 const int lx = int(gl_LocalInvocationID.x);
104 const int ly = int(gl_LocalInvocationID.y);
105
106 int z = 0;
107 for (; z + (LOCAL_MEMORY_UNROLL_INCH - 1) < psc(c); z += LOCAL_MEMORY_UNROLL_INCH)
108 {
109 if (ly < 4)
110 {
111 for (int z4 = 0; z4 < LOCAL_MEMORY_UNROLL_INCH; z4++)
112 {
113 tmp_v[lx][z4][ly] = sfp2lfp(bottom_blob_data[v_offset + z4 * psc(cstep) + ly]);
114 }
115 }
116
117 if (lx == 0)
118 {
119 for (int z4 = 0; z4 < LOCAL_MEMORY_UNROLL_INCH; z4++)
120 {
121 tmp_k[ly][z4] = sfp2lfp(weight_data[w_offset + z4]);
122 }
123 }
124
125 barrier();
126
127 for (int z4 = 0; z4 < LOCAL_MEMORY_UNROLL_INCH; z4++)
128 {
129 afp v0 = lfp2afp(tmp_v[lx][z4][0]);
130 afp v1 = lfp2afp(tmp_v[lx][z4][1]);
131 afp v2 = lfp2afp(tmp_v[lx][z4][2]);
132 afp v3 = lfp2afp(tmp_v[lx][z4][3]);
133
134 afp k = lfp2afp(tmp_k[ly][z4]);
135
136 sum0 += v0 * k;
137 sum1 += v1 * k;
138 sum2 += v2 * k;
139 sum3 += v3 * k;
140 }
141
142 v_offset += LOCAL_MEMORY_UNROLL_INCH * psc(cstep);
143 w_offset += LOCAL_MEMORY_UNROLL_INCH;
144
145 barrier();
146 }
147
148 if (z < psc(c))
149 {
150 const int remain = psc(c) - z;
151
152 if (ly < 4)
153 {
154 for (int z4 = 0; z4 < remain; z4++)
155 {
156 tmp_v[lx][z4][ly] = sfp2lfp(bottom_blob_data[v_offset + z4 * psc(cstep) + ly]);
157 }
158 }
159
160 if (lx == 0)
161 {
162 for (int z4 = 0; z4 < remain; z4++)
163 {
164 tmp_k[ly][z4] = sfp2lfp(weight_data[w_offset + z4]);
165 }
166 }
167
168 barrier();
169
170 for (int z4 = 0; z4 < remain; z4++)
171 {
172 afp v0 = lfp2afp(tmp_v[lx][z4][0]);
173 afp v1 = lfp2afp(tmp_v[lx][z4][1]);
174 afp v2 = lfp2afp(tmp_v[lx][z4][2]);
175 afp v3 = lfp2afp(tmp_v[lx][z4][3]);
176
177 afp k = lfp2afp(tmp_k[ly][z4]);
178
179 sum0 += v0 * k;
180 sum1 += v1 * k;
181 sum2 += v2 * k;
182 sum3 += v3 * k;
183 }
184 }
185#else
186 for (int z = 0; z < psc(c); z++)
187 {
188 afp v0 = buffer_ld1(bottom_blob_data, v_offset + 0);
189 afp v1 = buffer_ld1(bottom_blob_data, v_offset + 1);
190 afp v2 = buffer_ld1(bottom_blob_data, v_offset + 2);
191 afp v3 = buffer_ld1(bottom_blob_data, v_offset + 3);
192
193 afp k = buffer_ld1(weight_data, w_offset);
194
195 sum0 += v0 * k;
196 sum1 += v1 * k;
197 sum2 += v2 * k;
198 sum3 += v3 * k;
199
200 v_offset += psc(cstep);
201 w_offset += 1;
202 }
203#endif
204#endif
205
206#if NCNN_shader_local_memory
207 if (gx >= psc(outw) || gy >= psc(outh))
208 return;
209#endif
210
211#if NCNN_image_shader
212 image3d_st1(col_blob, ivec3(gx4.r, gy, 0), sum0);
213 image3d_st1(col_blob, ivec3(gx4.g, gy, 0), sum1);
214 image3d_st1(col_blob, ivec3(gx4.b, gy, 0), sum2);
215 image3d_st1(col_blob, ivec3(gx4.a, gy, 0), sum3);
216#else
217 const int gi = gy * psc(outw) + gx;
218
219 buffer_st1(col_blob_data, gi, sum0);
220 if (gx + 1 < psc(outw)) buffer_st1(col_blob_data, gi + 1, sum1);
221 if (gx + 2 < psc(outw)) buffer_st1(col_blob_data, gi + 2, sum2);
222 if (gx + 3 < psc(outw)) buffer_st1(col_blob_data, gi + 3, sum3);
223#endif
224}