|
- // Tencent is pleased to support the open source community by making ncnn available.
- //
- // Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
- //
- // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
- // in compliance with the License. You may obtain a copy of the License at
- //
- // https://opensource.org/licenses/BSD-3-Clause
- //
- // Unless required by applicable law or agreed to in writing, software distributed
- // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- // CONDITIONS OF ANY KIND, either express or implied. See the License for the
- // specific language governing permissions and limitations under the License.
-
- #version 450
-
- #if NCNN_fp16_storage
- #extension GL_EXT_shader_16bit_storage: require
- #endif
- #if NCNN_fp16_arithmetic
- #extension GL_EXT_shader_explicit_arithmetic_types_float16: require
- #endif
-
- layout (constant_id = 0) const int across_spatial = 0;
- layout (constant_id = 1) const int across_channel = 0;
- layout (constant_id = 2) const float eps = 0.f;
- layout (constant_id = 3) const int eps_mode = 0;
-
- #if NCNN_image_shader
- layout (binding = 0) uniform highp sampler3D sqsum_blob;
- layout (binding = 1, imfmtc1) writeonly uniform unfp image3D coeffs_blob;
- #else
- layout (binding = 0) readonly buffer sqsum_blob { float sqsum_blob_data[]; };
- layout (binding = 1) writeonly buffer coeffs_blob { sfp coeffs_blob_data[]; };
- #endif
-
- layout (push_constant) uniform parameter
- {
- int w;
- int h;
- int c;
- int cstep;
- } p;
-
- void main()
- {
- int gx = int(gl_GlobalInvocationID.x);
- int gy = int(gl_GlobalInvocationID.y);
- int gz = int(gl_GlobalInvocationID.z);
-
- if (gx >= p.w || gy >= p.h || gz >= p.c)
- return;
-
- #if NCNN_image_shader
- afp sqsum = afp(texelFetch(sqsum_blob, ivec3(gx, gy, gz), 0).r);
- #else
- int v_offset = gz * p.cstep + gx;
-
- afp sqsum = afp(sqsum_blob_data[v_offset]);
- #endif
-
- afp a;
-
- if (eps_mode == 0) // caffe/mxnet
- {
- a = afp(1.f) / sqrt(sqsum + afp(eps));
- }
-
- if (eps_mode == 1) // pytorch
- {
- a = afp(1.f) / max(sqrt(sqsum), afp(eps));
- }
-
- if (eps_mode == 2) // tensorflow
- {
- a = afp(1.f) / sqrt(max(sqsum, afp(eps)));
- }
-
- #if NCNN_image_shader
- int gi = gz * p.w * p.h + gy * p.w + gx;
-
- image3d_st1(coeffs_blob, ivec3(gi, 0, 0), a);
- #else
- int gi = gz * p.w + gx;
-
- buffer_st1(coeffs_blob_data, gi, a);
- #endif
- }
|