Skip to content

Commit

Permalink
Add bitwise tests
Browse files Browse the repository at this point in the history
Differential Revision: D64186612

Pull Request resolved: #6515
  • Loading branch information
manuelcandales authored Jan 7, 2025
1 parent 241cd0c commit 271a277
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 15 deletions.
74 changes: 69 additions & 5 deletions kernels/test/op_bitwise_and_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,83 @@ using exec_aten::ScalarType;
using exec_aten::Tensor;
using torch::executor::testing::TensorFactory;

class OpwiseBitwiseAndTest : public OperatorTest {
class OpBitwiseAndTensorOutTest : public OperatorTest {
protected:
Tensor& op_bitwise_and_scalar_out(
Tensor& op_bitwise_and_tensor_out(
const Tensor& self,
const Scalar& other,
const Tensor& other,
Tensor& out) {
return torch::executor::aten::bitwise_and_outf(context_, self, other, out);
}
};

Tensor& op_bitwise_and_tensor_out(
class OpBitwiseAndScalarOutTest : public OperatorTest {
protected:
Tensor& op_bitwise_and_scalar_out(
const Tensor& self,
const Tensor& other,
const Scalar& other,
Tensor& out) {
return torch::executor::aten::bitwise_and_outf(context_, self, other, out);
}
};

TEST_F(OpBitwiseAndTensorOutTest, SmokeTestInt) {
TensorFactory<ScalarType::Int> tf;

Tensor a = tf.make({2, 2}, {2, 3, 2, 5});
Tensor b = tf.make({2, 2}, {1, 6, 2, 3});

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

op_bitwise_and_tensor_out(a, b, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {0, 2, 2, 1}));
}

TEST_F(OpBitwiseAndTensorOutTest, SmokeTestBool) {
TensorFactory<ScalarType::Bool> tf;

Tensor a = tf.make({2, 2}, {true, false, true, false});
Tensor b = tf.make({2, 2}, {true, true, false, false});

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

op_bitwise_and_tensor_out(a, b, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {true, false, false, false}));
}

TEST_F(OpBitwiseAndTensorOutTest, SmokeTestMixed) {
TensorFactory<ScalarType::Int> tfInt;
TensorFactory<ScalarType::Bool> tfBool;

Tensor a = tfInt.make({2, 2}, {2, 3, 2, 5});
Tensor b = tfBool.make({2, 2}, {true, true, false, false});

Tensor out = tfInt.zeros({2, 2});

op_bitwise_and_tensor_out(a, b, out);
EXPECT_TENSOR_EQ(out, tfInt.make({2, 2}, {0, 1, 0, 0}));
}

TEST_F(OpBitwiseAndScalarOutTest, SmokeTestInt) {
TensorFactory<ScalarType::Int> tf;

Tensor a = tf.make({2, 2}, {2, 3, 2, 5});
Scalar b = 6;

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

op_bitwise_and_scalar_out(a, b, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {2, 2, 2, 4}));
}

TEST_F(OpBitwiseAndScalarOutTest, SmokeTestBool) {
TensorFactory<ScalarType::Bool> tf;

Tensor a = tf.make({2, 2}, {true, false, true, false});
Scalar b = true;

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

op_bitwise_and_scalar_out(a, b, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {true, false, true, false}));
}
74 changes: 69 additions & 5 deletions kernels/test/op_bitwise_or_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,83 @@ using exec_aten::ScalarType;
using exec_aten::Tensor;
using torch::executor::testing::TensorFactory;

