1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2022 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.
15
#ifndef NCNN_VULKAN_ACTIVATION_COMP
16
#define NCNN_VULKAN_ACTIVATION_COMP
18
afp activation_afp(afp v, int activation_type, float activation_param_0, float activation_param_1)
20
if (activation_type == 1)
24
if (activation_type == 2)
26
const afp slope = afp(activation_param_0);
27
v = v < afp(0.f) ? v * slope : v;
29
if (activation_type == 3)
31
const afp const_min = afp(activation_param_0);
32
const afp const_max = afp(activation_param_1);
33
v = clamp(v, const_min, const_max);
35
if (activation_type == 4)
37
v = afp(1.f) / (afp(1.f) + exp(-v));
39
if (activation_type == 5)
42
v = v * afp(tanh(float(log(exp(v) + afp(1.f)))));
44
v = v * tanh(log(exp(v) + afp(1.f)));
47
if (activation_type == 6)
49
const afp alpha = afp(activation_param_0);
50
const afp beta = afp(activation_param_1);
51
v = v * clamp(v * afp(alpha) + afp(beta), afp(0.f), afp(1.f));
57
afpvec4 activation_afpvec4(afpvec4 v, int activation_type, float activation_param_0, float activation_param_1)
59
if (activation_type == 1)
63
if (activation_type == 2)
65
const afp slope = afp(activation_param_0);
66
v = mix(v, v * afp(slope), lessThan(v, afpvec4(0.f)));
68
if (activation_type == 3)
70
const afp const_min = afp(activation_param_0);
71
const afp const_max = afp(activation_param_1);
72
v = clamp(v, const_min, const_max);
74
if (activation_type == 4)
76
v = afp(1.f) / (afp(1.f) + exp(-v));
78
if (activation_type == 5)
81
v = v * afpvec4(tanh(vec4(log(exp(v) + afp(1.f)))));
83
v = v * tanh(log(exp(v) + afp(1.f)));
86
if (activation_type == 6)
88
const afp alpha = afp(activation_param_0);
89
const afp beta = afp(activation_param_1);
90
v = v * clamp(v * afp(alpha) + afp(beta), afp(0.f), afp(1.f));
96
afpvec8 activation_afpvec8(afpvec8 v, int activation_type, float activation_param_0, float activation_param_1)
98
if (activation_type == 1)
100
v[0] = max(v[0], afp(0.f));
101
v[1] = max(v[1], afp(0.f));
103
if (activation_type == 2)
105
const afp slope = afp(activation_param_0);
106
v[0] = mix(v[0], v[0] * afp(slope), lessThan(v[0], afpvec4(0.f)));
107
v[1] = mix(v[1], v[1] * afp(slope), lessThan(v[1], afpvec4(0.f)));
109
if (activation_type == 3)
111
const afp const_min = afp(activation_param_0);
112
const afp const_max = afp(activation_param_1);
113
v[0] = clamp(v[0], const_min, const_max);
114
v[1] = clamp(v[1], const_min, const_max);
116
if (activation_type == 4)
118
v[0] = afp(1.f) / (afp(1.f) + exp(-v[0]));
119
v[1] = afp(1.f) / (afp(1.f) + exp(-v[1]));
121
if (activation_type == 5)
124
v[0] = v[0] * afpvec4(tanh(vec4(log(exp(v[0]) + afp(1.f)))));
125
v[1] = v[1] * afpvec4(tanh(vec4(log(exp(v[1]) + afp(1.f)))));
127
v[0] = v[0] * tanh(log(exp(v[0]) + afp(1.f)));
128
v[1] = v[1] * tanh(log(exp(v[1]) + afp(1.f)));
131
if (activation_type == 6)
133
const afp alpha = afp(activation_param_0);
134
const afp beta = afp(activation_param_1);
135
v[0] = v[0] * clamp(v[0] * afp(alpha) + afp(beta), afp(0.f), afp(1.f));
136
v[1] = v[1] * clamp(v[1] * afp(alpha) + afp(beta), afp(0.f), afp(1.f));
142
#endif // NCNN_VULKAN_ACTIVATION_COMP