Skip to content

RFC: Specialize for non-mixed-dtype in elementwise_util #9388

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

Merged
merged 66 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
31a49e0
Update
swolchok Mar 19, 2025
9fcd885
Update
swolchok Mar 19, 2025
29d6de9
Update
swolchok Mar 19, 2025
79b908c
Update
swolchok Mar 19, 2025
fd62a07
Update
swolchok Mar 19, 2025
854c991
Update
swolchok Mar 19, 2025
def7ed4
Update
swolchok Mar 19, 2025
40c1b1b
Update
swolchok Mar 19, 2025
7c78357
Update
swolchok Mar 19, 2025
7ba269a
Update
swolchok Mar 19, 2025
edd45fb
Update
swolchok Mar 19, 2025
b9c545f
Update
swolchok Mar 20, 2025
3091007
Update
swolchok Mar 20, 2025
4a00cac
Update
swolchok Mar 20, 2025
21b81bf
Update
swolchok Mar 20, 2025
4c4add0
Update
swolchok Mar 20, 2025
8782a90
Update
swolchok Mar 20, 2025
75f8970
Update
swolchok Mar 20, 2025
2d19e75
Update
swolchok Mar 20, 2025
b61a8a2
Update
swolchok Mar 25, 2025
91161bd
Update
swolchok Mar 25, 2025
4add706
Update
swolchok Mar 25, 2025
5348a92
Update
swolchok Mar 25, 2025
001d72c
Update
swolchok Mar 25, 2025
e49080d
Update
swolchok Mar 25, 2025
44ee51a
Update
swolchok Mar 25, 2025
f659627
Update
swolchok Mar 25, 2025
f1c5429
Update
swolchok Mar 25, 2025
b34f04f
Update
swolchok Mar 25, 2025
f934bc0
Update
swolchok Mar 25, 2025
3a74f25
Update
swolchok Mar 25, 2025
9a93839
Update
swolchok Mar 26, 2025
bb16a55
Update
swolchok Mar 26, 2025
2242f1e
Update
swolchok Mar 26, 2025
7f57a19
Update
swolchok Mar 26, 2025
5d95c06
Update
swolchok Mar 26, 2025
42623bb
Update
swolchok Mar 26, 2025
4553283
Update
swolchok Mar 26, 2025
39610ad
Update
swolchok Mar 26, 2025
b3120fa
Update
swolchok Mar 26, 2025
ff2c358
Update
swolchok Mar 26, 2025
7086659
Update
swolchok Mar 28, 2025
e13de0e
Update
swolchok Mar 28, 2025
943ab82
Update
swolchok Mar 28, 2025
f22d039
Update
swolchok Mar 28, 2025
45ce46d
Update
swolchok Mar 28, 2025
754dba4
Update
swolchok Mar 28, 2025
34eb5d4
Update
swolchok Mar 28, 2025
ea9dc6f
Update
swolchok Mar 28, 2025
7d7859e
Update
swolchok Mar 28, 2025
b98829d
Update
swolchok Mar 28, 2025
3140910
Update
swolchok Mar 28, 2025
946f2e0
Update
swolchok Mar 28, 2025
7f2bbdb
Update
swolchok Apr 2, 2025
960315e
Update
swolchok Apr 2, 2025
9e42e93
Update
swolchok Apr 2, 2025
96d258e
Update
swolchok Apr 2, 2025
e6f66ab
Update
swolchok Apr 2, 2025
de9d52f
Update
swolchok Apr 2, 2025
20f3046
Update
swolchok Apr 2, 2025
3aa266d
Update
swolchok Apr 2, 2025
3c88a56
Update
swolchok Apr 2, 2025
153735d
Update
swolchok Apr 2, 2025
cac4293
Update
swolchok Apr 2, 2025
85451ea
Update
swolchok Apr 2, 2025
b0fc7f9
Update
swolchok Apr 22, 2025
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
8 changes: 5 additions & 3 deletions kernels/portable/cpu/op_mul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ Tensor& mul_out(
out);

