Skip to content

Commit 33d78bc

Browse files
author
Ke Deng
committed
Merged PR 1138: Add activation functions: Elu, LeakyRelu, ThresholdedRelu
Add activation functions: Elu, LeakyRelu, ThresholdedRelu Related work items: #30
1 parent abf2ee0 commit 33d78bc

File tree

12 files changed

+202
-175
lines changed

12 files changed

+202
-175
lines changed

lotus/core/framework/kernel_def_builder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class KernelDef {
9595
return *this;
9696
}
9797

98+
// allowing output j to reuse memory of input i
9899
KernelDef& MayInplace(int i, int j) {
99100
// TODO: validate inputs.
100101
inplace_map_.push_back({i, j});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "core/providers/cpu/activation/activations.h"
2+
3+
namespace Lotus {
4+
5+
#define REGISTER_UNARY_ELEMENTWISE_KERNEL(x) \
6+
REGISTER_KERNEL(KernelDef(#x) \
7+
.Domain(LotusIR::kOnnxDomain) \
8+
.SinceVersion(1, 2) \
9+
.Provider(LotusIR::kCpuExecutionProvider) \
10+
.MayInplace(0, 0) \
11+
.TypeConstraint("T", DataTypeImpl::GetTensorType<float>()), \
12+
x<float>)
13+
14+
REGISTER_UNARY_ELEMENTWISE_KERNEL(Elu);
15+
REGISTER_UNARY_ELEMENTWISE_KERNEL(LeakyRelu);
16+
REGISTER_UNARY_ELEMENTWISE_KERNEL(Relu);
17+
REGISTER_UNARY_ELEMENTWISE_KERNEL(Sigmoid);
18+
REGISTER_UNARY_ELEMENTWISE_KERNEL(Tanh);
19+
REGISTER_UNARY_ELEMENTWISE_KERNEL(ThresholdedRelu);
20+
21+
} // namespace Lotus
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef CORE_PROVIDERS_CPU_ACTIVATION_RELU_H
2+
#define CORE_PROVIDERS_CPU_ACTIVATION_RELU_H
3+
4+
#include "core/common/common.h"
5+
#include "core/framework/op_kernel.h"
6+
#include "core/util/math_cpuonly.h"
7+
8+
namespace Lotus {
9+
10+
DECLARE_EIGEN_UNARY_ELEMENTWISE_KERNEL(Elu,
11+
{
12+
EIGEN_X_VAR(xm);
13+
EIGEN_Y = (xm >= 0).select(xm, Attr("alpha") * (xm.exp() - 1));
14+
},
15+
{"alpha"})
16+
17+
DECLARE_EIGEN_UNARY_ELEMENTWISE_KERNEL(LeakyRelu,
18+
{
19+
EIGEN_X_VAR(xm);
20+
EIGEN_Y = (xm >= 0).select(xm, Attr("alpha") * xm);
21+
},
22+
{"alpha"})
23+
24+
DECLARE_EIGEN_UNARY_ELEMENTWISE_KERNEL(Relu,
25+
{ EIGEN_Y = EIGEN_X.cwiseMax(0); },
26+
{})
27+
28+
DECLARE_EIGEN_UNARY_ELEMENTWISE_KERNEL(Sigmoid,
29+
{
30+
EIGEN_X_VAR(xm);
31+
EIGEN_Y_VAR(ym);
32+
ym = (xm >= 0).select(1 / (1. + (-xm.abs()).exp()), 1 - 1 / (1. + (-xm.abs()).exp()));
33+
},
34+
{})
35+
36+
DECLARE_EIGEN_UNARY_ELEMENTWISE_KERNEL(Tanh, { EIGEN_Y = EIGEN_X.tanh(); }, {})
37+
38+
DECLARE_EIGEN_UNARY_ELEMENTWISE_KERNEL(ThresholdedRelu,
39+
{
40+
EIGEN_X_VAR(xm);
41+
EIGEN_Y = (xm >= Attr("alpha")).select(xm, 0);
42+
},
43+
{"alpha"})
44+
45+
} // namespace Lotus
46+
47+
#endif // !CORE_PROVIDERS_CPU_ACTIVATION_RELU_H

lotus/core/providers/cpu/activation/relu.cc

Lines changed: 0 additions & 12 deletions
This file was deleted.

lotus/core/providers/cpu/activation/relu.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

lotus/core/providers/cpu/activation/sigmoid.cc

Lines changed: 0 additions & 12 deletions
This file was deleted.

lotus/core/providers/cpu/activation/sigmoid.h

Lines changed: 0 additions & 19 deletions
This file was deleted.

lotus/core/providers/cpu/activation/tanh.cc

Lines changed: 0 additions & 11 deletions
This file was deleted.

lotus/core/providers/cpu/activation/tanh.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

lotus/core/providers/cpu/math/clip.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ REGISTER_KERNEL(KernelDef("Clip")
3030
.Domain(LotusIR::kOnnxDomain)
3131
.SinceVersion(1, 2)
3232
.Provider(LotusIR::kCpuExecutionProvider)
33+
.MayInplace(0, 0)
3334
.TypeConstraint("T", DataTypeImpl::GetTensorType<float>()),
3435
Clip<float>);
3536
} // namespace Lotus

0 commit comments

Comments
 (0)