Elffuss BraidHub

kikocis/ncnn public

master · 5.3 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2023 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 dilation_w = 1;
29layout (constant_id = 2) const int stride_w = 1;
30layout (constant_id = 3) const int bias_term = 0;
31layout (constant_id = 4) const int activation_type = 0;
32layout (constant_id = 5) const float activation_param_0 = 0;
33layout (constant_id = 6) const float activation_param_1 = 0;
34
35#define shape_constant_id_offset 7
36layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
37layout (constant_id = shape_constant_id_offset + 1) const int h = 0;
38
39layout (constant_id = shape_constant_id_offset + 2) const int outw = 0;
40layout (constant_id = shape_constant_id_offset + 3) const int outh = 0;
41
42#if NCNN_image_shader
43layout (binding = 0) uniform unfp sampler3D bottom_blob;
44layout (binding = 1, imfmtc1) writeonly uniform unfp image3D top_blob;
45layout (binding = 2) uniform unfp sampler3D weight_blob;
46layout (binding = 3) uniform unfp sampler3D bias_blob;
47#else
48layout (binding = 0) readonly buffer bottom_blob { sfpvec4 bottom_blob_data[]; };
49layout (binding = 1) writeonly buffer top_blob { sfp top_blob_data[]; };
50layout (binding = 2) readonly buffer weight_blob { sfpvec4 weight_data[]; };
51layout (binding = 3) readonly buffer bias_blob { sfp bias_data[]; };
52#endif
53
54layout (push_constant) uniform parameter
55{
56 int w;
57 int h;
58
59 int outw;
60 int outh;
61} p;
62
63void main()
64{
65 int gx = int(gl_GlobalInvocationID.x) * 2;
66 int gy = int(gl_GlobalInvocationID.y) * 2;
67
68 if (gx >= psc(outw) || gy >= psc(outh))
69 return;
70
71 const ivec2 gx2 = gx + ivec2(0, 1);
72 const ivec2 gy2 = gy + ivec2(0, 1);
73
74 afp sum0 = afp(0.0f);
75 afp sum1 = afp(0.0f);
76 afp sum2 = afp(0.0f);
77 afp sum3 = afp(0.0f);
78
79 if (bias_term == 1)
80 {
81#if NCNN_image_shader
82 sum0 = image3d_ld1(bias_blob, ivec3(gy2.x, 0, 0));
83 sum2 = image3d_ld1(bias_blob, ivec3(gy2.y, 0, 0));
84#else
85 sum0 = buffer_ld1(bias_data, gy2.x);
86 sum2 = buffer_ld1(bias_data, gy2.y);
87#endif
88 sum1 = sum0;
89 sum3 = sum2;
90 }
91
92#if NCNN_image_shader
93
94 ivec2 v_offset = gx2 * stride_w;
95
96 for (int y = 0; y < psc(h); y++)
97 {
98 int wx = 0;
99
100 for (int x = 0; x < kernel_w; x++)
101 {
102 afpvec4 v0 = image3d_ld4(bottom_blob, ivec3(v_offset.x + x * dilation_w, y, 0));
103 afpvec4 v1 = image3d_ld4(bottom_blob, ivec3(v_offset.y + x * dilation_w, y, 0));
104
105 afpvec4 k0 = image3d_ld4(weight_blob, ivec3(wx, y, gy2.x));
106 afpvec4 k1 = image3d_ld4(weight_blob, ivec3(wx, y, gy2.y));
107
108 sum0 += dot(v0, k0);
109 sum1 += dot(v1, k0);
110 sum2 += dot(v0, k1);
111 sum3 += dot(v1, k1);
112
113 wx += 1;
114 }
115 }
116
117#else
118
119 ivec2 v_offset = gx2 * stride_w;
120 ivec2 w_offset = gy2 * psc(h) * kernel_w;
121
122 for (int y = 0; y < psc(h); y++)
123 {
124 for (int x = 0; x < kernel_w; x++)
125 {
126 afpvec4 v0 = buffer_ld4(bottom_blob_data, v_offset.x + x * dilation_w);
127 afpvec4 v1 = buffer_ld4(bottom_blob_data, v_offset.y + x * dilation_w);
128
129 afpvec4 k0 = buffer_ld4(weight_data, w_offset.x + x);
130 afpvec4 k1 = buffer_ld4(weight_data, w_offset.y + x);
131
132 sum0 += dot(v0, k0);
133 sum1 += dot(v1, k0);
134 sum2 += dot(v0, k1);
135 sum3 += dot(v1, k1);
136 }
137 v_offset += psc(w);
138 w_offset += kernel_w;
139 }
140
141#endif
142
143 sum0 = activation_afp(sum0, activation_type, activation_param_0, activation_param_1);
144 sum1 = activation_afp(sum1, activation_type, activation_param_0, activation_param_1);
145 sum2 = activation_afp(sum2, activation_type, activation_param_0, activation_param_1);
146 sum3 = activation_afp(sum3, activation_type, activation_param_0, activation_param_1);
147
148#if NCNN_image_shader
149
150 image3d_st1(top_blob, ivec3(gx2.x, gy2.x, 0), sum0);
151 image3d_st1(top_blob, ivec3(gx2.y, gy2.x, 0), sum1);
152 image3d_st1(top_blob, ivec3(gx2.x, gy2.y, 0), sum2);
153 image3d_st1(top_blob, ivec3(gx2.y, gy2.y, 0), sum3);
154
155#else
156
157 const int gi = gy * psc(outw) + gx;
158
159 buffer_st1(top_blob_data, gi, sum0);
160 if (gx + 1 < psc(outw)) buffer_st1(top_blob_data, gi + 1, sum1);
161 if (gy + 1 < psc(outh)) buffer_st1(top_blob_data, gi + psc(outw), sum2);
162 if (gy + 1 < psc(outh) && gx + 1 < psc(outw)) buffer_st1(top_blob_data, gi + psc(outw) + 1, sum3);
163
164#endif
165}