Elffuss BraidHub

kikocis/ncnn public

master · 6.6 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 { sfpvec4 bottom_blob_data[]; };
58layout (binding = 1) writeonly buffer top_blob { sfpvec4 top_blob_data[]; };
59#if NCNN_fp16_packed || (NCNN_fp16_storage && !NCNN_fp16_arithmetic)
60// GL_EXT_shader_16bit_storage does not define f16mat4 type :(
61layout (binding = 2) readonly buffer weight_blob { sfpvec4 weight_data[]; };
62#else
63layout (binding = 2) readonly buffer weight_blob { sfpmat4 weight_data[]; };
64#endif
65layout (binding = 3) readonly buffer bias_blob { sfpvec4 bias_data[]; };
66#endif
67
68layout (push_constant) uniform parameter
69{
70 int dims;
71 int w;
72 int h;
73 int c;
74 int cstep;
75
76 int outdims;
77 int outw;
78 int outh;
79 int outc;
80 int outcstep;
81} p;
82
83void main()
84{
85 int gx = int(gl_GlobalInvocationID.x);
86 int gy = int(gl_GlobalInvocationID.y);
87 int gz = int(gl_GlobalInvocationID.z);
88
89 if (gx >= psc(outw) || gy >= psc(outh) || gz >= psc(outc))
90 return;
91
92 afpvec4 sum;
93
94 if (bias_term == 1)
95 {
96#if NCNN_image_shader
97 sum = image3d_ld4(bias_blob, ivec3(gz, 0, 0));
98#else
99 sum = buffer_ld4(bias_data, gz);
100#endif
101 }
102 else
103 {
104 sum = afpvec4(0.f);
105 }
106
107 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
108 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
109
110#if NCNN_image_shader
111 for (int y = 0; y < kernel_h; y++)
112 {
113 int sys = (gy + y * dilation_h - (kernel_extent_h - 1));
114 if (sys < 0 || sys % stride_h != 0)
115 continue;
116
117 int sy = sys / stride_h;
118 if (sy >= psc(h))
119 continue;
120
121 for (int x = 0; x < kernel_w; x++)
122 {
123 int sxs = (gx + x * dilation_w - (kernel_extent_w - 1));
124 if (sxs < 0 || sxs % stride_w != 0)
125 continue;
126
127 int sx = sxs / stride_w;
128 if (sx >= psc(w))
129 continue;
130
131 int wx = (y * kernel_w + x) * 4;
132
133 for (int z = 0; z < psc(c); z++)
134 {
135 afpvec4 v = image3d_ld4(bottom_blob, ivec3(sx, sy, z));
136
137 afpmat4 k = afpmat4(
138 image3d_ld4(weight_blob, ivec3(wx + 0, z, gz)),
139 image3d_ld4(weight_blob, ivec3(wx + 1, z, gz)),
140 image3d_ld4(weight_blob, ivec3(wx + 2, z, gz)),
141 image3d_ld4(weight_blob, ivec3(wx + 3, z, gz))
142 );
143
144 sum += v * k;
145 }
146 }
147 }
148#else
149 int w_offset_0 = gz * psc(c) * kernel_w * kernel_h;
150
151 for (int y = 0; y < kernel_h; y++)
152 {
153 int sys = (gy + y * dilation_h - (kernel_extent_h - 1));
154 if (sys < 0 || sys % stride_h != 0)
155 continue;
156
157 int sy = sys / stride_h;
158 if (sy >= psc(h))
159 continue;
160
161 for (int x = 0; x < kernel_w; x++)
162 {
163 int sxs = (gx + x * dilation_w - (kernel_extent_w - 1));
164 if (sxs < 0 || sxs % stride_w != 0)
165 continue;
166
167 int sx = sxs / stride_w;
168 if (sx >= psc(w))
169 continue;
170
171 int v_offset = sy * psc(w) + sx;
172 int w_offset = w_offset_0 + y * kernel_w + x;
173
174 for (int z = 0; z < psc(c); z++)
175 {
176 afpvec4 v = buffer_ld4(bottom_blob_data, v_offset);
177
178#if NCNN_fp16_packed || (NCNN_fp16_storage && !NCNN_fp16_arithmetic)
179 // GL_EXT_shader_16bit_storage does not define f16mat4 type :(
180 afpmat4 k = afpmat4(
181 buffer_ld4(weight_data, w_offset * 4 + 0),
182 buffer_ld4(weight_data, w_offset * 4 + 1),
183 buffer_ld4(weight_data, w_offset * 4 + 2),
184 buffer_ld4(weight_data, w_offset * 4 + 3)
185 );
186#else
187 afpmat4 k = afpmat4(weight_data[w_offset]);
188#endif
189
190 sum += v * k;
191
192 v_offset += psc(cstep);
193 w_offset += kernel_w * kernel_h;
194 }
195 }
196 }
197#endif
198
199 sum = activation_afpvec4(sum, activation_type, activation_param_0, activation_param_1);
200
201#if NCNN_image_shader
202 image3d_st4(top_blob, ivec3(gx, gy, gz), sum);
203#else
204 const int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
205
206 buffer_st4(top_blob_data, gi, sum);
207#endif
208}