Skip to content

Commit 271a277

Browse files
Add bitwise tests
Differential Revision: D64186612 Pull Request resolved: #6515
1 parent 241cd0c commit 271a277

File tree

3 files changed

+207
-15
lines changed

3 files changed

+207
-15
lines changed

kernels/test/op_bitwise_and_test.cpp

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,83 @@ using exec_aten::ScalarType;
2020
using exec_aten::Tensor;
2121
using torch::executor::testing::TensorFactory;
2222

23-
class OpwiseBitwiseAndTest : public OperatorTest {
23+
class OpBitwiseAndTensorOutTest : public OperatorTest {
2424
protected:
25-
Tensor& op_bitwise_and_scalar_out(
25+
Tensor& op_bitwise_and_tensor_out(
2626
const Tensor& self,
27-
const Scalar& other,
27+
const Tensor& other,
2828
Tensor& out) {
2929
return torch::executor::aten::bitwise_and_outf(context_, self, other, out);
3030
}
31+
};
3132

32-
Tensor& op_bitwise_and_tensor_out(
33+
class OpBitwiseAndScalarOutTest : public OperatorTest {
34+
protected:
35+
Tensor& op_bitwise_and_scalar_out(
3336
const Tensor& self,
34-
const Tensor& other,
37+
const Scalar& other,
3538
Tensor& out) {
3639
return torch::executor::aten::bitwise_and_outf(context_, self, other, out);
3740
}
3841
};
42+
43+
TEST_F(OpBitwiseAndTensorOutTest, SmokeTestInt) {
44+
TensorFactory<ScalarType::Int> tf;
45+
46+
Tensor a = tf.make({2, 2}, {2, 3, 2, 5});
47+
Tensor b = tf.make({2, 2}, {1, 6, 2, 3});
48+
49+
Tensor out = tf.zeros({2, 2});
50+
51+
op_bitwise_and_tensor_out(a, b, out);
52+
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {0, 2, 2, 1}));
53+
}
54+
55+
TEST_F(OpBitwiseAndTensorOutTest, SmokeTestBool) {
56+
TensorFactory<ScalarType::Bool> tf;
57+
58+
Tensor a = tf.make({2, 2}, {true, false, true, false});
59+
Tensor b = tf.make({2, 2}, {true, true, false, false});
60+
61+
Tensor out = tf.zeros({2, 2});
62+
63+
op_bitwise_and_tensor_out(a, b, out);
64+
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {true, false, false, false}));
65+
}
66+
67+
TEST_F(OpBitwiseAndTensorOutTest, SmokeTestMixed) {
68+
TensorFactory<ScalarType::Int> tfInt;
69+
TensorFactory<ScalarType::Bool> tfBool;
70+
71+
Tensor a = tfInt.make({2, 2}, {2, 3, 2, 5});
72+
Tensor b = tfBool.make({2, 2}, {true, true, false, false});
73+
74+
Tensor out = tfInt.zeros({2, 2});
75+
76+
op_bitwise_and_tensor_out(a, b, out);
77+
EXPECT_TENSOR_EQ(out, tfInt.make({2, 2}, {0, 1, 0, 0}));
78+
}
79+
80+
TEST_F(OpBitwiseAndScalarOutTest, SmokeTestInt) {
81+
TensorFactory<ScalarType::Int> tf;
82+
83+
Tensor a = tf.make({2, 2}, {2, 3, 2, 5});
84+
Scalar b = 6;
85+
86+
Tensor out = tf.zeros({2, 2});
87+
88+
op_bitwise_and_scalar_out(a, b, out);
89+
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {2, 2, 2, 4}));
90+
}
91+
92+
TEST_F(OpBitwiseAndScalarOutTest, SmokeTestBool) {
93+
TensorFactory<ScalarType::Bool> tf;
94+
95+
Tensor a = tf.make({2, 2}, {true, false, true, false});
96+
Scalar b = true;
97+
98+
Tensor out = tf.zeros({2, 2});
99+
100+
op_bitwise_and_scalar_out(a, b, out);
101+
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {true, false, true, false}));
102+
}

