Skip to content

Reapply "Implement unary_ufunc functions using elementwise_util (#9386)" #11943

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

Open
wants to merge 4 commits into
base: gh/swolchok/474/base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion backends/cadence/fusion_g3/operators/op_exp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ Tensor& exp_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {

return out;
} else {
static constexpr const char op_name[] = "exp.out";
return torch::executor::native::internal::
unary_ufunc_realhbbf16_to_floathbf16(std::exp, ctx, in, out);
unary_ufunc_realhbbf16_to_floathbf16<op_name>(
[](auto x) { return executorch::math::exp(x); }, ctx, in, out);
}
}

Expand Down
14 changes: 4 additions & 10 deletions backends/cadence/fusion_g3/operators/op_rsqrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ namespace impl {
namespace G3 {
namespace native {

namespace {

double rsqrt(double x) {
return 1.0 / std::sqrt(x);
}

} // namespace

Tensor& rsqrt_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
#ifdef OP_ARG_CHECK
// Resize for dynamic shape
Expand Down Expand Up @@ -60,12 +52,14 @@ Tensor& rsqrt_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {

return out;
} else {
static constexpr const char op_name[] = "rsqrt.out";
return torch::executor::native::internal::
unary_ufunc_realhbbf16_to_floathbf16(rsqrt, ctx, in, out);
unary_ufunc_realhbbf16_to_floathbf16<op_name>(
[](auto x) { return executorch::math::rsqrt(x); }, ctx, in, out);
}
}

} // namespace native
} // namespace G3
} // namespace impl
} // namespace cadence
} // namespace cadence
4 changes: 3 additions & 1 deletion backends/cadence/fusion_g3/operators/op_sqrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ Tensor& sqrt_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {

return out;
} else {
static constexpr const char op_name[] = "sqrt.out";
return torch::executor::native::internal::
unary_ufunc_realhbbf16_to_floathbf16(std::sqrt, ctx, in, out);
unary_ufunc_realhbbf16_to_floathbf16<op_name>(
[](auto x) { return executorch::math::sqrt(x); }, ctx, in, out);
}
}

Expand Down
4 changes: 3 additions & 1 deletion backends/cadence/fusion_g3/operators/op_tanh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ Tensor& tanh_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {

return out;
} else {
static constexpr const char op_name[] = "tanh.out";
return torch::executor::native::internal::
unary_ufunc_realhbbf16_to_floathbf16(std::tanh, ctx, in, out);
unary_ufunc_realhbbf16_to_floathbf16<op_name>(
[](auto x) { return executorch::math::tanh(x); }, ctx, in, out);
}
}

Expand Down
11 changes: 3 additions & 8 deletions backends/cadence/hifi/operators/op_rsqrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ namespace cadence {
namespace impl {
namespace HiFi {
namespace native {
namespace {

double rsqrt(double x) {
return 1.0 / std::sqrt(x);
}

} // namespace

Tensor& rsqrt_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) {
bool optimized = true;
Expand All @@ -45,8 +38,10 @@ Tensor& rsqrt_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) {
return out;
}

static constexpr const char op_name[] = "rsqrt.out";
return torch::executor::native::internal::
unary_ufunc_realhbbf16_to_floathbf16(rsqrt, ctx, in, out);
unary_ufunc_realhbbf16_to_floathbf16<op_name>(
[](auto x) { return executorch::math::rsqrt(x); }, ctx, in, out);
}

} // namespace native
Expand Down
6 changes: 4 additions & 2 deletions backends/cadence/hifi/operators/op_tanh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ Tensor& tanh_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) {
return out;
}

