Skip to content

Enable optimized op_le broadcast against 1 element tensor #9507

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 1 commit into from
Mar 22, 2025
Merged
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
57 changes: 53 additions & 4 deletions kernels/optimized/cpu/op_le.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <executorch/kernels/optimized/vec/functional.h>
#include <executorch/kernels/optimized/vec/vec.h>
#include <executorch/kernels/portable/cpu/scalar_utils.h>
#include <executorch/kernels/portable/cpu/util/broadcast_util.h>
#include <executorch/runtime/kernel/kernel_includes.h>
#include <executorch/runtime/platform/assert.h>

Expand All @@ -26,6 +27,58 @@ Tensor& opt_le_tensor_out(
Tensor& out) {
(void)ctx;

ScalarType a_type = a.scalar_type();
ScalarType b_type = b.scalar_type();
ScalarType out_type = out.scalar_type();

if (a.numel() == 1 || b.numel() == 1) {
const Tensor* tensor;
const Tensor* scalar;
ScalarType tensor_type;
ScalarType scalar_type;
if (a.numel() == 1) {
tensor = &b;
tensor_type = b_type;
scalar = &a;
scalar_type = a_type;
} else {
tensor = &a;
tensor_type = a_type;
scalar = &b;
scalar_type = b_type;
}
ET_KERNEL_CHECK(
ctx,
resize_to_broadcast_target_size(a, b, out) == Error::Ok,
InvalidArgument,
out);

constexpr auto name = "le.Tensor_out";

ET_SWITCH_REALB_TYPES(tensor_type, ctx, name, CTYPE, [&]() {
ET_SWITCH_REALB_TYPES(scalar_type, ctx, name, CTYPE_SCALAR, [&]() {
CTYPE_SCALAR scalar_val = *scalar->const_data_ptr<CTYPE_SCALAR>();
CTYPE scalar_casted = static_cast<CTYPE>(scalar_val);

using Vec = executorch::vec::Vectorized<CTYPE>;
if (a.numel() == 1) {
executorch::vec::map<CTYPE>(
[scalar_casted](Vec x) { return Vec(scalar_casted).le(x); },
out.mutable_data_ptr<CTYPE>(),
tensor->const_data_ptr<CTYPE>(),
out.numel());
} else {
executorch::vec::map<CTYPE>(
[scalar_casted](Vec x) { return x.le(Vec(scalar_casted)); },
out.mutable_data_ptr<CTYPE>(),
tensor->const_data_ptr<CTYPE>(),
out.numel());
}
});
});
return out;
}

ET_KERNEL_CHECK(ctx, tensors_have_same_shape(a, b), InvalidArgument, out);

// Resize for dynamic shape
Expand All @@ -37,10 +90,6 @@ Tensor& opt_le_tensor_out(
out,
"Failed to resize output tensor.");

ScalarType a_type = a.scalar_type();
ScalarType b_type = b.scalar_type();
ScalarType out_type = out.scalar_type();

if (a_type == b_type && a_type == out_type) {
ET_SWITCH_REAL_TYPES_AND(
Bool, out_type, ctx, "le.Tensor_out", CTYPE, [&]() {
Expand Down
1 change: 1 addition & 0 deletions kernels/optimized/cpu/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ _OPTIMIZED_ATEN_OPS = (
name = "op_le",
deps = [
"//executorch/kernels/portable/cpu:scalar_utils",
"//executorch/kernels/portable/cpu/util:broadcast_util",
],
),
op_target(
Expand Down
12 changes: 12 additions & 0 deletions kernels/test/op_le_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,15 @@ TEST_F(OpLeTensorOutTest, DynamicOutShapeTest) {
op_le_tensor_out(a, b, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {false, true, true, false}));
}

TEST_F(OpLeTensorOutTest, BroadcastTest) {
TensorFactory<ScalarType::Int> tf;

Tensor a = tf.make(/*sizes=*/{4}, /*data=*/{2, 3, 2, 4});
Tensor b = tf.make({1, 1}, {3});

Tensor out = tf.zeros({1, 4});

op_le_tensor_out(a, b, out);
EXPECT_TENSOR_EQ(out, tf.make({1, 4}, {true, true, true, false}));
}
Loading