Skip to content

Implement unary_ufunc functions using elementwise_util #9386

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

Draft
wants to merge 3 commits into
base: gh/swolchok/380/head
Choose a base branch
from
Draft
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
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 std::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 std::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 std::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 std::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 std::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 std::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_realh(std::ceil, ctx, in, out);
static constexpr const char op_name[] = "ceil.out";
return internal::unary_ufunc_realh<op_name>(
[](auto x) { return std::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 std::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 std::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 std::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 std::exp(x); }, ctx, in, out);
}

} // namespace native
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_expm1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ 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 std::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_realh(std::floor, ctx, in, out);
static constexpr const char op_name[] = "floor.out";
return internal::unary_ufunc_realh<op_name>(
[](auto x) { return std::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 std::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 std::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 std::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 std::log2(x); }, ctx, in, out);
}

} // namespace native
Expand Down
15 changes: 13 additions & 2 deletions kernels/portable/cpu/op_reciprocal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,27 @@ namespace executor {
namespace native {
namespace {

float reciprocal(float x) {
return 1.0f / x;
}

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

template <
typename Integer,
std::enable_if_t<std::is_integral_v<Integer>, bool> = true>
double reciprocal(Integer x) {
return reciprocal((double)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 reciprocal(x); }, ctx, in, out);
}

} // namespace native
Expand Down
17 changes: 9 additions & 8 deletions kernels/portable/cpu/op_rsqrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
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) {
auto result = std::sqrt(x);
return static_cast<decltype(result)>(1) / result;
},
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 std::sin(x); }, ctx, in, out);
}

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

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

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

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

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

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

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

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

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

Tensor& trunc_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
return internal::unary_ufunc_realh(std::trunc, ctx, in, out);
static constexpr const char op_name[] = "trunc.out";
return internal::unary_ufunc_realh<op_name>(
[](auto x) { return std::trunc(x); }, ctx, in, out);
}

} // namespace native
Expand Down
Loading
Loading