kernels/test/op_bitwise_or_test.cpp

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,83 @@ using exec_aten::ScalarType;
2020
using exec_aten::Tensor;
2121
using torch::executor::testing::TensorFactory;
2222

23-
class OpBitwiseOrTest : public OperatorTest {
23+
class OpBitwiseOrTensorOutTest : public OperatorTest {
2424
protected:
25-
Tensor& op_bitwise_or_scalar_out(
25+
Tensor& op_bitwise_or_tensor_out(
2626
const Tensor& self,
27-
const Scalar& other,
27+
const Tensor& other,
2828
Tensor& out) {
2929
return torch::executor::aten::bitwise_or_outf(context_, self, other, out);
3030
}
31+
};
3132

32-
Tensor& op_bitwise_or_tensor_out(
33+
class OpBitwiseOrScalarOutTest : public OperatorTest {
34+
protected:
35+
Tensor& op_bitwise_or_scalar_out(
3336
const Tensor& self,
34-
const Tensor& other,
37+
const Scalar& other,
3538
Tensor& out) {
3639
return torch::executor::aten::bitwise_or_outf(context_, self, other, out);
3740
}
3841
};
42+
43+
TEST_F(OpBitwiseOrTensorOutTest, SmokeTestInt) {
44+
TensorFactory<ScalarType::Int> tf;
45+
46+
Tensor a = tf.make({2, 2}, {2, 3, 2, 5});
47+
Tensor b = tf.make({2, 2}, {1, 6, 2, 3});
48+
49+
Tensor out = tf.zeros({2, 2});
50+
51+
op_bitwise_or_tensor_out(a, b, out);
52+
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {3, 7, 2, 7}));
53+
}
54+
55+
TEST_F(OpBitwiseOrTensorOutTest, SmokeTestBool) {
56+
TensorFactory<ScalarType::Bool> tf;
57+
58+
Tensor a = tf.make({2, 2}, {true, false, true, false});
59+
Tensor b = tf.make({2, 2}, {true, true, false, false});
60+
61+
Tensor out = tf.zeros({2, 2});
62+
63+
op_bitwise_or_tensor_out(a, b, out);
64+
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {true, true, true, false}));
65+
}
66+
67+
TEST_F(OpBitwiseOrTensorOutTest, SmokeTestMixed) {
68+
TensorFactory<ScalarType::Int> tfInt;
69+
TensorFactory<ScalarType::Bool> tfBool;
70+
71+
Tensor a = tfInt.make({2, 2}, {2, 3, 2, 5});
72+
Tensor b = tfBool.make({2, 2}, {true, true, false, false});
73+
74+
Tensor out = tfInt.zeros({2, 2});
75+
76+
op_bitwise_or_tensor_out(a, b, out);
77+
EXPECT_TENSOR_EQ(out, tfInt.make({2, 2}, {3, 3, 2, 5}));
78+
}
79+
80+
TEST_F(OpBitwiseOrScalarOutTest, SmokeTestInt) {
81+
TensorFactory<ScalarType::Int> tf;
82+
83+
Tensor a = tf.make({2, 2}, {2, 3, 2, 5});
84+
Scalar b = 6;
85+
86+
Tensor out = tf.zeros({2, 2});
87+
88+
op_bitwise_or_scalar_out(a, b, out);
89+
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {6, 7, 6, 7}));
90+
}
91+
92+
TEST_F(OpBitwiseOrScalarOutTest, SmokeTestBool) {
93+
TensorFactory<ScalarType::Bool> tf;
94+
95+
Tensor a = tf.make({2, 2}, {true, false, true, false});
96+
Scalar b = true;
97+
98+
Tensor out = tf.zeros({2, 2});
99+
100+
op_bitwise_or_scalar_out(a, b, out);
101+
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {true, true, true, true}));
102+
}

