Elffuss BraidHub

kikocis/ncnn public

master · 3.6 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2020 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 across_spatial = 0;
26layout (constant_id = 1) const int across_channel = 0;
27layout (constant_id = 2) const float eps = 0.f;
28layout (constant_id = 3) const int eps_mode = 0;
29
30#if NCNN_image_shader
31layout (binding = 0) uniform highp sampler3D sqsum_blob;
32layout (binding = 1, imfmtc4) writeonly uniform unfp image3D coeffs_blob;
33#else
34layout (binding = 0) readonly buffer sqsum_blob { mat2x4 sqsum_blob_data[]; };
35layout (binding = 1) writeonly buffer coeffs_blob { sfpvec8 coeffs_blob_data[]; };
36#endif
37
38layout (push_constant) uniform parameter
39{
40 int w;
41 int h;
42 int c;
43 int cstep;
44} p;
45
46void main()
47{
48 int gx = int(gl_GlobalInvocationID.x);
49 int gy = int(gl_GlobalInvocationID.y);
50 int gz = int(gl_GlobalInvocationID.z);
51
52 if (gx >= p.w || gy >= p.h || gz >= p.c)
53 return;
54
55#if NCNN_image_shader
56 mat2x4 sqsum_8 = mat2x4(texelFetch(sqsum_blob, ivec3(gx * 2, gy, gz), 0), texelFetch(sqsum_blob, ivec3(gx * 2 + 1, gy, gz), 0));
57#else
58 int v_offset = gz * p.cstep + gx;
59
60 mat2x4 sqsum_8 = sqsum_blob_data[v_offset];
61#endif
62
63 if (across_channel == 0)
64 {
65 afpvec8 sqsum = afpvec8(sqsum_8[0], sqsum_8[1]);
66
67 afpvec8 a;
68
69 if (eps_mode == 0) // caffe/mxnet
70 {
71 a[0] = afp(1.f) / sqrt(sqsum[0] + afp(eps));
72 a[1] = afp(1.f) / sqrt(sqsum[1] + afp(eps));
73 }
74
75 if (eps_mode == 1) // pytorch
76 {
77 a[0] = afp(1.f) / max(sqrt(sqsum[0]), afp(eps));
78 a[1] = afp(1.f) / max(sqrt(sqsum[1]), afp(eps));
79 }
80
81 if (eps_mode == 2) // tensorflow
82 {
83 a[0] = afp(1.f) / sqrt(max(sqsum[0], afp(eps)));
84 a[1] = afp(1.f) / sqrt(max(sqsum[1], afp(eps)));
85 }
86
87#if NCNN_image_shader
88 int gi = gz * p.w * p.h + gy * p.w + gx;
89
90 image3d_st8(coeffs_blob, ivec3(gi, 0, 0), a);
91#else
92 int gi = gz * p.w + gx;
93
94 buffer_st8(coeffs_blob_data, gi, a);
95#endif
96 }
97
98 if (across_channel == 1)
99 {
100 vec4 sqsum_4 = sqsum_8[0] + sqsum_8[1];
101
102 afp sqsum = afp(sqsum_4.r + sqsum_4.g + sqsum_4.b + sqsum_4.a);
103
104 afp a;
105
106 if (eps_mode == 0) // caffe/mxnet
107 {
108 a = afp(1.f) / sqrt(sqsum + afp(eps));
109 }
110
111 if (eps_mode == 1) // pytorch
112 {
113 a = afp(1.f) / max(sqrt(sqsum), afp(eps));
114 }
115
116 if (eps_mode == 2) // tensorflow
117 {
118 a = afp(1.f) / sqrt(max(sqsum, afp(eps)));
119 }
120
121 afpvec8 a8 = afpvec8(afpvec4(a), afpvec4(a));
122
123#if NCNN_image_shader
124 int gi = gz * p.w * p.h + gy * p.w + gx;
125
126 image3d_st8(coeffs_blob, ivec3(gi, 0, 0), a8);
127#else
128 int gi = gz * p.w + gx;
129
130 buffer_st8(coeffs_blob_data, gi, a8);
131#endif
132 }
133}