1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2019 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
20
#if NCNN_fp16_arithmetic
21
#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
24
layout (constant_id = 0) const int across_spatial = 0;
25
layout (constant_id = 1) const int across_channel = 0;
26
layout (constant_id = 2) const float eps = 0.f;
27
layout (constant_id = 3) const int eps_mode = 0;
30
layout (binding = 0) uniform highp sampler3D sqsum_blob;
31
layout (binding = 1, imfmtc4) writeonly uniform unfp image3D coeffs_blob;
33
layout (binding = 0) readonly buffer sqsum_blob { vec4 sqsum_blob_data[]; };
34
layout (binding = 1) writeonly buffer coeffs_blob { sfpvec4 coeffs_blob_data[]; };
37
layout (push_constant) uniform parameter
47
int gx = int(gl_GlobalInvocationID.x);
48
int gy = int(gl_GlobalInvocationID.y);
49
int gz = int(gl_GlobalInvocationID.z);
51
if (gx >= p.w || gy >= p.h || gz >= p.c)
55
vec4 sqsum_4 = texelFetch(sqsum_blob, ivec3(gx, gy, gz), 0);
57
int v_offset = gz * p.cstep + gx;
59
vec4 sqsum_4 = sqsum_blob_data[v_offset];
62
if (across_channel == 0)
64
afpvec4 sqsum = afpvec4(sqsum_4);
68
if (eps_mode == 0) // caffe/mxnet
70
a = afp(1.f) / sqrt(sqsum + afp(eps));
73
if (eps_mode == 1) // pytorch
75
a = afp(1.f) / max(sqrt(sqsum), afp(eps));
78
if (eps_mode == 2) // tensorflow
80
a = afp(1.f) / sqrt(max(sqsum, afp(eps)));
84
int gi = gz * p.w * p.h + gy * p.w + gx;
86
image3d_st4(coeffs_blob, ivec3(gi, 0, 0), a);
88
int gi = gz * p.w + gx;
90
buffer_st4(coeffs_blob_data, gi, a);
94
if (across_channel == 1)
96
afp sqsum = afp(sqsum_4.r + sqsum_4.g + sqsum_4.b + sqsum_4.a);
100
if (eps_mode == 0) // caffe/mxnet
102
a = afp(1.f) / sqrt(sqsum + afp(eps));
105
if (eps_mode == 1) // pytorch
107
a = afp(1.f) / max(sqrt(sqsum), afp(eps));
110
if (eps_mode == 2) // tensorflow
112
a = afp(1.f) / sqrt(max(sqsum, afp(eps)));
116
int gi = gz * p.w * p.h + gy * p.w + gx;
118
image3d_st4(coeffs_blob, ivec3(gi, 0, 0), afpvec4(a));
120
int gi = gz * p.w + gx;
122
buffer_st4(coeffs_blob_data, gi, afpvec4(a));