-
Notifications
You must be signed in to change notification settings - Fork 608
Add optimized op_where #8866
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
Add optimized op_where #8866
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
f1ace77
Update
swolchok d3a0f67
Update
swolchok a78277d
Update
swolchok 6b6180c
Update
swolchok 7cbe7a1
Update
swolchok 8e8ce33
Update
swolchok 093a6d3
Update
swolchok 77f039c
Update
swolchok c4e4541
Update
swolchok d3edbcb
Update
swolchok bf96b84
Update
swolchok f7e4fc9
Update
swolchok 90da9b1
Update
swolchok 29d1122
Update
swolchok 14a55be
Update
swolchok 3e20161
Update
swolchok 1497adb
Update
swolchok 8ade738
Update
swolchok 0a18dab
Update
swolchok 62ab1e7
Update
swolchok a9bbae4
Update
swolchok 7bce689
Update
swolchok d208351
Update
swolchok 289d53c
Update
swolchok 6893c27
Update
swolchok 7cc62d2
Update
swolchok 570845e
Update
swolchok e52508d
Update
swolchok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
#include <executorch/kernels/portable/cpu/util/elementwise_util.h> | ||
#include <executorch/runtime/kernel/kernel_includes.h> | ||
#include <iostream> | ||
|
||
namespace torch { | ||
namespace executor { | ||
namespace native { | ||
|
||
Tensor& opt_where_out( | ||
KernelRuntimeContext& ctx, | ||
const Tensor& cond, | ||
const Tensor& a, | ||
const Tensor& b, | ||
Tensor& out) { | ||
// Common Dtype | ||
ScalarType common_type = promoteTypes(a.scalar_type(), b.scalar_type()); | ||
|
||
// Check Common Dtype | ||
ET_KERNEL_CHECK(ctx, common_type == out.scalar_type(), InvalidArgument, out); | ||
|
||
// Check Dim Order | ||
ET_KERNEL_CHECK( | ||
ctx, tensors_have_same_dim_order(cond, a, b, out), InvalidArgument, out); | ||
|
||
// Resize | ||
ET_KERNEL_CHECK( | ||
ctx, | ||
resize_to_broadcast_target_size(a, b, cond, out) == Error::Ok, | ||
InvalidArgument, | ||
out); | ||
|
||
// Compute Dtype | ||
ScalarType compute_type = utils::get_compute_type(common_type); | ||
|
||
// @lint-ignore CLANGTIDY facebook-hte-CArray | ||
static constexpr const char op_name[] = "where.self_out"; | ||
|
||
if (a.scalar_type() == b.scalar_type() && | ||
a.scalar_type() == out.scalar_type() && a.scalar_type() == compute_type && | ||
// Using a Byte tensor for cond has been deprecated for a long time. | ||
cond.scalar_type() == ScalarType::Bool) { | ||
auto out_numel = out.numel(); | ||
ET_SWITCH_REALB_TYPES(compute_type, ctx, op_name, CTYPE_COMPUTE, [&]() { | ||
const bool a_is_broadcasted = !out.sizes().equals(a.sizes()); | ||
const bool b_is_broadcasted = !out.sizes().equals(b.sizes()); | ||
const bool cond_is_broadcasted = !out.sizes().equals(cond.sizes()); | ||
const bool any_is_broadcasted = | ||
(a_is_broadcasted || b_is_broadcasted || cond_is_broadcasted); | ||
const CTYPE_COMPUTE* const data_a = a.const_data_ptr<CTYPE_COMPUTE>(); | ||
const CTYPE_COMPUTE* const data_b = b.const_data_ptr<CTYPE_COMPUTE>(); | ||
const bool* const data_cond = cond.const_data_ptr<bool>(); | ||
CTYPE_COMPUTE* const data_out = out.data_ptr<CTYPE_COMPUTE>(); | ||
if (any_is_broadcasted) { | ||
for (const auto [out_index, a_index, b_index, cond_index] : | ||
BroadcastIndexesRange<3>(out, a, b, cond)) { | ||
data_out[out_index] = | ||
data_cond[cond_index] ? data_a[a_index] : data_b[b_index]; | ||
} | ||
} else { | ||
for (const auto i : c10::irange(out_numel)) { | ||
data_out[i] = data_cond[i] ? data_a[i] : data_b[i]; | ||
} | ||
} | ||
}); | ||
} else { | ||
// Fall back for mixed dtype to keep code size and compile time | ||
// reasonable. | ||
ET_SWITCH_REALB_TYPES(compute_type, ctx, op_name, CTYPE_COMPUTE, [&]() { | ||
utils::apply_tritensor_elementwise_fn<CTYPE_COMPUTE, op_name>( | ||
[](const CTYPE_COMPUTE val_a, | ||
const CTYPE_COMPUTE val_b, | ||
const CTYPE_COMPUTE val_c) { return val_c ? val_a : val_b; }, | ||
ctx, | ||
a, | ||
utils::SupportedTensorDtypes::REALHBBF16, | ||
b, | ||
utils::SupportedTensorDtypes::REALHBBF16, | ||
cond, | ||
utils::SupportedTensorDtypes::BOOL_OR_BYTE, | ||
out, | ||
utils::SupportedTensorDtypes::SAME_AS_COMMON); | ||
}); | ||
} | ||
|
||
return out; | ||
} | ||
|
||
} // namespace native | ||
} // namespace executor | ||
} // namespace torch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love how clean it reads :)
Couple of high level comments,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can't possibly know this in general.
not currently on my agenda, but I may have to come back.