Skip to content

Commit 0042bd0

Browse files
committed
[Executorch] optimized sigmoid
Pull Request resolved: #6522 basically use exp approximation using sleef instead of std::exp ghstack-source-id: 252186436 @exported-using-ghexport Differential Revision: [D64156864](https://our.internmc.facebook.com/intern/diff/D64156864/)
1 parent ce50825 commit 0042bd0

File tree

5 files changed

+114
-1
lines changed

5 files changed

+114
-1
lines changed

kernels/optimized/cpu/op_sigmoid.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#include <cmath>
10+
11+
#include <executorch/kernels/optimized/vec/functional.h>
12+
#include <executorch/kernels/optimized/vec/vec.h>
13+
#include <executorch/runtime/kernel/kernel_includes.h>
14+
15+
namespace torch {
16+
namespace executor {
17+
namespace native {
18+
19+
namespace {
20+
template <
21+
typename CTYPE_IN,
22+
typename CTYPE_OUT,
23+
typename std::enable_if<
24+
std::is_same_v<CTYPE_IN, CTYPE_OUT> &&
25+
!std::is_same_v<CTYPE_IN, exec_aten::Half> &&
26+
!std::is_same_v<CTYPE_OUT, exec_aten::BFloat16>,
27+
int>::type = 0>
28+
void sigmoid_data(
29+
const CTYPE_IN* in_data,
30+
const size_t numel,
31+
CTYPE_OUT* out_data) {
32+
using Vec = executorch::vec::Vectorized<CTYPE_IN>;
33+
executorch::vec::map<CTYPE_IN>(
34+
[](Vec x) {
35+
auto one_plus_exp = x.neg().exp() + Vec(1.0);
36+
return one_plus_exp.reciprocal();
37+
},
38+
out_data,
39+
in_data,
40+
numel);
41+
}
42+
43+
template <
44+
typename CTYPE_IN,
45+
typename CTYPE_OUT,
46+
typename std::enable_if<
47+
!std::is_same_v<CTYPE_IN, CTYPE_OUT> ||
48+
std::is_same_v<CTYPE_IN, exec_aten::Half> ||
49+
std::is_same_v<CTYPE_IN, exec_aten::BFloat16> ||
50+
std::is_same_v<CTYPE_OUT, exec_aten::Half> ||
51+
std::is_same_v<CTYPE_OUT, exec_aten::BFloat16>,
52+
int>::type = 0>
53+
void sigmoid_data(
54+
const CTYPE_IN* in_data,
55+
const size_t numel,
56+
CTYPE_OUT* out_data) {
57+
for (size_t i = 0; i < numel; i++) {
58+
CTYPE_OUT xi = static_cast<CTYPE_OUT>(in_data[i]);
59+
out_data[i] = (1.0 / (1.0 + std::exp(-xi)));
60+
}
61+
}
62+
63+
} // namespace
64+
65+
using Tensor = exec_aten::Tensor;
66+
67+
Tensor&
68+
opt_sigmoid_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
69+
(void)ctx;
70+
71+
ET_KERNEL_CHECK(
72+
ctx, in.scalar_type() != ScalarType::Bool, InvalidArgument, out);
73+
ET_KERNEL_CHECK(ctx, tensor_is_floating_type(out), InvalidArgument, out);
74+
75+
ET_KERNEL_CHECK(
76+
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);
77+
78+
// Resize for dynamic shape
79+
ET_KERNEL_CHECK_MSG(
80+
ctx,
81+
resize_tensor(out, in.sizes()) == Error::Ok,
82+
InvalidArgument,
83+
out,
84+
"Failed to resize output tensor.");
85+
86+
ScalarType in_type = in.scalar_type();
87+
ScalarType out_type = out.scalar_type();
88+
ET_SWITCH_REALHB_TYPES(in_type, ctx, "sigmoid.out", CTYPE_IN, [&]() {
89+
ET_SWITCH_FLOATH_TYPES(out_type, ctx, "sigmoid.out", CTYPE_OUT, [&]() {
90+
sigmoid_data<CTYPE_IN, CTYPE_OUT>(
91+
in.const_data_ptr<CTYPE_IN>(),
92+
in.numel(),
93+
out.mutable_data_ptr<CTYPE_OUT>());
94+
});
95+
});
96+
97+
return out;
98+
}
99+
100+
} // namespace native
101+
} // namespace executor
102+
} // namespace torch

kernels/optimized/cpu/targets.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ _OPTIMIZED_ATEN_OPS = (
2525
],
2626
),
2727
op_target(name = "op_exp"),
28+
op_target(name = "op_sigmoid"),
2829
op_target(
2930
name = "op_gelu",
3031
deps = select({

kernels/optimized/optimized-oss.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
- arg_meta: null
3636
kernel_name: torch::executor::opt_exp_out
3737

38+
- op: sigmoid.out
39+
kernels:
40+
- arg_meta: null
41+
kernel_name: torch::executor::opt_exp_out
42+
3843
- op: le.Scalar_out
3944
kernels:
4045
- arg_meta: null

kernels/optimized/optimized.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
- arg_meta: null
3838
kernel_name: torch::executor::opt_exp_out
3939

40+
- op: sigmoid.out
41+
kernels:
42+
- arg_meta: null
43+
kernel_name: torch::executor::opt_sigmoid_out
44+
4045
- op: gelu.out
4146
kernels:
4247
- arg_meta: null

kernels/test/targets.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def define_common_targets():
296296
_common_op_test("op_scatter_add_test", ["aten", "portable"])
297297
_common_op_test("op_select_scatter_test", ["aten", "portable"])
298298
_common_op_test("op_select_copy_test", ["aten", "portable"])
299-
_common_op_test("op_sigmoid_test", ["aten", "portable"])
299+
_common_op_test("op_sigmoid_test", ["aten", "portable", "optimized"])
300300
_common_op_test("op_sign_test", ["aten", "portable"])
301301
_common_op_test("op_sin_test", ["aten", "portable"])
302302
_common_op_test("op_sinh_test", ["aten", "portable"])

0 commit comments

Comments
 (0)