Skip to content

Commit 32e427b

Browse files
committed
Implement unary_ufunc functions using elementwise_util
One less set of independent implementations to worry about going forward (e.g., we don't have to vectorize these separately from elementwise_util and they get all benefits of elementwise_util). ghstack-source-id: 11dfccc ghstack-comment-id: 2735017402 Pull Request resolved: #9386
1 parent 7c4b6da commit 32e427b

37 files changed

+234
-259
lines changed

kernels/portable/cpu/op_acos.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& acos_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(
19-
std::acos, ctx, in, out);
18+
static constexpr const char op_name[] = "acos.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::acos(x); }, ctx, in, out);
2021
}
2122

2223
} // namespace native

kernels/portable/cpu/op_acosh.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& acosh_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(
19-
std::acosh, ctx, in, out);
18+
static constexpr const char op_name[] = "acosh.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::acosh(x); }, ctx, in, out);
2021
}
2122

2223
} // namespace native

kernels/portable/cpu/op_asin.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& asin_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(
19-
std::asin, ctx, in, out);
18+
static constexpr const char op_name[] = "asin.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::asin(x); }, ctx, in, out);
2021
}
2122

2223
} // namespace native

kernels/portable/cpu/op_asinh.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& asinh_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(
19-
std::asinh, ctx, in, out);
18+
static constexpr const char op_name[] = "asinh.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::asinh(x); }, ctx, in, out);
2021
}
2122

2223
} // namespace native

kernels/portable/cpu/op_atan.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& atan_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(
19-
std::atan, ctx, in, out);
18+
static constexpr const char op_name[] = "atan.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::atan(x); }, ctx, in, out);
2021
}
2122

2223
} // namespace native

kernels/portable/cpu/op_atanh.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& atanh_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(
19-
std::atanh, ctx, in, out);
18+
static constexpr const char op_name[] = "atanh.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::atanh(x); }, ctx, in, out);
2021
}
2122

2223
} // namespace native

kernels/portable/cpu/op_ceil.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ namespace native {
1717
using executorch::aten::Tensor;
1818

1919
Tensor& ceil_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
20-
return internal::unary_ufunc_realhbf16(std::ceil, ctx, in, out);
20+
static constexpr const char op_name[] = "ceil.out";
21+
return internal::unary_ufunc_realhbf16<op_name>(
22+
[](auto x) { return executorch::math::ceil(x); }, ctx, in, out);
2123
}
2224

2325
} // namespace native

kernels/portable/cpu/op_cos.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& cos_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(std::cos, ctx, in, out);
18+
static constexpr const char op_name[] = "cos.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::cos(x); }, ctx, in, out);
1921
}
2022

2123
} // namespace native

kernels/portable/cpu/op_cosh.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& cosh_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(
19-
std::cosh, ctx, in, out);
18+
static constexpr const char op_name[] = "cosh.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::cosh(x); }, ctx, in, out);
2021
}
2122

2223
} // namespace native

kernels/portable/cpu/op_erf.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& erf_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(std::erf, ctx, in, out);
18+
static constexpr const char op_name[] = "erf.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::erf(x); }, ctx, in, out);
1921
}
2022

2123
} // namespace native

kernels/portable/cpu/op_exp.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& exp_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(std::exp, ctx, in, out);
18+
static constexpr const char op_name[] = "exp.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::exp(x); }, ctx, in, out);
1921
}
2022

2123
} // namespace native

kernels/portable/cpu/op_expm1.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
*/
88

99
#include <executorch/kernels/portable/cpu/pattern/pattern.h>
10+
#include <executorch/kernels/portable/cpu/util/elementwise_util.h>
1011
#include <executorch/runtime/kernel/kernel_includes.h>
1112
#include <cmath>
13+
#include <type_traits>
1214

1315
namespace torch {
1416
namespace executor {
1517
namespace native {
1618

1719
Tensor& expm1_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(
19-
std::expm1, ctx, in, out);
20+
static constexpr const char op_name[] = "expm1.out";
21+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
22+
[](auto x) { return executorch::math::expm1(x); }, ctx, in, out);
2023
}
2124

2225
} // namespace native

kernels/portable/cpu/op_floor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ namespace native {
1717
using executorch::aten::Tensor;
1818

1919
Tensor& floor_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
20-
return internal::unary_ufunc_realhbf16(std::floor, ctx, in, out);
20+
static constexpr const char op_name[] = "floor.out";
21+
return internal::unary_ufunc_realhbf16<op_name>(
22+
[](auto x) { return executorch::math::floor(x); }, ctx, in, out);
2123
}
2224

2325
} // namespace native

kernels/portable/cpu/op_isinf.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ namespace native {
1717
Tensor& isinf_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
1818
// Lambda is syntactic sugar needed to workaround compilation on some older
1919
// non-compatible distros where isnan is returning int rather than bool
20-
return internal::unary_ufunc_realhb_to_bool(
21-
[](double x) -> bool { return std::isinf(x); }, ctx, in, out);
20+
static constexpr const char op_name[] = "isinf.out";
21+
return internal::unary_ufunc_realhb_to_bool<op_name>(
22+
[](auto x) -> bool { return std::isinf(x); }, ctx, in, out);
2223
}
2324

2425
} // namespace native

