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