Skip to content
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
111 changes: 40 additions & 71 deletions kernels/optimized/cpu/op_le.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,25 @@ Tensor& opt_le_tensor_out(
const Tensor& a,
const Tensor& b,
Tensor& out) {
(void)ctx;

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

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

ET_KERNEL_CHECK(
ctx,
resize_to_broadcast_target_size(a, b, out) == Error::Ok,
InvalidArgument,
out);

// @lint-ignore CLANGTIDY facebook-hte-CArray
static constexpr const char op_name[] = "le.Tensor_out";

// Check for optimized broadcast paths
auto selected_optimized_path = select_optimized_path(a, b, out);
if (selected_optimized_path == ElementwiseOptimizedPath::kTreatAs1d) {
// Resize for dynamic shape
auto error = resize_to_broadcast_target_size(a, b, out);
ET_KERNEL_CHECK_MSG(
ctx,
error == Error::Ok,
InvalidArgument,
out,
"Failed to resize output tensor.");

ET_SWITCH_REALB_TYPES(a_type, ctx, "le.Tensor_out", CTYPE, [&]() {
ET_SWITCH_REALB_TYPES(a_type, ctx, op_name, CTYPE, [&]() {
using Vec = at::vec::Vectorized<CTYPE>;
at::vec::map2<CTYPE>(
[](Vec x, Vec y) { return x.le(y); },
Expand All @@ -55,16 +56,13 @@ Tensor& opt_le_tensor_out(
});
} else if (selected_optimized_path != ElementwiseOptimizedPath::kNone) {
// Handle optimized broadcast cases
ET_SWITCH_REALB_TYPES(out_type, ctx, "le.Tensor_out", CTYPE, [&]() {
ET_SWITCH_REALB_TYPES(out_type, ctx, op_name, CTYPE, [&]() {
auto le_lambda = [](auto x, auto y) { return x.le(y); };
torch::executor::handle_broadcast_elementwise<CTYPE>(
ctx, le_lambda, a, b, out, selected_optimized_path);
});
} else {
// @lint-ignore CLANGTIDY facebook-hte-CArray
static constexpr const char op_name[] = "le.Tensor_out";
return internal::comparison_tensor_out<std::less_equal, op_name>(
ctx, a, b, out);
internal::comparison_tensor_out<std::less_equal, op_name>(ctx, a, b, out);
}

return out;
Expand All @@ -75,66 +73,37 @@ Tensor& opt_le_scalar_out(
const Tensor& a,
const Scalar& b,
Tensor& out) {
(void)ctx;

// Resize for dynamic shape
auto error = resize_tensor(out, a.sizes());
ET_KERNEL_CHECK_MSG(
ctx,
error == Error::Ok,
InvalidArgument,
out,
"Failed to resize output tensor.");

ScalarType a_type = a.scalar_type();
ScalarType b_type = utils::get_scalar_dtype(b);
ScalarType common_type = promoteTypes(a_type, b_type);
ScalarType common_type = utils::promote_type_with_scalar(a_type, b);
ScalarType out_type = out.scalar_type();

if (a_type == common_type && a_type == out_type) {
ET_SWITCH_REAL_TYPES_AND(Bool, a_type, ctx, "le.Scalar_out", CTYPE, [&]() {
ET_SWITCH_REAL_TYPES_AND(
Bool, b_type, ctx, "le.Scalar_out", CTYPE_B, [&]() {
CTYPE_B b_val = 0;
ET_EXTRACT_SCALAR(b, b_val);
CTYPE b_casted = static_cast<CTYPE>(b_val);
using Vec = at::vec::Vectorized<CTYPE>;
at::vec::map<CTYPE>(
[b_casted](Vec x) { return x.le(Vec(b_casted)); },
out.mutable_data_ptr<CTYPE>(),
a.const_data_ptr<CTYPE>(),
a.numel());
});
ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(a, out), InvalidArgument, out);

ET_KERNEL_CHECK(
ctx, resize_tensor(out, a.sizes()) == Error::Ok, InvalidArgument, out);

// @lint-ignore CLANGTIDY facebook-hte-CArray
static constexpr const char op_name[] = "le.Scalar_out";

if (a_type == common_type && a_type == out_type &&
a_type != ScalarType::Half && a_type != ScalarType::BFloat16) {
ET_SWITCH_REALB_TYPES(a_type, ctx, op_name, CTYPE, [&]() {
ET_SWITCH_REALB_TYPES(b_type, ctx, op_name, CTYPE_B, [&]() {
CTYPE_B b_val = 0;
ET_EXTRACT_SCALAR(b, b_val);
CTYPE b_casted = static_cast<CTYPE>(b_val);
using Vec = at::vec::Vectorized<CTYPE>;
at::vec::map<CTYPE>(
[b_casted](Vec x) { return x.le(Vec(b_casted)); },
out.mutable_data_ptr<CTYPE>(),
a.const_data_ptr<CTYPE>(),
a.numel());
});
});
} else {
ET_SWITCH_REAL_TYPES_AND(
Bool, a_type, ctx, "le.Scalar_out", CTYPE_A, [&]() {
ET_SWITCH_REAL_TYPES_AND(
Bool, b_type, ctx, "le.Scalar_out", CTYPE_B, [&]() {
ET_SWITCH_REAL_TYPES_AND(
Bool, common_type, ctx, "le.Scalar_out", CTYPE_IN, [&]() {
ET_SWITCH_REAL_TYPES_AND(
Bool,
out_type,
ctx,
"le.Scalar_out",
CTYPE_OUT,
[&]() {
CTYPE_B b_val = 0;
ET_EXTRACT_SCALAR(b, b_val);
CTYPE_IN b_casted = static_cast<CTYPE_IN>(b_val);
const size_t n = a.numel();
const CTYPE_A* a_data = a.const_data_ptr<CTYPE_A>();
CTYPE_OUT* out_data =
out.mutable_data_ptr<CTYPE_OUT>();
for (auto i = 0; i < n; ++i) {
out_data[i] = static_cast<CTYPE_OUT>(
static_cast<CTYPE_IN>(a_data[i]) <= b_casted);
}
});
});
});
});
internal::comparison_scalar_out<std::less_equal, op_name>(ctx, a, b, out);
}

return out;
Expand Down
12 changes: 6 additions & 6 deletions kernels/test/op_le_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ TEST_F(OpLeScalarOutTest, AllRealInputBoolOutputSupport) {
#define TEST_ENTRY(ctype_in, dtype_in, ctype_out, dtype_out) \
test_le_scalar_out<ScalarType::dtype_in, ScalarType::dtype_out>();

#define TEST_FORALL_OUT_TYPES(ctype_in, dtype_in) \
ET_FORALL_REAL_TYPES_WITH2(ctype_in, dtype_in, TEST_ENTRY) \
#define TEST_FORALL_OUT_TYPES(ctype_in, dtype_in) \
ET_FORALL_REALHBF16_TYPES_WITH2(ctype_in, dtype_in, TEST_ENTRY) \
test_le_scalar_out<ScalarType::dtype_in, ScalarType::Bool>();

ET_FORALL_REAL_TYPES(TEST_FORALL_OUT_TYPES)
ET_FORALL_REALHBF16_TYPES(TEST_FORALL_OUT_TYPES)

#undef TEST_FORALL_OUT_TYPES
#undef TEST_ENTRY
Expand Down Expand Up @@ -124,11 +124,11 @@ TEST_F(OpLeTensorOutTest, AllDtypesSupported) {
#define TEST_ENTRY(ctype_in, dtype_in, ctype_out, dtype_out) \
test_dtype<ScalarType::dtype_in, ScalarType::dtype_out>();

#define TEST_FORALL_OUT_TYPES(ctype_in, dtype_in) \
ET_FORALL_REAL_TYPES_WITH2(ctype_in, dtype_in, TEST_ENTRY) \
#define TEST_FORALL_OUT_TYPES(ctype_in, dtype_in) \
ET_FORALL_REALHBF16_TYPES_WITH2(ctype_in, dtype_in, TEST_ENTRY) \
test_dtype<ScalarType::dtype_in, ScalarType::Bool>();

ET_FORALL_REAL_TYPES(TEST_FORALL_OUT_TYPES);
ET_FORALL_REALHBF16_TYPES(TEST_FORALL_OUT_TYPES);

#undef TEST_FORALL_OUT_TYPES
#undef TEST_ENTRY
Expand Down
Loading