Skip to content

[Executorch] optimized sigmoid #6522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
762f0aa
[Executorch] optimized sigmoid
kimishpatel Oct 28, 2024
b20c519
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 28, 2024
92f9c26
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 28, 2024
d7271b5
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 29, 2024
0217dab
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 29, 2024
64a4245
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 29, 2024
9af78a2
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 30, 2024
ad03b35
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 30, 2024
3acab31
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 30, 2024
7b1ac4b
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 30, 2024
d6c8622
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 30, 2024
56d3571
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 30, 2024
fb72c8a
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 30, 2024
40f2554
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 30, 2024
874900b
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 31, 2024
60b1a92
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 31, 2024
e26b9ef
Update on "[Executorch] optimized sigmoid"
kimishpatel Oct 31, 2024
39e5ffe
Update on "[Executorch] optimized sigmoid"
kimishpatel Nov 4, 2024
45686b4
Update on "[Executorch] optimized sigmoid"
kimishpatel Nov 4, 2024
f944b2f
Update on "[Executorch] optimized sigmoid"
kimishpatel Nov 5, 2024
7f406c6
Update on "[Executorch] optimized sigmoid"
kimishpatel Nov 5, 2024
3bd2839
Update on "[Executorch] optimized sigmoid"
kimishpatel Nov 6, 2024
110d5e4
Update on "[Executorch] optimized sigmoid"
kimishpatel Nov 11, 2024
1ae4748
Update on "[Executorch] optimized sigmoid"
kimishpatel Nov 12, 2024
946e71b
Update on "[Executorch] optimized sigmoid"
kimishpatel Nov 13, 2024
5ea00e9
Update on "[Executorch] optimized sigmoid"
kimishpatel Nov 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions kernels/optimized/cpu/op_sigmoid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <cmath>

#include <executorch/kernels/optimized/vec/functional.h>
#include <executorch/kernels/optimized/vec/vec.h>
#include <executorch/runtime/kernel/kernel_includes.h>

namespace torch {
namespace executor {
namespace native {

namespace {

template <typename T>
constexpr bool is_half_or_bf16_v = std::is_same_v<T, exec_aten::Half> ||
std::is_same_v<T, exec_aten::BFloat16>;

template <
typename CTYPE_IN,
typename CTYPE_OUT,
typename std::enable_if<
std::is_same_v<CTYPE_IN, CTYPE_OUT> && !is_half_or_bf16_v<CTYPE_IN> &&
!is_half_or_bf16_v<CTYPE_OUT>,
int>::type = 0>
void sigmoid_data(
const CTYPE_IN* in_data,
const size_t numel,
CTYPE_OUT* out_data) {
using Vec = executorch::vec::Vectorized<CTYPE_IN>;
executorch::vec::map<CTYPE_IN>(
[](Vec x) {
auto one_plus_exp = x.neg().exp() + Vec(static_cast<CTYPE_IN>(1.0));
return one_plus_exp.reciprocal();
},
out_data,
in_data,
numel);
}

template <
typename CTYPE_IN,
typename CTYPE_OUT,
typename std::enable_if<
!std::is_same_v<CTYPE_IN, CTYPE_OUT> || is_half_or_bf16_v<CTYPE_IN> ||
is_half_or_bf16_v<CTYPE_OUT>,
int>::type = 0>
void sigmoid_data(
const CTYPE_IN* in_data,
const size_t numel,
CTYPE_OUT* out_data) {
for (size_t i = 0; i < numel; i++) {
CTYPE_OUT xi = static_cast<CTYPE_OUT>(in_data[i]);
out_data[i] = (1.0f / (1.0f + std::exp(-xi)));
}
}

} // namespace

using Tensor = exec_aten::Tensor;

Tensor&
opt_sigmoid_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
(void)ctx;

ET_KERNEL_CHECK(
ctx, in.scalar_type() != ScalarType::Bool, InvalidArgument, out);
ET_KERNEL_CHECK(ctx, tensor_is_floating_type(out), InvalidArgument, out);

ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);

// Resize for dynamic shape
ET_KERNEL_CHECK_MSG(
ctx,
resize_tensor(out, in.sizes()) == Error::Ok,
InvalidArgument,
out,
"Failed to resize output tensor.");

ScalarType in_type = in.scalar_type();
ScalarType out_type = out.scalar_type();
ET_SWITCH_REALHB_TYPES(in_type, ctx, "sigmoid.out", CTYPE_IN, [&]() {
ET_SWITCH_FLOATH_TYPES(out_type, ctx, "sigmoid.out", CTYPE_OUT, [&]() {
sigmoid_data<CTYPE_IN, CTYPE_OUT>(
in.const_data_ptr<CTYPE_IN>(),
in.numel(),
out.mutable_data_ptr<CTYPE_OUT>());
});
});

return out;
}

} // namespace native
} // namespace executor
} // namespace torch
1 change: 1 addition & 0 deletions kernels/optimized/cpu/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ _OPTIMIZED_ATEN_OPS = (
],
),
op_target(name = "op_exp"),
op_target(name = "op_sigmoid"),
op_target(
name = "op_gelu",
deps = select({
Expand Down
5 changes: 5 additions & 0 deletions kernels/optimized/optimized-oss.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
- arg_meta: null
kernel_name: torch::executor::opt_exp_out

- op: sigmoid.out
kernels:
- arg_meta: null
kernel_name: torch::executor::opt_sigmoid_out

- op: le.Scalar_out
kernels:
- arg_meta: null
Expand Down
5 changes: 5 additions & 0 deletions kernels/optimized/optimized.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
- arg_meta: null
kernel_name: torch::executor::opt_exp_out

- op: sigmoid.out
kernels:
- arg_meta: null
kernel_name: torch::executor::opt_sigmoid_out

- op: gelu.out
kernels:
- arg_meta: null
Expand Down
2 changes: 1 addition & 1 deletion kernels/test/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def define_common_targets():
_common_op_test("op_scatter_add_test", ["aten", "portable"])
_common_op_test("op_select_scatter_test", ["aten", "portable"])
_common_op_test("op_select_copy_test", ["aten", "portable"])
_common_op_test("op_sigmoid_test", ["aten", "portable"])
_common_op_test("op_sigmoid_test", ["aten", "portable", "optimized"])
_common_op_test("op_sign_test", ["aten", "portable"])
_common_op_test("op_sin_test", ["aten", "portable"])
_common_op_test("op_sinh_test", ["aten", "portable"])
Expand Down
Loading