1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
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
8
// https://opensource.org/licenses/BSD-3-Clause
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.
18
#extension GL_EXT_shader_16bit_storage: require
19
struct sfpvec8 { f16vec4 abcd; f16vec4 efgh; };
21
#if NCNN_fp16_arithmetic
22
#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
25
#extension GL_GOOGLE_include_directive: enable
26
#include "vulkan_activation.comp"
28
layout (constant_id = 0) const int kernel_w = 1;
29
layout (constant_id = 1) const int kernel_h = 1;
30
layout (constant_id = 2) const int dilation_w = 1;
31
layout (constant_id = 3) const int dilation_h = 1;
32
layout (constant_id = 4) const int stride_w = 1;
33
layout (constant_id = 5) const int stride_h = 1;
34
layout (constant_id = 6) const int bias_term = 0;
35
layout (constant_id = 7) const int activation_type = 0;
36
layout (constant_id = 8) const float activation_param_0 = 0;
37
layout (constant_id = 9) const float activation_param_1 = 0;
39
#define shape_constant_id_offset 10
40
layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
41
layout (constant_id = shape_constant_id_offset + 1) const int h = 0;
42
layout (constant_id = shape_constant_id_offset + 2) const int c = 0;
43
layout (constant_id = shape_constant_id_offset + 3) const int cstep = 0;
45
layout (constant_id = shape_constant_id_offset + 4) const int outw = 0;
46
layout (constant_id = shape_constant_id_offset + 5) const int outh = 0;
47
layout (constant_id = shape_constant_id_offset + 6) const int outc = 0;
48
layout (constant_id = shape_constant_id_offset + 7) const int outcstep = 0;
51
layout (binding = 0) uniform unfp sampler3D bottom_blob;
52
layout (binding = 1, imfmtc1) writeonly uniform unfp image3D top_blob;
53
layout (binding = 2) uniform unfp sampler3D weight_blob;
54
layout (binding = 3) uniform unfp sampler3D bias_blob;
56
layout (binding = 0) readonly buffer bottom_blob { sfpvec8 bottom_blob_data[]; };
57
layout (binding = 1) writeonly buffer top_blob { sfp top_blob_data[]; };
58
layout (binding = 2) readonly buffer weight_blob { sfpvec8 weight_data[]; };
59
layout (binding = 3) readonly buffer bias_blob { sfp bias_data[]; };
62
layout (push_constant) uniform parameter
77
int gx = int(gl_GlobalInvocationID.x) * 4;
78
int gy = int(gl_GlobalInvocationID.y);
80
const int outsize = psc(outw) * psc(outh);
82
if (gx >= outsize || gy >= psc(outc))
93
sum0 = image3d_ld1(bias_blob, ivec3(gy, 0, 0));
95
sum0 = buffer_ld1(bias_data, gy);
109
const int maxk = kernel_w * kernel_h;
110
const int N = psc(c) * maxk;
112
const ivec4 gx4 = gx + ivec4(0, 1, 2, 3);
114
const ivec4 sy4 = gx4 / psc(outw);
115
const ivec4 sx4 = gx4 % psc(outw);
117
const ivec4 sxs4 = sx4 * stride_w;
118
const ivec4 sys4 = sy4 * stride_h;
121
for (int z = 0; z < N; z++)
123
const int sz = z / maxk;
124
const int kk = z % maxk;
126
const int ky = kk / kernel_w;
127
const int kx = kk % kernel_w;
129
const ivec4 x4 = sxs4 + kx * dilation_w;
130
const ivec4 y4 = sys4 + ky * dilation_h;
132
afpvec8 v0 = image3d_ld8(bottom_blob, ivec3(x4.r, y4.r, sz));
133
afpvec8 v1 = image3d_ld8(bottom_blob, ivec3(x4.g, y4.g, sz));
134
afpvec8 v2 = image3d_ld8(bottom_blob, ivec3(x4.b, y4.b, sz));
135
afpvec8 v3 = image3d_ld8(bottom_blob, ivec3(x4.a, y4.a, sz));
137
afpvec8 k = image3d_ld8(weight_blob, ivec3(z, gy, 0));
140
sum0 += dot(v0[0], k[0]) + dot(v0[1], k[1]);
141
sum1 += dot(v1[0], k[0]) + dot(v1[1], k[1]);
142
sum2 += dot(v2[0], k[0]) + dot(v2[1], k[1]);
143
sum3 += dot(v3[0], k[0]) + dot(v3[1], k[1]);
146
int w_offset = gy * N;
148
for (int z = 0; z < N; z++)
150
const int sz = z / maxk;
151
const int kk = z % maxk;
153
const int ky = kk / kernel_w;
154
const int kx = kk % kernel_w;
156
const ivec4 v_offset = sz * psc(cstep) + (sys4 + ky * dilation_h) * psc(w) + sxs4 + kx * dilation_w;
158
afpvec8 v0 = buffer_ld8(bottom_blob_data, v_offset.r);
159
afpvec8 v1 = buffer_ld8(bottom_blob_data, v_offset.g);
160
afpvec8 v2 = buffer_ld8(bottom_blob_data, v_offset.b);
161
afpvec8 v3 = buffer_ld8(bottom_blob_data, v_offset.a);
163
afpvec8 k = buffer_ld8(weight_data, w_offset);
166
sum0 += dot(v0[0], k[0]) + dot(v0[1], k[1]);
167
sum1 += dot(v1[0], k[0]) + dot(v1[1], k[1]);
168
sum2 += dot(v2[0], k[0]) + dot(v2[1], k[1]);
169
sum3 += dot(v3[0], k[0]) + dot(v3[1], k[1]);
175
sum0 = activation_afp(sum0, activation_type, activation_param_0, activation_param_1);
176
sum1 = activation_afp(sum1, activation_type, activation_param_0, activation_param_1);
177
sum2 = activation_afp(sum2, activation_type, activation_param_0, activation_param_1);
178
sum3 = activation_afp(sum3, activation_type, activation_param_0, activation_param_1);
181
image3d_st1(top_blob, ivec3(sx4.r, sy4.r, gy), sum0);
182
image3d_st1(top_blob, ivec3(sx4.g, sy4.g, gy), sum1);
183
image3d_st1(top_blob, ivec3(sx4.b, sy4.b, gy), sum2);
184
image3d_st1(top_blob, ivec3(sx4.a, sy4.a, gy), sum3);
186
const int gi = gy * psc(outcstep) + gx;
188
buffer_st1(top_blob_data, gi, sum0);
189
if (gx + 1 < outsize) buffer_st1(top_blob_data, gi + 1, sum1);
190
if (gx + 2 < outsize) buffer_st1(top_blob_data, gi + 2, sum2);
191
if (gx + 3 < outsize) buffer_st1(top_blob_data, gi + 3, sum3);