Elffuss BraidHub

kikocis/ncnn public

master · 4.4 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;
42
43layout (constant_id = shape_constant_id_offset + 2) const int outw = 0;
44layout (constant_id = shape_constant_id_offset + 3) const int outh = 0;
45layout (constant_id = shape_constant_id_offset + 4) const int outc = 0;
46layout (constant_id = shape_constant_id_offset + 5) const int outcstep = 0;
47
48#if NCNN_image_shader
49layout (binding = 0) uniform unfp sampler3D col_blob;
50layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob;
51layout (binding = 2) uniform unfp sampler3D bias_blob;
52#else
53layout (binding = 0) readonly buffer col_blob { sfpvec8 col_blob_data[]; };
54layout (binding = 1) writeonly buffer top_blob { sfpvec8 top_blob_data[]; };
55layout (binding = 2) readonly buffer bias_blob { sfpvec8 bias_data[]; };
56#endif
57
58layout (push_constant) uniform parameter
59{
60 int w;
61 int h;
62
63 int outw;
64 int outh;
65 int outc;
66 int outcstep;
67} p;
68
69void main()
70{
71 int gx = int(gl_GlobalInvocationID.x);
72 int gy = int(gl_GlobalInvocationID.y);
73 int gz = int(gl_GlobalInvocationID.z);
74
75 if (gx >= psc(outw) || gy >= psc(outh) || gz >= psc(outc))
76 return;
77
78 afpvec8 sum;
79
80 if (bias_term == 1)
81 {
82#if NCNN_image_shader
83 sum = image3d_ld8(bias_blob, ivec3(gz, 0, 0));
84#else
85 sum = buffer_ld8(bias_data, gz);
86#endif
87 }
88 else
89 {
90 sum = afpvec8(afpvec4(0.f), afpvec4(0.f));
91 }
92
93 const int maxk = kernel_w * kernel_h;
94
95 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
96 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
97
98 const int sx_start = (gx < kernel_extent_w) ? 0 : (gx - kernel_extent_w) / stride_w + 1;
99 const int sx_end = min(gx / stride_w + 1, psc(w));
100
101 const int sy_start = (gy < kernel_extent_h) ? 0 : (gy - kernel_extent_h) / stride_h + 1;
102 const int sy_end = min(gy / stride_h + 1, psc(h));
103
104 for (int sy = sy_start; sy < sy_end; sy += 1)
105 {
106 for (int sx = sx_start; sx < sx_end; sx += 1)
107 {
108 int h_k = (gy - sy * stride_h);
109 int w_k = (gx - sx * stride_w);
110
111 if (h_k % dilation_h == 0 && w_k % dilation_w == 0)
112 {
113 h_k /= dilation_h;
114 w_k /= dilation_w;
115
116#if NCNN_image_shader
117 sum += image3d_ld8(col_blob, ivec3(sy * psc(w) + sx, gz * maxk + h_k * kernel_w + w_k, 0));
118#else
119 const int gi = (gz * maxk + h_k * kernel_w + w_k) * psc(w) * psc(h) + sy * psc(w) + sx;
120
121 sum += buffer_ld8(col_blob_data, gi);
122#endif
123 }
124 }
125 }
126
127 sum = activation_afpvec8(sum, activation_type, activation_param_0, activation_param_1);
128
129#if NCNN_image_shader
130 image3d_st8(top_blob, ivec3(gx, gy, gz), sum);
131#else
132 const int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
133
134 buffer_st8(top_blob_data, gi, sum);
135#endif
136}