kernels/portable/cpu/op_isnan.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ namespace native {
1717
Tensor& isnan_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
1818
// Lambda is syntactic sugar needed to workaround compilation on some older
1919
// non-compatible distros where isnan is returning int rather than bool
20-
return internal::unary_ufunc_realhb_to_bool(
21-
[](double x) -> bool { return std::isnan(x); }, ctx, in, out);
20+
static constexpr const char op_name[] = "isnan.out";
21+
return internal::unary_ufunc_realhb_to_bool<op_name>(
22+
[](auto x) -> bool { return std::isnan(x); }, ctx, in, out);
2223
}
2324

2425
} // namespace native

kernels/portable/cpu/op_log.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& log_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(std::log, ctx, in, out);
18+
static constexpr const char op_name[] = "log.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::log(x); }, ctx, in, out);
1921
}
2022

2123
} // namespace native

kernels/portable/cpu/op_log10.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& log10_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(
19-
std::log10, ctx, in, out);
18+
static constexpr const char op_name[] = "log10.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::log10(x); }, ctx, in, out);
2021
}
2122

2223
} // namespace native

kernels/portable/cpu/op_log1p.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& log1p_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(
19-
std::log1p, ctx, in, out);
18+
static constexpr const char op_name[] = "log1p.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::log1p(x); }, ctx, in, out);
2021
}
2122

2223
} // namespace native

kernels/portable/cpu/op_log2.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& log2_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(
19-
std::log2, ctx, in, out);
18+
static constexpr const char op_name[] = "log2.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::log2(x); }, ctx, in, out);
2021
}
2122

2223
} // namespace native

kernels/portable/cpu/op_reciprocal.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,11 @@
1212
namespace torch {
1313
namespace executor {
1414
namespace native {
15-
namespace {
16-
17-
double reciprocal(double x) {
18-
return 1.0 / x;
19-
}
20-
21-
} // namespace
22-
2315
Tensor&
2416
reciprocal_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
25-
return internal::unary_ufunc_realhbbf16_to_floathbf16(
26-
reciprocal, ctx, in, out);
17+
static constexpr const char op_name[] = "reciprocal.out";
18+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
19+
[](auto x) { return executorch::math::reciprocal(x); }, ctx, in, out);
2720
}
2821

2922
} // namespace native

kernels/portable/cpu/op_rsqrt.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,11 @@
1212
namespace torch {
1313
namespace executor {
1414
namespace native {
15-
namespace {
16-
17-
double rsqrt(double x) {
18-
return 1.0 / std::sqrt(x);
19-
}
20-
21-
} // namespace
2215

2316
Tensor& rsqrt_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
24-
return internal::unary_ufunc_realhbbf16_to_floathbf16(rsqrt, ctx, in, out);
17+
static constexpr const char op_name[] = "rsqrt.out";
18+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
19+
[](auto x) { return executorch::math::rsqrt(x); }, ctx, in, out);
2520
}
2621

2722
} // namespace native

kernels/portable/cpu/op_sin.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& sin_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(std::sin, ctx, in, out);
18+
static constexpr const char op_name[] = "sin.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::sin(x); }, ctx, in, out);
1921
}
2022

2123
} // namespace native

kernels/portable/cpu/op_sinh.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& sinh_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(
19-
std::sinh, ctx, in, out);
18+
static constexpr const char op_name[] = "sinh.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::sinh(x); }, ctx, in, out);
2021
}
2122

2223
} // namespace native

kernels/portable/cpu/op_sqrt.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& sqrt_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(
19-
std::sqrt, ctx, in, out);
18+
static constexpr const char op_name[] = "sqrt.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::sqrt(x); }, ctx, in, out);
2021
}
2122

2223
} // namespace native

kernels/portable/cpu/op_tan.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& tan_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(std::tan, ctx, in, out);
18+
static constexpr const char op_name[] = "tan.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::tan(x); }, ctx, in, out);
1921
}
2022

2123
} // namespace native

kernels/portable/cpu/op_tanh.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& tanh_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbbf16_to_floathbf16(
19-
std::tanh, ctx, in, out);
18+
static constexpr const char op_name[] = "tanh.out";
19+
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
20+
[](auto x) { return executorch::math::tanh(x); }, ctx, in, out);
2021
}
2122

2223
} // namespace native

kernels/portable/cpu/op_trunc.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ namespace executor {
1515
namespace native {
1616

1717
Tensor& trunc_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
18-
return internal::unary_ufunc_realhbf16(std::trunc, ctx, in, out);
18+
static constexpr const char op_name[] = "trunc.out";
19+
return internal::unary_ufunc_realhbf16<op_name>(
20+
[](auto x) { return executorch::math::trunc(x); }, ctx, in, out);
1921
}
2022

2123
} // namespace native
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 <executorch/kernels/portable/cpu/pattern/pattern.h>
10+
11+
namespace torch::executor::native::internal {
12+
13+
bool check_and_resize_inputs(
14+
KernelRuntimeContext& ctx,
15+
const Tensor& in,
16+
Tensor& out) {
17+
ET_KERNEL_CHECK(
18+
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, false);
19+
ET_KERNEL_CHECK_MSG(
20+
ctx,
21+
resize_tensor(out, in.sizes()) == Error::Ok,
22+
InvalidArgument,
23+
false,
24+
"Failed to resize output tensor.");
25+
return true;
26+
}
27+
28+
} // namespace torch::executor::native::internal

0 commit comments

Comments
 (0)