class OpBitwiseOrTest : public OperatorTest {
class OpBitwiseOrTensorOutTest : public OperatorTest {
protected:
Tensor& op_bitwise_or_scalar_out(
Tensor& op_bitwise_or_tensor_out(
const Tensor& self,
const Scalar& other,
const Tensor& other,
Tensor& out) {
return torch::executor::aten::bitwise_or_outf(context_, self, other, out);
}
};

Tensor& op_bitwise_or_tensor_out(
class OpBitwiseOrScalarOutTest : public OperatorTest {
protected:
Tensor& op_bitwise_or_scalar_out(
const Tensor& self,
const Tensor& other,
const Scalar& other,
Tensor& out) {
return torch::executor::aten::bitwise_or_outf(context_, self, other, out);
}
};

TEST_F(OpBitwiseOrTensorOutTest, SmokeTestInt) {
TensorFactory<ScalarType::Int> tf;

Tensor a = tf.make({2, 2}, {2, 3, 2, 5});
Tensor b = tf.make({2, 2}, {1, 6, 2, 3});

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

op_bitwise_or_tensor_out(a, b, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {3, 7, 2, 7}));
}

TEST_F(OpBitwiseOrTensorOutTest, SmokeTestBool) {
TensorFactory<ScalarType::Bool> tf;

Tensor a = tf.make({2, 2}, {true, false, true, false});
Tensor b = tf.make({2, 2}, {true, true, false, false});

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

op_bitwise_or_tensor_out(a, b, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {true, true, true, false}));
}

TEST_F(OpBitwiseOrTensorOutTest, SmokeTestMixed) {
TensorFactory<ScalarType::Int> tfInt;
TensorFactory<ScalarType::Bool> tfBool;

Tensor a = tfInt.make({2, 2}, {2, 3, 2, 5});
Tensor b = tfBool.make({2, 2}, {true, true, false, false});

Tensor out = tfInt.zeros({2, 2});

op_bitwise_or_tensor_out(a, b, out);
EXPECT_TENSOR_EQ(out, tfInt.make({2, 2}, {3, 3, 2, 5}));
}

TEST_F(OpBitwiseOrScalarOutTest, SmokeTestInt) {
TensorFactory<ScalarType::Int> tf;

Tensor a = tf.make({2, 2}, {2, 3, 2, 5});
Scalar b = 6;

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

op_bitwise_or_scalar_out(a, b, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {6, 7, 6, 7}));
}

TEST_F(OpBitwiseOrScalarOutTest, SmokeTestBool) {
TensorFactory<ScalarType::Bool> tf;

Tensor a = tf.make({2, 2}, {true, false, true, false});
Scalar b = true;

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

op_bitwise_or_scalar_out(a, b, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {true, true, true, true}));
}
74 changes: 69 additions & 5 deletions kernels/test/op_bitwise_xor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,83 @@ using exec_aten::ScalarType;
using exec_aten::Tensor;
using torch::executor::testing::TensorFactory;

class OpBitwiseOrTest : public OperatorTest {
class OpBitwiseXorTensorOutTest : public OperatorTest {
protected:
Tensor& op_bitwise_xor_scalar_out(
Tensor& op_bitwise_xor_tensor_out(
const Tensor& self,
const Scalar& other,
const Tensor& other,
Tensor& out) {
return torch::executor::aten::bitwise_xor_outf(context_, self, other, out);
}
};

Tensor& op_bitwise_xor_tensor_out(
class OpBitwiseXorScalarOutTest : public OperatorTest {
protected:
Tensor& op_bitwise_xor_scalar_out(
const Tensor& self,
const Tensor& other,
const Scalar& other,
Tensor& out) {
return torch::executor::aten::bitwise_xor_outf(context_, self, other, out);
}
};

TEST_F(OpBitwiseXorTensorOutTest, SmokeTestInt) {
TensorFactory<ScalarType::Int> tf;

Tensor a = tf.make({2, 2}, {2, 3, 2, 5});
Tensor b = tf.make({2, 2}, {1, 6, 2, 3});

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

op_bitwise_xor_tensor_out(a, b, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {3, 5, 0, 6}));
}

TEST_F(OpBitwiseXorTensorOutTest, SmokeTestBool) {
TensorFactory<ScalarType::Bool> tf;

Tensor a = tf.make({2, 2}, {true, false, true, false});
Tensor b = tf.make({2, 2}, {true, true, false, false});

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

op_bitwise_xor_tensor_out(a, b, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {false, true, true, false}));
}

TEST_F(OpBitwiseXorTensorOutTest, SmokeTestMixed) {
TensorFactory<ScalarType::Int> tfInt;
TensorFactory<ScalarType::Bool> tfBool;

Tensor a = tfInt.make({2, 2}, {2, 3, 2, 5});
Tensor b = tfBool.make({2, 2}, {true, true, false, false});

Tensor out = tfInt.zeros({2, 2});

op_bitwise_xor_tensor_out(a, b, out);
EXPECT_TENSOR_EQ(out, tfInt.make({2, 2}, {3, 2, 2, 5}));
}

TEST_F(OpBitwiseXorScalarOutTest, SmokeTestInt) {
TensorFactory<ScalarType::Int> tf;

Tensor a = tf.make({2, 2}, {2, 3, 2, 5});
Scalar b = 6;

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

op_bitwise_xor_scalar_out(a, b, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {4, 5, 4, 3}));
}

TEST_F(OpBitwiseXorScalarOutTest, SmokeTestBool) {
TensorFactory<ScalarType::Bool> tf;

Tensor a = tf.make({2, 2}, {true, false, true, false});
Scalar b = true;

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

op_bitwise_xor_scalar_out(a, b, out);
EXPECT_TENSOR_EQ(out, tf.make({2, 2}, {false, true, false, true}));
}

0 comments on commit 271a277

Please sign in to comment.