ET_SWITCH_REALB_TYPES(compute_type, ctx, op_name, CTYPE_COMPUTE, [&]() {
utils::apply_bitensor_elementwise_fn<CTYPE_COMPUTE, op_name>(
utils::apply_bitensor_elementwise_fn<
CTYPE_COMPUTE,
op_name,
utils::SupportedTensorDtypes::REALHBBF16>(
[](const CTYPE_COMPUTE val_a, const CTYPE_COMPUTE val_b) {
return val_a * val_b;
},
Expand All @@ -61,8 +64,7 @@ Tensor& mul_out(
utils::SupportedTensorDtypes::REALHBBF16,
b,
utils::SupportedTensorDtypes::REALHBBF16,
out,
utils::SupportedTensorDtypes::REALHBBF16);
out);
});

return out;
Expand Down
19 changes: 19 additions & 0 deletions kernels/portable/cpu/util/dtype_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,25 @@ bool check_tensor_dtype(
SupportedTensorDtypes dtypes,
const ScalarType compute_type);

/// Return the one output type we are willing to emit specialized code
/// to handle, given a compute type of CTYPE_COMMON and supported
/// output types of out_dtypes.
template <typename CTYPE_COMPUTE>
inline constexpr ScalarType specialized_output_scalar_type(
SupportedTensorDtypes out_dtypes) {
switch (out_dtypes) {
case SupportedTensorDtypes::BOOL_OR_BYTE:
return ScalarType::Bool;
case SupportedTensorDtypes::REALHBBF16:
case SupportedTensorDtypes::REALHBF16:
case SupportedTensorDtypes::FLOATHBF16:
case SupportedTensorDtypes::INTB:
case SupportedTensorDtypes::SAME_AS_COMPUTE:
case SupportedTensorDtypes::SAME_AS_COMMON:
return CppTypeToScalarType<CTYPE_COMPUTE>::value;
}
}

} // namespace internal
} // namespace utils
} // namespace native
Expand Down
119 changes: 103 additions & 16 deletions kernels/portable/cpu/util/elementwise_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,44 @@ inline int64_t scalar_to<int64_t>(const Scalar& s) {
}

namespace internal {
template <
typename CTYPE_COMPUTE,
typename CTYPE_OUT,
typename Op,
typename... Args>
inline void dtype_specialized_elementwise_fn_impl(
const Op& compute_fun,
KernelRuntimeContext& ctx,
const Tensor& out,
Args... inputs) {
constexpr auto kNumInputs = sizeof...(inputs);
ET_DCHECK(((inputs.first->element_size() == sizeof(CTYPE_COMPUTE)) && ...));

::executorch::extension::parallel_for(
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 range =
BroadcastIndexesRange<kNumInputs>(out, (*inputs.first)...);
auto begin_it = range.begin();
begin_it += begin;
for (; (*begin_it)[0] < end; ++begin_it) {
const auto& indexes = *begin_it;
std::array<CTYPE_COMPUTE, kNumInputs> loaded_inputs;
for (const auto idx : c10::irange(kNumInputs)) {
loaded_inputs[idx] = inputs_data_ptrs[idx][indexes[idx + 1]];
}
data_out[indexes[0]] = std::apply(compute_fun, loaded_inputs);
}
});
}

