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

Add vectorization in elementwise_util #9432

Draft
wants to merge 7 commits into
base: gh/swolchok/385/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
4 changes: 4 additions & 0 deletions .lintrunner.toml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ exclude_patterns = [
'examples/**',
'exir/verification/bindings.cpp',
'extension/**',
# Uses properly-gated (ET_USE_PYTORCH_HEADERS) ATen include.
'kernels/portable/cpu/util/elementwise_util.h',
'kernels/portable/cpu/util/math_util.h',
'kernels/portable/cpu/util/vectorized_math.h',
'kernels/optimized/**',
'runtime/core/exec_aten/**',
# Want to be able to keep c10 in sync with PyTorch core.
Expand Down
2 changes: 1 addition & 1 deletion kernels/portable/cpu/op_atan2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Tensor& atan2_out(
op_name,
utils::SupportedTensorDtypes::FLOATHBF16>(
[](const auto val_a, const auto val_b) {
return std::atan2(val_a, val_b);
return executorch::math::atan2(val_a, val_b);
},
ctx,
a,
Expand Down
3 changes: 1 addition & 2 deletions kernels/portable/cpu/op_elu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ Tensor& elu_out(
CTYPE,
op_name,
utils::SupportedTensorDtypes::SAME_AS_COMMON>(
[negcoef, math_scale, math_input_scale](const auto x) {
// TODO: rewrite this to be vectorization-capable.
[negcoef, math_scale, math_input_scale](const CTYPE x) {
return MathT(x) <= MathT(0)
? std::expm1(MathT(x) * math_input_scale) * negcoef
: MathT(x) * math_scale;
Expand Down
8 changes: 3 additions & 5 deletions kernels/portable/cpu/op_fmod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Tensor& fmod_Tensor_out(
utils::SupportedTensorDtypes::REALHBF16>(
[&div_by_zero_error](
const CTYPE_COMPUTE val_a, const CTYPE_COMPUTE val_b) {
// TODO: rewrite this to be vectorization-capable.
// TODO: rewrite this to be vectorization-capable?
CTYPE_COMPUTE value = 0;
if (is_integral_type<CTYPE_COMPUTE, /*includeBool=*/true>::value) {
if (val_b == 0) {
Expand Down Expand Up @@ -138,10 +138,8 @@ Tensor& fmod_Scalar_out(
CTYPE_COMPUTE,
op_name,
utils::SupportedTensorDtypes::REALHBF16>(
[val_b](const CTYPE_COMPUTE val_a) {
// TODO: rewrite this to be vectorization-capable.
CTYPE_COMPUTE value = std::fmod(val_a, val_b);
return value;
[val_b](const auto val_a) {
return executorch::math::fmod(val_a, (decltype(val_a))val_b);
},
ctx,
a,
Expand Down
2 changes: 1 addition & 1 deletion kernels/portable/cpu/op_maximum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Tensor& maximum_out(
CTYPE_COMPUTE,
op_name,
utils::SupportedTensorDtypes::REALHBBF16>(
[](const CTYPE_COMPUTE val_a, const CTYPE_COMPUTE val_b) {
[](const auto val_a, const auto val_b) {
return utils::max_override(val_a, val_b);
},
ctx,
Expand Down
3 changes: 1 addition & 2 deletions kernels/portable/cpu/op_minimum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ Tensor& minimum_out(
CTYPE_COMPUTE,
op_name,
utils::SupportedTensorDtypes::REALHBBF16>(
[](const CTYPE_COMPUTE val_a, const CTYPE_COMPUTE val_b) {
// TODO: rewrite this to be vectorization-capable.
[](const auto val_a, const auto val_b) {
return utils::min_override(val_a, val_b);
},
ctx,
Expand Down
4 changes: 1 addition & 3 deletions kernels/portable/cpu/op_mul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ Tensor& mul_out(
CTYPE_COMPUTE,
op_name,
utils::SupportedTensorDtypes::REALHBBF16>(
[](const CTYPE_COMPUTE val_a, const CTYPE_COMPUTE val_b) {
return val_a * val_b;
},
[](const auto val_a, const auto val_b) { return val_a * val_b; },
ctx,
a,
utils::SupportedTensorDtypes::REALHBBF16,
Expand Down
4 changes: 2 additions & 2 deletions kernels/portable/cpu/op_pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ Tensor& pow_Tensor_Tensor_out(
CTYPE_COMPUTE,
op_name,
utils::SupportedTensorDtypes::REALHBF16>(
[](const CTYPE_COMPUTE val_a, const CTYPE_COMPUTE val_b) {
[](const auto val_a, const auto val_b) {
// TODO: rewrite this to be vectorization-capable.
return std::pow(val_a, val_b);
return executorch::math::pow(val_a, val_b);
},
ctx,
a,
Expand Down
5 changes: 3 additions & 2 deletions kernels/portable/cpu/op_sigmoid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ Tensor& sigmoid_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
CTYPE_COMPUTE,
op_name,
utils::SupportedTensorDtypes::FLOATHBF16>(
[](const auto val_in) -> CTYPE_COMPUTE {
// TODO: rewrite this to be vectorization-capable
[](const CTYPE_COMPUTE val_in) {
// TODO: rewrite this to be vectorization-capable; need
// unary - overload for Vectorized.
CTYPE_COMPUTE out_val = static_cast<CTYPE_COMPUTE>(1.0) /
(static_cast<CTYPE_COMPUTE>(1.0) + exp(-val_in));
return out_val;
Expand Down
2 changes: 1 addition & 1 deletion kernels/portable/cpu/op_where.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Tensor& where_out(
CTYPE_COMPUTE,
op_name,
utils::SupportedTensorDtypes::SAME_AS_COMMON>(
[](const auto val_a, const auto val_b, const auto val_c) {
[](const CTYPE_COMPUTE val_a, const CTYPE_COMPUTE val_b, const CTYPE_COMPUTE val_c) {
Copy link
Contributor

Choose a reason for hiding this comment

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

is this unrelated change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, we can't vectorize this op

return val_c ? val_a : val_b;
},
Comment on lines +50 to 52
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(ATen, and therefore our own optimized op_where, doesn't vectorize this)

ctx,
Expand Down
116 changes: 115 additions & 1 deletion kernels/portable/cpu/util/elementwise_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
#include <executorch/kernels/portable/cpu/util/broadcast_indexes_range.h>
#include <executorch/kernels/portable/cpu/util/broadcast_util.h>
#include <executorch/kernels/portable/cpu/util/dtype_util.h>
#include <executorch/kernels/portable/cpu/util/vectorized_math.h> // Make vectorization support easy for clients.
#include <executorch/runtime/kernel/kernel_runtime_context.h>
#include <executorch/runtime/kernel/thread_parallel_interface.h>

#ifdef ET_USE_PYTORCH_HEADERS
#include <ATen/cpu/vec/vec.h>
#endif // ET_USE_PYTORCH_HEADERS

#include <array>
#include <utility>

Expand Down Expand Up @@ -51,6 +56,34 @@ inline int64_t scalar_to<int64_t>(const Scalar& s) {
}

namespace internal {
template <typename Ignore, typename T>
using ignore_first_yield_second = T;
Copy link
Contributor

Choose a reason for hiding this comment

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

I like these names


#ifdef ET_USE_PYTORCH_HEADERS
// Can I call a function of type Op with sizeof...(Args) arguments of type
// at::vec::Vectorized<CTYPE_COMPUTE>?
//
// See [NOTE: Generic lambdas] below for requirements on Op.
template <typename CTYPE_COMPUTE, typename Op, typename... Args>
constexpr bool can_use_vectorized() {
using Vec = at::vec::Vectorized<CTYPE_COMPUTE>;
if constexpr (std::is_invocable_v<
Op,
ignore_first_yield_second<Args, Vec>...>) {
// For bool, we will get a false positive if we rely on only the
Copy link
Contributor

Choose a reason for hiding this comment

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

you mean bool return type?

Copy link
Contributor Author

@swolchok swolchok Apr 2, 2025

Choose a reason for hiding this comment

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

bool ctype_compute

// is_invocable_v check above because at::vec::Vectorized is
// implicitly convertible to a pointer, which makes it implicitly
// convertible to bool (which was 15 minutes of fun to debug). Also
// just seems like good hygiene to make sure we get the Vectorized
// we're expecting.
return std::is_same_v<
std::invoke_result_t<Op, ignore_first_yield_second<Args, Vec>...>,
Vec>;
}
return false;
}
#endif // ET_USE_PYTORCH_HEADERS

template <
typename CTYPE_COMPUTE,
typename CTYPE_OUT,
Expand All @@ -61,8 +94,71 @@ inline void dtype_specialized_elementwise_fn_impl(
KernelRuntimeContext& ctx,
const Tensor& out,
Args... inputs) {
static_assert(
(std::is_same_v<Args, std::pair<const Tensor*, SupportedTensorDtypes>> &&
...));
constexpr auto kNumInputs = sizeof...(inputs);
ET_DCHECK(((inputs.first->element_size() == sizeof(CTYPE_COMPUTE)) && ...));
// All inputs must be of type CTYPE_COMPUTE.
ET_DCHECK(
((inputs.first->scalar_type() ==
CppTypeToScalarType<CTYPE_COMPUTE>::value) &&
...));
Comment on lines +103 to +105
Copy link
Contributor

Choose a reason for hiding this comment

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

Man, this is a good reminder of all the template meta programming magic


#ifdef ET_USE_PYTORCH_HEADERS
if constexpr (can_use_vectorized<CTYPE_COMPUTE, Op, Args...>()) {
const bool any_is_broadcasted =
!(torch::executor::internal::sizes_match_ignoring_leading_1s(
inputs.first->sizes(), out.sizes()) &&
...);
if (!any_is_broadcasted) {
using Vec = at::vec::Vectorized<CTYPE_COMPUTE>;
Copy link
Contributor

Choose a reason for hiding this comment

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

So the one point of contention for me is that why do we need vectorized_math.h which largely is doing trampoline to underlying vectorized methods. Mainly you dont even need to use can_use_vectorized, because on non accelerated platforms Vectorized falls back to scalar impl even if Vec::size() != `. Maybe you said that the generated code would be worse if forced Vectorized, but I am not sure why. Rest makes sense.

However, place where I can potentially see that being useful is for dtype that Vectorized doesnt support but for float I am not sure. So maybe if you can clarify that it would help.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why do we need vectorized_math.h which largely is doing trampoline to underlying vectorized methods

without it, you can't take the same lambda you already wrote for scalars and reuse it for Vectorized (the change isn't zero because you have to point at executorch::math, but crucially it doesn't require separate code)

::executorch::extension::parallel_for(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think doing this blindly for each op is a bit risky in that, no all multithreading is always better. some ops benefit from smaller grain size while others with larger

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IIRC xnnpack blindly parallelizes absolutely everything; we're doing strictly better here

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah I am not comparing with xnnpack. In fact one bit part of the reason why we ended up leveraging optimized op lib for some of the llama stuff for exactly that reason. That it blindly parallelized everything and that actually hurt perf

0,
out.numel(),
::executorch::extension::internal::GRAIN_SIZE,
[&](const auto begin, const auto end) {
std::array<const CTYPE_COMPUTE*, kNumInputs> inputs_data_ptrs = {
inputs.first->template const_data_ptr<CTYPE_COMPUTE>()...};

CTYPE_OUT* const data_out = out.mutable_data_ptr<CTYPE_OUT>();

const auto vectorized_begin =
begin + (Vec::size() - begin % Vec::size()) % Vec::size();
Comment on lines +125 to +126
Copy link
Contributor

Choose a reason for hiding this comment

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

Feels like something that has chances of bug. Hope we test this enough. I would doubt if our test cases will exercise this code path.

Copy link
Contributor

Choose a reason for hiding this comment

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

although I do see you treat scalar left overs of both head and tails separately

const auto vectorized_end = end - (end % Vec::size());
// Scalar prologue.
for (const auto idx : c10::irange(begin, vectorized_begin)) {
std::array<CTYPE_COMPUTE, kNumInputs> loaded_inputs;
for (const auto input_idx : c10::irange(kNumInputs)) {
loaded_inputs[input_idx] = inputs_data_ptrs[input_idx][idx];
}
data_out[idx] = std::apply(compute_fun, loaded_inputs);
}

// Main vectorized loop.
for (auto idx = vectorized_begin; idx < vectorized_end;
idx += Vec::size()) {
std::array<Vec, kNumInputs> loaded_vec_inputs;
for (const auto input_idx : c10::irange(kNumInputs)) {
loaded_vec_inputs[input_idx] =
Vec::loadu(&inputs_data_ptrs[input_idx][idx]);
}
auto result_vec = std::apply(compute_fun, loaded_vec_inputs);
result_vec.store(&data_out[idx]);
}

// Scalar epilogue.
for (const auto idx : c10::irange(vectorized_end, end)) {
std::array<CTYPE_COMPUTE, kNumInputs> loaded_inputs;
for (const auto input_idx : c10::irange(kNumInputs)) {
loaded_inputs[input_idx] = inputs_data_ptrs[input_idx][idx];
}
data_out[idx] = std::apply(compute_fun, loaded_inputs);
}
});
return;
}
}
#endif

::executorch::extension::parallel_for(
0,
Expand Down Expand Up @@ -240,6 +336,19 @@ inline void apply_unitensor_elementwise_fn(
compute_fun, ctx, out, out_dtypes, std::make_pair(&a, a_dtypes));
}

/**
* Useful for unary elementwise operators. For each element of the
* input, call Op and write to the corresponding element of the
* output. Tensor broadcasting is applied wherever it is required.
*
* [NOTE: Generic lambdas]: If Op is a *generic* lambda (i.e., one with `auto`
* parameters; normal lambdas are fine), it must fulfill one of the
* following conditions. Either:
* 1) It must in fact compile when passed at::vec::Vectorized<CTYPE_COMPUTE>, or
* 2) It must be actively SFINAE-friendly, as per the C++17 examples in
* https://stackoverflow.com/questions/76525790/detecting-if-a-generic-lambda-with-certain-arguments-is-invocable
* .
*/
template <
typename CTYPE_COMPUTE,
const char* op_name,
Expand Down Expand Up @@ -281,6 +390,8 @@ inline void apply_bitensor_elementwise_fn(
* Useful for bi-tensor elementwise operators. For each element of the inputs,
* perform a computation and write to the corresponding element of the output.
* Tensor broadcasting is applied wherever it is required.
* See [NOTE: Generic lambdas] if you want to pass a generic lambda for
* compute_fun.
*/
template <
typename CTYPE_COMPUTE,
Expand Down Expand Up @@ -347,6 +458,9 @@ inline void apply_tritensor_elementwise_fn(
*
* static constexpr const char op_name[] = "my_op";
* apply_ternary_elementwise_fn<CTYPE_COMPUTE, op_name>.
*
* See [NOTE: Generic lambdas] if you want to pass a generic lambda for
* compute_fun.
*/
template <
typename CTYPE_COMPUTE,
Expand Down
19 changes: 19 additions & 0 deletions kernels/portable/cpu/util/math_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

#pragma once

#ifdef ET_USE_PYTORCH_HEADERS
#include <ATen/cpu/vec/vec.h>
#endif

namespace torch {
namespace executor {
namespace native {
Expand Down Expand Up @@ -138,6 +142,21 @@ T max_override(T a, T b) {
return b;
}

#ifdef ET_USE_PYTORCH_HEADERS
template <typename T>
at::vec::Vectorized<T> min_override(
at::vec::Vectorized<T> a,
at::vec::Vectorized<T> b) {
return at::vec::minimum(a, b);
}

template <typename T>
at::vec::Vectorized<T> max_override(
at::vec::Vectorized<T> a,
at::vec::Vectorized<T> b) {
return at::vec::maximum(a, b);
}
#endif
/**
* There is a slight difference in how std::fmod works compared to how ATen
* determines remainders:
Expand Down
15 changes: 15 additions & 0 deletions kernels/portable/cpu/util/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def define_common_targets():
"//executorch/kernels/portable/cpu/util:slice_util",
"//executorch/kernels/portable/cpu/util:elementwise_util",
"//executorch/kernels/portable/cpu/util:upsample_util",
"//executorch/kernels/portable/cpu/util:vectorized_math",
"//executorch/runtime/kernel:thread_parallel_interface",
],
visibility = ["//executorch/...", "@EXECUTORCH_CLIENTS"],
Expand Down Expand Up @@ -110,6 +111,8 @@ def define_common_targets():
":broadcast_indexes_range",
":broadcast_util",
":dtype_util",
":vectorized_math",
"//executorch/runtime/core/portable_type/c10/c10:aten_headers_for_executorch",
"//executorch/runtime/kernel:kernel_runtime_context",
"//executorch/runtime/kernel:thread_parallel_interface",
],
Expand Down Expand Up @@ -260,6 +263,9 @@ def define_common_targets():
srcs = [],
exported_headers = ["math_util.h"],
visibility = ["//executorch/kernels/portable/cpu/...", "//executorch/kernels/quantized/..."],
exported_deps = [
"//executorch/runtime/core/portable_type/c10/c10:aten_headers_for_executorch",
],
)

runtime.cxx_library(
Expand Down Expand Up @@ -307,6 +313,15 @@ def define_common_targets():
],
)

runtime.cxx_library(
name = "vectorized_math",
exported_headers = ["vectorized_math.h"],
visibility = ["//executorch/..."],
exported_deps = [
"//executorch/runtime/core/exec_aten/util:scalar_type_util",
],
)

# Utility functions that can be used by operators that perform reduction
for aten_mode in get_aten_mode_options():
suffix = "_aten" if aten_mode else ""
Expand Down
Loading
Loading