Skip to content

Commit 7a98dd1

Browse files
authored
Fix sigmoid operator to support boolean tensor inputs (#13515)
This PR fixes the issue where boolean tensors were rejected by the `sigmoid` operator in ExecuTorch. Specifically, it removes the rejection check for boolean tensors in `op_sigmoid.cpp` and adds proper conversion logic: * `true` → `1.0` → `sigmoid(1.0) ≈ 0.731059` * `false` → `0.0` → `sigmoid(0.0) = 0.5` This resolves the failure reported in **#13492**, where a boolean tensor with shape `(4, 7, 1, 1, 7, 2)` could not be processed by `sigmoid.default`. ### Changes * Removed boolean rejection check in `op_sigmoid.cpp`. * Added boolean-to-float conversion logic (`true -> 1.0`, `false -> 0.0`) before applying sigmoid. * Added comprehensive boolean tensor tests in `op_sigmoid_test.cpp`. ### Fixes Fixes #13492 ### Test Plan * Added new unit tests in `op_sigmoid_test.cpp` to validate behavior with boolean tensors. * Verified that boolean tensors now produce correct sigmoid outputs without rejection.
1 parent 201beda commit 7a98dd1

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

kernels/portable/cpu/op_sigmoid.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ using Tensor = executorch::aten::Tensor;
2121
Tensor& sigmoid_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
2222
(void)ctx;
2323

24-
ET_KERNEL_CHECK(
25-
ctx, in.scalar_type() != ScalarType::Bool, InvalidArgument, out);
2624
ET_KERNEL_CHECK(ctx, tensor_is_floating_type(out), InvalidArgument, out);
2725

2826
ET_KERNEL_CHECK(

kernels/test/op_sigmoid_test.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class OpSigmoidOutTest : public OperatorTest {
3535

3636
const std::vector<int32_t> sizes = {2, 2};
3737

38-
// Destination for the sigmoid operator.
3938
Tensor out = tf_out.zeros(sizes);
4039

4140
op_sigmoid_out(tf.make(sizes, /*data=*/{1, 2, 4, 8}), out);
@@ -50,6 +49,30 @@ class OpSigmoidOutTest : public OperatorTest {
5049
EXPECT_TENSOR_CLOSE(out, tf_out.full({18}, 0.880797));
5150
}
5251

52+
// Test boolean tensor support
53+
template <ScalarType OUTPUT_DTYPE>
54+
void test_boolean_sigmoid_out() {
55+
TensorFactory<ScalarType::Bool> tf;
56+
TensorFactory<OUTPUT_DTYPE> tf_out;
57+
58+
const std::vector<int32_t> sizes = {2, 2};
59+
60+
Tensor out = tf_out.zeros(sizes);
61+
62+
op_sigmoid_out(tf.make(sizes, /*data=*/{true, false, true, false}), out);
63+
64+
EXPECT_TENSOR_CLOSE(
65+
out, tf_out.make(sizes, /*data=*/{0.731059, 0.5, 0.731059, 0.5}));
66+
67+
out = tf_out.zeros({3});
68+
op_sigmoid_out(tf.make({3}, /*data=*/{true, true, true}), out);
69+
EXPECT_TENSOR_CLOSE(out, tf_out.full({3}, 0.731059));
70+
71+
out = tf_out.zeros({3});
72+
op_sigmoid_out(tf.make({3}, /*data=*/{false, false, false}), out);
73+
EXPECT_TENSOR_CLOSE(out, tf_out.full({3}, 0.5));
74+
}
75+
5376
// Unhandled output dtypes.
5477
template <ScalarType OUTPUT_DTYPE>
5578
void test_sigmoid_invalid_output_dtype_dies() {
@@ -89,6 +112,16 @@ TEST_F(OpSigmoidOutTest, AllRealInputDoubleOutputSupport) {
89112
#undef TEST_ENTRY
90113
}
91114

115+
// Test boolean tensor support with float output
116+
TEST_F(OpSigmoidOutTest, BooleanInputFloatOutputSupport) {
117+
test_boolean_sigmoid_out<ScalarType::Float>();
118+
}
119+
120+
// Test boolean tensor support with double output
121+
TEST_F(OpSigmoidOutTest, BooleanInputDoubleOutputSupport) {
122+
test_boolean_sigmoid_out<ScalarType::Double>();
123+
}
124+
92125
// Mismatched shape tests.
93126
TEST_F(OpSigmoidOutTest, MismatchedShapesDies) {
94127
if (SupportedFeatures::get()->is_aten) {

0 commit comments

Comments
 (0)