template <typename CTYPE_COMPUTE, typename Op, typename... Args>
inline bool validate_elementwise_fn_inputs(
const Op& compute_fun,
Expand Down Expand Up @@ -81,18 +119,12 @@ template <
const char* op_name,
typename Op,
typename... Args>
inline void apply_elementwise_fn(
inline void apply_elementwise_fn_generic_impl(
const Op& compute_fun,
KernelRuntimeContext& ctx,
const Tensor& out,
SupportedTensorDtypes out_dtypes,
Args... inputs) {
const bool inputs_valid = validate_elementwise_fn_inputs<CTYPE_COMPUTE>(
compute_fun, ctx, out, out_dtypes, inputs...);
if (!inputs_valid) {
return;
}

constexpr auto kNumInputs = sizeof...(inputs);

struct InputInfo {
Expand Down Expand Up @@ -138,6 +170,63 @@ inline void apply_elementwise_fn(
});
}

template <
typename CTYPE_COMPUTE,
const char* op_name,
typename Op,
typename... Args>
inline void apply_elementwise_fn_runtime_out_dtypes(
const Op& compute_fun,
KernelRuntimeContext& ctx,
const Tensor& out,
SupportedTensorDtypes out_dtypes,
Args... inputs) {
const bool inputs_valid = validate_elementwise_fn_inputs<CTYPE_COMPUTE>(
compute_fun, ctx, out, out_dtypes, inputs...);
if (!inputs_valid) {
return;
}

apply_elementwise_fn_generic_impl<CTYPE_COMPUTE, op_name>(
compute_fun, ctx, out, out_dtypes, inputs...);
}

template <
typename CTYPE_COMPUTE,
const char* op_name,
SupportedTensorDtypes out_dtypes,
typename Op,
typename... Args>
inline void apply_elementwise_fn(
const Op& compute_fun,
KernelRuntimeContext& ctx,
const Tensor& out,
Args... inputs) {
const bool inputs_valid = validate_elementwise_fn_inputs<CTYPE_COMPUTE>(
compute_fun, ctx, out, out_dtypes, inputs...);
if (!inputs_valid) {
return;
}

constexpr auto compute_type = CppTypeToScalarType<CTYPE_COMPUTE>::value;
const bool all_inputs_compute_dtype =
((inputs.first->scalar_type() == compute_type) && ...);

constexpr ScalarType out_specialized_scalar_type =
specialized_output_scalar_type<CTYPE_COMPUTE>(out_dtypes);
Copy link
Contributor

Choose a reason for hiding this comment

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

here, CTYPE_COMPUTE

if (all_inputs_compute_dtype &&
out.scalar_type() == out_specialized_scalar_type) {
using CTYPE_OUT =
typename ScalarTypeToCppType<out_specialized_scalar_type>::type;
dtype_specialized_elementwise_fn_impl<CTYPE_COMPUTE, CTYPE_OUT>(
compute_fun, ctx, out, inputs...);
return;
}

apply_elementwise_fn_generic_impl<CTYPE_COMPUTE, op_name>(
compute_fun, ctx, out, out_dtypes, inputs...);
}

/// DEPRECATED: prefer the variant with out_dtypes in the template argument.
template <typename CTYPE_COMPUTE, const char* op_name, typename Op>
inline void apply_unitensor_elementwise_fn(
Expand All @@ -147,7 +236,7 @@ inline void apply_unitensor_elementwise_fn(
SupportedTensorDtypes a_dtypes,
const Tensor& out,
SupportedTensorDtypes out_dtypes) {
internal::apply_elementwise_fn<CTYPE_COMPUTE, op_name>(
internal::apply_elementwise_fn_runtime_out_dtypes<CTYPE_COMPUTE, op_name>(
compute_fun, ctx, out, out_dtypes, std::make_pair(&a, a_dtypes));
}

Expand All @@ -162,8 +251,8 @@ inline void apply_unitensor_elementwise_fn(
const Tensor& a,
SupportedTensorDtypes a_dtypes,
const Tensor& out) {
internal::apply_elementwise_fn<CTYPE_COMPUTE, op_name>(
compute_fun, ctx, out, out_dtypes, std::make_pair(&a, a_dtypes));
internal::apply_elementwise_fn<CTYPE_COMPUTE, op_name, out_dtypes>(
compute_fun, ctx, out, std::make_pair(&a, a_dtypes));
}

/**
Expand All @@ -179,7 +268,7 @@ inline void apply_bitensor_elementwise_fn(
SupportedTensorDtypes b_dtypes,
const Tensor& out,
SupportedTensorDtypes out_dtypes) {
internal::apply_elementwise_fn<CTYPE_COMPUTE, op_name>(
internal::apply_elementwise_fn_runtime_out_dtypes<CTYPE_COMPUTE, op_name>(
compute_fun,
ctx,
out,
Expand All @@ -206,11 +295,10 @@ inline void apply_bitensor_elementwise_fn(
const Tensor& b,
SupportedTensorDtypes b_dtypes,
const Tensor& out) {
internal::apply_elementwise_fn<CTYPE_COMPUTE, op_name>(
internal::apply_elementwise_fn<CTYPE_COMPUTE, op_name, out_dtypes>(
compute_fun,
ctx,
out,
out_dtypes,
std::make_pair(&a, a_dtypes),
std::make_pair(&b, b_dtypes));
}
Expand All @@ -230,7 +318,7 @@ inline void apply_tritensor_elementwise_fn(
SupportedTensorDtypes c_dtypes,
const Tensor& out,
SupportedTensorDtypes out_dtypes) {
internal::apply_elementwise_fn<CTYPE_COMPUTE, op_name>(
internal::apply_elementwise_fn_runtime_out_dtypes<CTYPE_COMPUTE, op_name>(
compute_fun,
ctx,
out,
Expand Down Expand Up @@ -275,11 +363,10 @@ inline void apply_tritensor_elementwise_fn(
const Tensor& c,
SupportedTensorDtypes c_dtypes,
const Tensor& out) {
internal::apply_elementwise_fn<CTYPE_COMPUTE, op_name>(
internal::apply_elementwise_fn<CTYPE_COMPUTE, op_name, out_dtypes>(
compute_fun,
ctx,
out,
out_dtypes,
std::make_pair(&a, a_dtypes),
std::make_pair(&b, b_dtypes),
std::make_pair(&c, c_dtypes));
Expand Down
Loading