Elffuss BraidHub

kikocis/ncnn public

master · 4.3 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, imfmtc1) 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 { sfpvec8 bottom_tm_blob_data[]; };
41layout (binding = 1) writeonly buffer top_tm_blob { sfp 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 afp sum0 = afp(0.f);
65 afp sum1 = afp(0.f);
66 afp sum2 = afp(0.f);
67 afp sum3 = afp(0.f);
68
69#if NCNN_image_shader
70 for (int z = 0; z < psc(c); z++)
71 {
72 afpvec8 v0 = image3d_ld8(bottom_tm_blob, ivec3(gx + 0, z, gz));
73 afpvec8 v1 = image3d_ld8(bottom_tm_blob, ivec3(gx + 1, z, gz));
74 afpvec8 v2 = image3d_ld8(bottom_tm_blob, ivec3(gx + 2, z, gz));
75 afpvec8 v3 = image3d_ld8(bottom_tm_blob, ivec3(gx + 3, z, gz));
76
77 afpvec8 k = image3d_ld8(weight_tm_blob, ivec3(z, gy, gz));
78
79 // sum += dot(v, k);
80 sum0 += dot(v0[0], k[0]) + dot(v0[1], k[1]);
81 sum1 += dot(v1[0], k[0]) + dot(v1[1], k[1]);
82 sum2 += dot(v2[0], k[0]) + dot(v2[1], k[1]);
83 sum3 += dot(v3[0], k[0]) + dot(v3[1], k[1]);
84 }
85#else
86 int v_offset = gz * psc(cstep) + gx;
87 int w_offset = gz * psc(c) * psc(outc) + gy * psc(c);
88
89 for (int z = 0; z < psc(c); z++)
90 {
91 afpvec8 v0 = buffer_ld8(bottom_tm_blob_data, v_offset + 0);
92 afpvec8 v1 = buffer_ld8(bottom_tm_blob_data, v_offset + 1);
93 afpvec8 v2 = buffer_ld8(bottom_tm_blob_data, v_offset + 2);
94 afpvec8 v3 = buffer_ld8(bottom_tm_blob_data, v_offset + 3);
95
96 afpvec8 k = buffer_ld8(weight_tm_data, w_offset);
97
98 // sum += dot(v, k);
99 sum0 += dot(v0[0], k[0]) + dot(v0[1], k[1]);
100 sum1 += dot(v1[0], k[0]) + dot(v1[1], k[1]);
101 sum2 += dot(v2[0], k[0]) + dot(v2[1], k[1]);
102 sum3 += dot(v3[0], k[0]) + dot(v3[1], k[1]);
103
104 v_offset += psc(outw);
105 w_offset += 1;
106 }
107#endif
108
109#if NCNN_image_shader
110 image3d_st1(top_tm_blob, ivec3(gx + 0, gy, gz), sum0);
111 image3d_st1(top_tm_blob, ivec3(gx + 1, gy, gz), sum1);
112 image3d_st1(top_tm_blob, ivec3(gx + 2, gy, gz), sum2);
113 image3d_st1(top_tm_blob, ivec3(gx + 3, gy, gz), sum3);
114#else
115 int gi = gz * psc(outcstep) + gy * psc(outw) + gx;
116
117 buffer_st1(top_tm_blob_data, gi + 0, sum0);
118 if (gx + 1 < psc(outw)) buffer_st1(top_tm_blob_data, gi + 1, sum1);
119 if (gx + 2 < psc(outw)) buffer_st1(top_tm_blob_data, gi + 2, sum2);
120 if (gx + 3 < psc(outw)) buffer_st1(top_tm_blob_data, gi + 3, sum3);
121#endif
122}