kernels/test/op_bitwise_xor_test.cpp

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,83 @@ using exec_aten::ScalarType;
2020
using exec_aten::Tensor;
2121
using torch::executor::testing::TensorFactory;
2222

23-
class OpBitwiseOrTest : public OperatorTest {
23+
class OpBitwiseXorTensorOutTest : public OperatorTest {
2424
protected:
25-
Tensor& op_bitwise_xor_scalar_out(
25+
Tensor& op_bitwise_xor_tensor_out(
2626
const Tensor& self,
27-
const Scalar& other,
27+
const Tensor& other,
2828
Tensor& out) {
2929
return torch::executor::aten::bitwise_xor_outf(context_, self, other, out);
3030
}
31+
};
3132

32-
Tensor& op_bitwise_xor_tensor_out(
33+
class OpBitwiseXorScalarOutTest : public OperatorTest {
34+
protected:
35+
Tensor& op_bitwise_xor_scalar_out(
3336
const Tensor& self,
34-
const Tensor& other,
37+
const Scalar& other,
3538
Tensor& out) {
3639
return torch::executor::aten::bitwise_xor_outf(context_, self, other, out);
3740
}
3841
};
42+
43+
TEST_F(OpBitwiseXorTensorOutTest, SmokeTestInt) {
44+
TensorFactory<ScalarType::Int> tf;
45+
46+
Tensor a = tf.make({2, 2}, {2, 3, 2, 5});
47+
Tensor b = tf.make({2, 2}, {1, 6, 2, 3});
48+
49+
Tensor out = tf.zeros({2, 2});
50+
51+
op_bitwise_xor_tensor_out(a, b, out);
52+
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {3, 5, 0, 6}));
53+
}
54+
55+
TEST_F(OpBitwiseXorTensorOutTest, SmokeTestBool) {
56+
TensorFactory<ScalarType::Bool> tf;
57+
58+
Tensor a = tf.make({2, 2}, {true, false, true, false});
59+
Tensor b = tf.make({2, 2}, {true, true, false, false});
60+
61+
Tensor out = tf.zeros({2, 2});
62+
63+
op_bitwise_xor_tensor_out(a, b, out);
64+
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {false, true, true, false}));
65+
}
66+
67+
TEST_F(OpBitwiseXorTensorOutTest, SmokeTestMixed) {
68+
TensorFactory<ScalarType::Int> tfInt;
69+
TensorFactory<ScalarType::Bool> tfBool;
70+
71+
Tensor a = tfInt.make({2, 2}, {2, 3, 2, 5});
72+
Tensor b = tfBool.make({2, 2}, {true, true, false, false});
73+
74+
Tensor out = tfInt.zeros({2, 2});
75+
76+
op_bitwise_xor_tensor_out(a, b, out);
77+
EXPECT_TENSOR_EQ(out, tfInt.make({2, 2}, {3, 2, 2, 5}));
78+
}
79+
80+
TEST_F(OpBitwiseXorScalarOutTest, SmokeTestInt) {
81+
TensorFactory<ScalarType::Int> tf;
82+
83+
Tensor a = tf.make({2, 2}, {2, 3, 2, 5});
84+
Scalar b = 6;
85+
86+
Tensor out = tf.zeros({2, 2});
87+
88+
op_bitwise_xor_scalar_out(a, b, out);
89+
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {4, 5, 4, 3}));
90+
}
91+
92+
TEST_F(OpBitwiseXorScalarOutTest, SmokeTestBool) {
93+
TensorFactory<ScalarType::Bool> tf;
94+
95+
Tensor a = tf.make({2, 2}, {true, false, true, false});
96+
Scalar b = true;
97+
98+
Tensor out = tf.zeros({2, 2});
99+
100+
op_bitwise_xor_scalar_out(a, b, out);
101+
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {false, true, false, true}));
102+
}

0 commit comments

Comments
 (0)