Skip to content
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

NOCOMMIT: PoC: vectorize just expm1 #9586

Draft
wants to merge 2 commits into
base: gh/swolchok/396/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
54 changes: 52 additions & 2 deletions kernels/portable/cpu/op_expm1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,66 @@
*/

#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>

namespace torch {
namespace executor {
namespace native {

// REVIEW: I'm not entirely sure what the best way to implement this
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either the original vectorization PR or a preliminary PR will address this

// namespace is. Some options:
// 1) All in one file, with or without an `IMPLEMENT_VECTORIZED_MATH_OP` macro.
// 2) Include in each `unary_ufunc_*` op_foo.cpp, with or without an
// `IMPLEMENT_VECTORIZED_MATH_OP` macro.
//
// I think my preferred option would be (2) with a macro, but I've
// left the macro out for ease of reading this PoC PR.
namespace math {
using std::expm1;
#ifdef ET_USE_PYTORCH_HEADERS
template <typename T>
auto expm1(at::vec::Vectorized<T> x) {
// ATen knows to do this conversion because the TensorIterator for this op
// (and lots of similar ones in aten/src/ATen/native/UnaryOps.cpp) is created
// with build_borrowing_unary_float_op.
if constexpr (!executorch::runtime::is_floating_point<T>::value) {
return at::vec::convert<float>(x).expm1();
} else {
return x.expm1();
}
}
#endif
} // namespace math
Tensor& expm1_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
return internal::unary_ufunc_realhbbf16_to_floathbf16(
std::expm1, ctx, in, out);
ET_KERNEL_CHECK(ctx, tensor_is_floating_type(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.");

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

static constexpr const char op_name[] = "expm1.out";
ET_SWITCH_REALHBBF16_TYPES(in.scalar_type(), ctx, op_name, CTYPE_IN, [&] {
utils::apply_unitensor_elementwise_fn<
CTYPE_IN,
op_name,
utils::SupportedTensorDtypes::FLOATHBF16>(
[](auto x) { return math::expm1(x); },
ctx,
in,
utils::SupportedTensorDtypes::REALHBBF16,
out);
});

return out;
}

} // namespace native
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ ATEN_OPS = (
name = "op_expm1",
deps = [
"//executorch/kernels/portable/cpu/pattern:pattern",
"//executorch/kernels/portable/cpu/util:elementwise_util",
],
),
op_target(
Expand Down
Loading