Skip to content

[ET][Portable] Fix & cleanup op logit #695

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

Closed
Closed
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
12 changes: 6 additions & 6 deletions kernels/portable/cpu/op_logit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ Tensor& logit_out(
(void)ctx;

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

ET_SWITCH_REAL_TYPES_AND(Bool, in.scalar_type(), ctx, "logit", CTYPE_IN, [&] {
ET_SWITCH_FLOAT_TYPES(out.scalar_type(), ctx, "logit", CTYPE_OUT, [&] {
ScalarType in_type = in.scalar_type();
ScalarType out_type = out.scalar_type();
ET_SWITCH_REAL_TYPES_AND(Bool, in_type, ctx, __func__, CTYPE_IN, [&] {
ET_SWITCH_FLOAT_TYPES(out_type, ctx, __func__, CTYPE_OUT, [&] {
apply_unary_map_fn(
[eps](const CTYPE_IN val_in) {
CTYPE_OUT xi = static_cast<CTYPE_OUT>(val_in);
Expand All @@ -41,7 +42,6 @@ Tensor& logit_out(
xi = 1 - eps.value();
}
}
ET_CHECK_MSG(xi > 0.0 && xi < 1.0, "input must be in (0, 1).");
return static_cast<CTYPE_OUT>(
log(xi / (static_cast<CTYPE_OUT>(1.0) - xi)));
},
Expand Down
12 changes: 4 additions & 8 deletions kernels/test/op_logit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ void test_integer_logit_out() {
// Destination for the logit operator.
Tensor out = tf_out.zeros(sizes);

ET_EXPECT_KERNEL_FAILURE(
op_logit_out(tf.make(sizes, /*data=*/{1, 2, 4, 8}), 0, out));
op_logit_out(tf.make(sizes, /*data=*/{1, 2, 4, 8}), 0, out);
EXPECT_TENSOR_CLOSE(
out,
tf_out.make(sizes, /*data=*/{INFINITY, INFINITY, INFINITY, INFINITY}));
}

template <>
Expand Down Expand Up @@ -79,19 +81,13 @@ void test_integer_logit_out_eps_set() {
}

TEST(OpLogitOutKernelTest, AllRealInputFloatOutputSupport) {
if (torch::executor::testing::SupportedFeatures::get()->is_aten) {
GTEST_SKIP() << "ATen kernel can handle this";
}
#define TEST_ENTRY(ctype, dtype) \
test_integer_logit_out<ScalarType::dtype, ScalarType::Float>();
ET_FORALL_REAL_TYPES(TEST_ENTRY);
#undef TEST_ENTRY
}

TEST(OpLogitOutKernelTest, AllRealInputDoubleOutputSupport) {
if (torch::executor::testing::SupportedFeatures::get()->is_aten) {
GTEST_SKIP() << "ATen kernel can handle this";
}
#define TEST_ENTRY(ctype, dtype) \
test_integer_logit_out<ScalarType::dtype, ScalarType::Double>();
ET_FORALL_REAL_TYPES(TEST_ENTRY);
Expand Down