Skip to content

Commit 665b378

Browse files
committed
Reapply "Implement unary_ufunc functions using elementwise_util (#9386)"
This PR was reverted due to internal test failures, which should be fixed now (and is being sent as an exported diff to make sure). Original summary: 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). Differential Revision: [D76754824](https://our.internmc.facebook.com/intern/diff/D76754824/) [ghstack-poisoned]
1 parent 5c819bd commit 665b378

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+242
-274
lines changed

backends/cadence/fusion_g3/operators/op_exp.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ Tensor& exp_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
5959

6060
return out;
6161
} else {
62+
static constexpr const char op_name[] = "exp.out";
6263
return torch::executor::native::internal::
63-
unary_ufunc_realhbbf16_to_floathbf16(std::exp, ctx, in, out);
64+
unary_ufunc_realhbbf16_to_floathbf16<op_name>(
65+
[](auto x) { return executorch::math::exp(x); }, ctx, in, out);
6466
}
6567
}
6668

backends/cadence/fusion_g3/operators/op_rsqrt.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@ namespace impl {
2525
namespace G3 {
2626
namespace native {
2727

28-
namespace {
29-
30-
double rsqrt(double x) {
31-
return 1.0 / std::sqrt(x);
32-
}
33-
34-
} // namespace
35-
3628
Tensor& rsqrt_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
3729
#ifdef OP_ARG_CHECK
3830
// Resize for dynamic shape
@@ -60,12 +52,14 @@ Tensor& rsqrt_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
6052

6153
return out;
6254
} else {
55+
static constexpr const char op_name[] = "rsqrt.out";
6356
return torch::executor::native::internal::
64-
unary_ufunc_realhbbf16_to_floathbf16(rsqrt, ctx, in, out);
57+
unary_ufunc_realhbbf16_to_floathbf16<op_name>(
58+
[](auto x) { return executorch::math::rsqrt(x); }, ctx, in, out);
6559
}
6660
}
6761

6862
} // namespace native
6963
} // namespace G3
7064
} // namespace impl
71-
} // namespace cadence
65+
} // namespace cadence

backends/cadence/fusion_g3/operators/op_sqrt.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ Tensor& sqrt_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
5454

5555
return out;
5656
} else {
57+
static constexpr const char op_name[] = "sqrt.out";
5758
return torch::executor::native::internal::
58-
unary_ufunc_realhbbf16_to_floathbf16(std::sqrt, ctx, in, out);
59+
unary_ufunc_realhbbf16_to_floathbf16<op_name>(
60+
[](auto x) { return executorch::math::sqrt(x); }, ctx, in, out);
5961
}
6062
}
6163

backends/cadence/fusion_g3/operators/op_tanh.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ Tensor& tanh_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
5454

5555
return out;
5656
} else {
57+
static constexpr const char op_name[] = "tanh.out";
5758
return torch::executor::native::internal::
58-
unary_ufunc_realhbbf16_to_floathbf16(std::tanh, ctx, in, out);
59+
unary_ufunc_realhbbf16_to_floathbf16<op_name>(
60+
[](auto x) { return executorch::math::tanh(x); }, ctx, in, out);
5961
}
6062
}
6163

backends/cadence/hifi/operators/op_rsqrt.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ namespace cadence {
1919
namespace impl {
2020
namespace HiFi {
2121
namespace native {
22-
namespace {
23-
24-
double rsqrt(double x) {
25-
return 1.0 / std::sqrt(x);
26-
}
27-
28-
} // namespace
2922

3023
Tensor& rsqrt_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) {
3124
bool optimized = true;
@@ -45,8 +38,10 @@ Tensor& rsqrt_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) {
4538
return out;
4639
}
4740

41+
static constexpr const char op_name[] = "rsqrt.out";
4842
return torch::executor::native::internal::
49-
unary_ufunc_realhbbf16_to_floathbf16(rsqrt, ctx, in, out);
43+
unary_ufunc_realhbbf16_to_floathbf16<op_name>(
44+
[](auto x) { return executorch::math::rsqrt(x); }, ctx, in, out);
5045
}
5146

5247
} // namespace native

backends/cadence/hifi/operators/op_tanh.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ Tensor& tanh_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) {
3434
return out;
3535
}
3636

37+
static constexpr const char op_name[] = "tanh.out";
3738
return torch::executor::native::internal::
38-
unary_ufunc_realhbbf16_to_floathbf16(std::tanh, ctx, in, out);
39+
unary_ufunc_realhbbf16_to_floathbf16<op_name>(
40+
[](auto x) { return executorch::math::tanh(x); }, ctx, in, out);
3941
}
4042

4143
} // namespace native
4244
} // namespace HiFi
4345
} // namespace impl
44-
} // namespace cadence
46+
} // namespace cadence

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

0 commit comments

Comments
 (0)