static constexpr const char op_name[] = "tanh.out";
return torch::executor::native::internal::
unary_ufunc_realhbbf16_to_floathbf16(std::tanh, ctx, in, out);
unary_ufunc_realhbbf16_to_floathbf16<op_name>(
[](auto x) { return executorch::math::tanh(x); }, ctx, in, out);
}

} // namespace native
} // namespace HiFi
} // namespace impl
} // namespace cadence
} // namespace cadence
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_acos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ namespace executor {
namespace native {

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

} // namespace native
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_acosh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ namespace executor {
namespace native {

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

} // namespace native
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_asin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ namespace executor {
namespace native {

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

} // namespace native
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_asinh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ namespace executor {
namespace native {

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

} // namespace native
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_atan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ namespace executor {
namespace native {

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

} // namespace native
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_atanh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ namespace executor {
namespace native {

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

} // namespace native
Expand Down
4 changes: 3 additions & 1 deletion kernels/portable/cpu/op_ceil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ namespace native {
using executorch::aten::Tensor;

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

} // namespace native
Expand Down
4 changes: 3 additions & 1 deletion kernels/portable/cpu/op_cos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ namespace executor {
namespace native {

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

} // namespace native
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_cosh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ namespace executor {
namespace native {

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

} // namespace native
Expand Down
4 changes: 3 additions & 1 deletion kernels/portable/cpu/op_erf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ namespace executor {
namespace native {

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

} // namespace native
Expand Down
4 changes: 3 additions & 1 deletion kernels/portable/cpu/op_exp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ namespace executor {
namespace native {

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

} // namespace native
Expand Down
7 changes: 5 additions & 2 deletions kernels/portable/cpu/op_expm1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
*/

#include <executorch/kernels/portable/cpu/pattern/pattern.h>
#include <executorch/kernels/portable/cpu/util/elementwise_util.h>
#include <executorch/runtime/kernel/kernel_includes.h>
#include <cmath>
#include <type_traits>

namespace torch {
namespace executor {
namespace native {

Tensor& expm1_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
return internal::unary_ufunc_realhbbf16_to_floathbf16(
std::expm1, ctx, in, out);
static constexpr const char op_name[] = "expm1.out";
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
[](auto x) { return executorch::math::expm1(x); }, ctx, in, out);
}

} // namespace native
Expand Down
4 changes: 3 additions & 1 deletion kernels/portable/cpu/op_floor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ namespace native {
using executorch::aten::Tensor;

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

} // namespace native
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_isinf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ namespace native {
Tensor& isinf_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
// Lambda is syntactic sugar needed to workaround compilation on some older
// non-compatible distros where isnan is returning int rather than bool
return internal::unary_ufunc_realhb_to_bool(
[](double x) -> bool { return std::isinf(x); }, ctx, in, out);
static constexpr const char op_name[] = "isinf.out";
return internal::unary_ufunc_realhb_to_bool<op_name>(
[](auto x) -> bool { return std::isinf(x); }, ctx, in, out);
}

} // namespace native
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_isnan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ namespace native {
Tensor& isnan_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
// Lambda is syntactic sugar needed to workaround compilation on some older
// non-compatible distros where isnan is returning int rather than bool
return internal::unary_ufunc_realhb_to_bool(
[](double x) -> bool { return std::isnan(x); }, ctx, in, out);
static constexpr const char op_name[] = "isnan.out";
return internal::unary_ufunc_realhb_to_bool<op_name>(
[](auto x) -> bool { return std::isnan(x); }, ctx, in, out);
}

} // namespace native
Expand Down
4 changes: 3 additions & 1 deletion kernels/portable/cpu/op_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ namespace executor {
namespace native {

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

} // namespace native
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_log10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ namespace executor {
namespace native {

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

} // namespace native
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_log1p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ namespace executor {
namespace native {

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

} // namespace native
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_log2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ namespace executor {
namespace native {

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

} // namespace native
Expand Down
13 changes: 3 additions & 10 deletions kernels/portable/cpu/op_reciprocal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,11 @@
namespace torch {
namespace executor {
namespace native {
namespace {

double reciprocal(double x) {
return 1.0 / x;
}

} // namespace

Tensor&
reciprocal_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
return internal::unary_ufunc_realhbbf16_to_floathbf16(
reciprocal, ctx, in, out);
static constexpr const char op_name[] = "reciprocal.out";
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
[](auto x) { return executorch::math::reciprocal(x); }, ctx, in, out);
}

} // namespace native
Expand Down
11 changes: 3 additions & 8 deletions kernels/portable/cpu/op_rsqrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@
namespace torch {
namespace executor {
namespace native {
namespace {

double rsqrt(double x) {
return 1.0 / std::sqrt(x);
}

} // namespace

Tensor& rsqrt_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
return internal::unary_ufunc_realhbbf16_to_floathbf16(rsqrt, ctx, in, out);
static constexpr const char op_name[] = "rsqrt.out";
return internal::unary_ufunc_realhbbf16_to_floathbf16<op_name>(
[](auto x) { return executorch::math::rsqrt(x); }, ctx, in, out);
}

} // namespace native
Expand Down
4 changes: 3 additions & 1 deletion kernels/portable/cpu/op_sin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ namespace executor {
namespace native {

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

} // namespace native
Expand Down
Loading
Loading