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
25layout (constant_id = 0) const int batch = 1;
26
27#define shape_constant_id_offset 1
28layout (constant_id = shape_constant_id_offset + 0) const int c = 0;
29layout (constant_id = shape_constant_id_offset + 1) const int cstep = 0;
30
31layout (constant_id = shape_constant_id_offset + 2) const int outw = 0;
32layout (constant_id = shape_constant_id_offset + 3) const int outc = 0;
33layout (constant_id = shape_constant_id_offset + 4) const int outcstep = 0;
34
35#if NCNN_image_shader
36layout (binding = 0) uniform unfp sampler3D bottom_tm_blob;
37layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_tm_blob;
38layout (binding = 2) uniform unfp sampler3D weight_tm_blob;
39#else
40layout (binding = 0) readonly buffer bottom_tm_blob { sfp bottom_tm_blob_data[]; };
41layout (binding = 1) writeonly buffer top_tm_blob { sfpvec8 top_tm_blob_data[]; };
42layout (binding = 2) readonly buffer weight_tm_blob { sfpvec8 weight_tm_data[]; };
43#endif
44
45layout (push_constant) uniform parameter
46{
47 int c;
48 int cstep;
49
50 int outw;
51 int outc;
52 int outcstep;
53} p;
54
55void main()
56{
57 int gx = int(gl_GlobalInvocationID.x) * 4;
58 int gy = int(gl_GlobalInvocationID.y);
59 int gz = int(gl_GlobalInvocationID.z);
60
61 if (gx >= psc(outw) || gy >= psc(outc) || gz >= batch)
62 return;
63
64 afpvec8 sum0 = afpvec8(afpvec4(0.f), afpvec4(0.f));
65 afpvec8 sum1 = afpvec8(afpvec4(0.f), afpvec4(0.f));
66 afpvec8 sum2 = afpvec8(afpvec4(0.f), afpvec4(0.f));
67 afpvec8 sum3 = afpvec8(afpvec4(0.f), afpvec4(0.f));
68
69#if NCNN_image_shader
70 for (int z = 0; z < psc(c); z++)
71 {
72 afp v0 = image3d_ld1(bottom_tm_blob, ivec3(gx + 0, z, gz));
73 afp v1 = image3d_ld1(bottom_tm_blob, ivec3(gx + 1, z, gz));
74 afp v2 = image3d_ld1(bottom_tm_blob, ivec3(gx + 2, z, gz));
75 afp v3 = image3d_ld1(bottom_tm_blob, ivec3(gx + 3, z, gz));
76
77 afpvec8 k = image3d_ld8(weight_tm_blob, ivec3(z, gy, gz));
78
79 // sum += v * k;
80 sum0[0] += v0 * k[0];
81 sum0[1] += v0 * k[1];
82
83 sum1[0] += v1 * k[0];
84 sum1[1] += v1 * k[1];
85
86 sum2[0] += v2 * k[0];
87 sum2[1] += v2 * k[1];
88
89 sum3[0] += v3 * k[0];
90 sum3[1] += v3 * k[1];
91 }
92#else
93 int v_offset = gz * psc(cstep) + gx;
94 int w_offset = gz * psc(c) * psc(outc) + gy * psc(c);
95
96 for (int z = 0; z < psc(c); z++)
97 {
98 afp v0 = buffer_ld1(bottom_tm_blob_data, v_offset + 0);
99 afp v1 = buffer_ld1(bottom_tm_blob_data, v_offset + 1);
100 afp v2 = buffer_ld1(bottom_tm_blob_data, v_offset + 2);
101 afp v3 = buffer_ld1(bottom_tm_blob_data, v_offset + 3);
102
103 afpvec8 k = buffer_ld8(weight_tm_data, w_offset);
104
105 // sum += v * k;
106 sum0[0] += v0 * k[0];
107 sum0[1] += v0 * k[1];
108
109 sum1[0] += v1 * k[0];
110 sum1[1] += v1 * k[1];
111
112 sum2[0] += v2 * k[0];
113 sum2[1] += v2 * k[1];
114
115 sum3[0] += v3 * k[0];
116 sum3[1] += v3 * k[1];
117
118 v_offset += psc(outw);
119 w_offset += 1;
120 }
121#endif
122
123#if NCNN_image_shader
124 image3d_st8(top_tm_blob, ivec3(gx + 0, gy, gz), sum0);
125 image3d_st8(top_tm_blob, ivec3(gx + 1, gy, gz), sum1);
126 image3d_st8(top_tm_blob, ivec3(gx + 2, gy, gz), sum2);
127 image3d_st8(top_tm_blob, ivec3(gx + 3, gy, gz), sum3);
128#else
129 int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
130
131 buffer_st8(top_tm_blob_data, gi + 0, sum0);
132 if (gx + 1 < psc(outw)) buffer_st8(top_tm_blob_data, gi + 1, sum1);
133 if (gx + 2 < psc(outw)) buffer_st8(top_tm_blob_data, gi + 2, sum2);
134 if (gx + 3 < psc(outw)) buffer_st8(top_tm_blob_data, gi + 3, sum3);
135#endif
136}