def test_or(self): int16_tensor = ht.array([[1, 1], [2, 2]], dtype=ht.int16) int16_vector = ht.array([[3, 4]], dtype=ht.int16) self.assertTrue( ht.equal(int16_tensor | int16_vector, ht.bitwise_or(int16_tensor, int16_vector)) )
def test_bitwise_or(self): an_int_tensor = ht.array([[1, 2], [3, 4]]) an_int_vector = ht.array([2, 2]) another_int_vector = ht.array([2, 2, 2, 2]) int_result = ht.array([[3, 2], [3, 6]]) a_boolean_vector = ht.array([False, True, False, True]) another_boolean_vector = ht.array([False, False, True, True]) boolean_result = ht.array([False, True, True, True]) self.assertTrue( ht.equal(ht.bitwise_or(an_int_tensor, self.an_int_scalar), int_result)) self.assertTrue( ht.equal(ht.bitwise_or(an_int_tensor, an_int_vector), int_result)) self.assertTrue( ht.equal(ht.bitwise_or(a_boolean_vector, another_boolean_vector), boolean_result)) self.assertTrue( ht.equal( ht.bitwise_or(an_int_tensor.copy().resplit_(0), an_int_vector), int_result)) with self.assertRaises(TypeError): ht.bitwise_or(self.a_tensor, self.another_tensor) with self.assertRaises(ValueError): ht.bitwise_or(an_int_vector, another_int_vector) with self.assertRaises(TypeError): ht.bitwise_or(self.a_tensor, self.erroneous_type) with self.assertRaises(TypeError): ht.bitwise_or("T", "s") with self.assertRaises(TypeError): ht.bitwise_or(an_int_tensor, "s") with self.assertRaises(TypeError): ht.bitwise_or(self.an_int_scalar, "s") with self.assertRaises(TypeError): ht.bitwise_or("s", self.an_int_scalar) with self.assertRaises(TypeError): ht.bitwise_or(self.an_int_scalar, self.a_scalar)
def test_bitwise_or(self): an_int_tensor = ht.array([[1, 2], [3, 4]], device=ht_device) an_int_vector = ht.array([2, 2], device=ht_device) another_int_vector = ht.array([2, 2, 2, 2], device=ht_device) int_result = ht.array([[3, 2], [3, 6]], device=ht_device) int16_result = ht.array([[0, 2], [2, 0]], dtype=ht.int16, device=ht_device) a_boolean_vector = ht.array([False, True, False, True], device=ht_device) another_boolean_vector = ht.array([False, False, True, True], device=ht_device) boolean_result = ht.array([False, True, True, True], device=ht_device) self.assertTrue( ht.equal(ht.bitwise_or(an_int_tensor, self.an_int_scalar), int_result)) self.assertTrue( ht.equal(ht.bitwise_or(an_int_tensor, an_int_vector), int_result)) self.assertTrue( ht.equal(ht.bitwise_or(a_boolean_vector, another_boolean_vector), boolean_result)) with self.assertRaises(TypeError): ht.bitwise_or(self.a_tensor, self.another_tensor) with self.assertRaises(TypeError): ht.bitwise_or(another_int_vector, a_boolean_vector) with self.assertRaises(ValueError): ht.bitwise_or(an_int_vector, another_int_vector) with self.assertRaises(TypeError): ht.bitwise_or(self.a_tensor, self.errorneous_type) with self.assertRaises(TypeError): ht.bitwise_or("T", "s") with self.assertRaises(TypeError): ht.bitwise_or(an_int_tensor, "s") with self.assertRaises(TypeError): ht.bitwise_or(self.an_int_scalar, "s") with self.assertRaises(TypeError): ht.bitwise_or("s", self.an_int_scalar) with self.assertRaises(TypeError): ht.bitwise_or(self.an_int_scalar, self.a_scalar) with self.assertRaises(TypeError): ht.bitwise_or(int_result, int16_result) with self.assertRaises(NotImplementedError): ht.bitwise_or([1], 2) with self.assertRaises(TypeError): ht.bitwise_or(1, [2])
def test___binary_bit_op_broadcast(self): # broadcast without split left_tensor = ht.ones((4, 1), dtype=ht.int32) right_tensor = ht.ones((1, 2), dtype=ht.int32) result = left_tensor & right_tensor self.assertEqual(result.shape, (4, 2)) result = right_tensor & left_tensor self.assertEqual(result.shape, (4, 2)) # broadcast with split=0 for both operants left_tensor = ht.ones((4, 1), split=0, dtype=ht.int32) right_tensor = ht.ones((1, 2), split=0, dtype=ht.int32) result = left_tensor | right_tensor self.assertEqual(result.shape, (4, 2)) result = right_tensor | left_tensor self.assertEqual(result.shape, (4, 2)) # broadcast with split=1 for both operants left_tensor = ht.ones((4, 1), split=1, dtype=ht.int32) right_tensor = ht.ones((1, 2), split=1, dtype=ht.int32) result = left_tensor ^ right_tensor self.assertEqual(result.shape, (4, 2)) result = right_tensor ^ left_tensor self.assertEqual(result.shape, (4, 2)) # broadcast with split=1 for second operant left_tensor = ht.ones((4, 1), dtype=ht.int32) right_tensor = ht.ones((1, 2), split=1, dtype=ht.int32) result = left_tensor & right_tensor self.assertEqual(result.shape, (4, 2)) result = right_tensor & left_tensor self.assertEqual(result.shape, (4, 2)) # broadcast with split=0 for first operant left_tensor = ht.ones((4, 1), split=0, dtype=ht.int32) right_tensor = ht.ones((1, 2), dtype=ht.int32) result = left_tensor | right_tensor self.assertEqual(result.shape, (4, 2)) result = right_tensor | left_tensor self.assertEqual(result.shape, (4, 2)) # broadcast with unequal dimensions and one splitted tensor left_tensor = ht.ones((2, 4, 1), split=0, dtype=ht.int32) right_tensor = ht.ones((1, 2), dtype=ht.int32) result = left_tensor ^ right_tensor self.assertEqual(result.shape, (2, 4, 2)) result = right_tensor ^ left_tensor self.assertEqual(result.shape, (2, 4, 2)) # broadcast with unequal dimensions, a scalar, and one splitted tensor left_scalar = ht.np.int32(1) right_tensor = ht.ones((1, 2), split=0, dtype=ht.int32) result = ht.bitwise_or(left_scalar, right_tensor) self.assertEqual(result.shape, (1, 2)) result = right_tensor | left_scalar self.assertEqual(result.shape, (1, 2)) # broadcast with unequal dimensions and two splitted tensors left_tensor = ht.ones((4, 1, 3, 1, 2), split=0, dtype=torch.uint8) right_tensor = ht.ones((1, 3, 1), split=0, dtype=torch.uint8) result = left_tensor & right_tensor self.assertEqual(result.shape, (4, 1, 3, 3, 2)) result = right_tensor & left_tensor self.assertEqual(result.shape, (4, 1, 3, 3, 2)) with self.assertRaises(TypeError): ht.bitwise_and(ht.ones((1, 2)), "wrong type") with self.assertRaises(NotImplementedError): ht.bitwise_or(ht.ones((1, 2), dtype=ht.int32, split=0), ht.ones((1, 2), dtype=ht.int32, split=1))
def test___binary_bit_op_broadcast(self): # broadcast without split left_tensor = ht.ones((4, 1), dtype=ht.int32) right_tensor = ht.ones((1, 2), dtype=ht.int32) result = left_tensor & right_tensor self.assertEqual(result.shape, (4, 2)) result = right_tensor & left_tensor self.assertEqual(result.shape, (4, 2)) # broadcast with split=0 for both operants left_tensor = ht.ones((4, 1), split=0, dtype=ht.int32) right_tensor = ht.ones((1, 2), split=0, dtype=ht.int32) result = left_tensor | right_tensor self.assertEqual(result.shape, (4, 2)) result = right_tensor | left_tensor self.assertEqual(result.shape, (4, 2)) # broadcast with split=1 for both operants left_tensor = ht.ones((4, 1), split=1, dtype=ht.int32) right_tensor = ht.ones((1, 2), split=1, dtype=ht.int32) result = left_tensor ^ right_tensor self.assertEqual(result.shape, (4, 2)) result = right_tensor ^ left_tensor self.assertEqual(result.shape, (4, 2)) # broadcast with split=1 for second operant left_tensor = ht.ones((4, 1), dtype=ht.int32) right_tensor = ht.ones((1, 2), split=1, dtype=ht.int32) result = left_tensor & right_tensor self.assertEqual(result.shape, (4, 2)) result = right_tensor & left_tensor self.assertEqual(result.shape, (4, 2)) # broadcast with split=0 for first operant left_tensor = ht.ones((4, 1), split=0, dtype=ht.int32) right_tensor = ht.ones((1, 2), dtype=ht.int32) result = left_tensor | right_tensor self.assertEqual(result.shape, (4, 2)) result = right_tensor | left_tensor self.assertEqual(result.shape, (4, 2)) # broadcast with unequal dimensions and one splitted tensor left_tensor = ht.ones((2, 4, 1), split=0, dtype=ht.int32) right_tensor = ht.ones((1, 2), dtype=ht.int32) result = left_tensor ^ right_tensor self.assertEqual(result.shape, (2, 4, 2)) result = right_tensor ^ left_tensor self.assertEqual(result.shape, (2, 4, 2)) # broadcast with unequal dimensions, a scalar, and one splitted tensor left_scalar = ht.np.int32(1) right_tensor = ht.ones((1, 2), split=0, dtype=ht.int32) result = ht.bitwise_or(left_scalar, right_tensor) self.assertEqual(result.shape, (1, 2)) result = right_tensor | left_scalar self.assertEqual(result.shape, (1, 2)) # broadcast with unequal dimensions and two splitted tensors left_tensor = ht.ones((4, 1, 3, 1, 2), split=2, dtype=torch.uint8) right_tensor = ht.ones((1, 3, 1), split=0, dtype=torch.uint8) result = left_tensor & right_tensor self.assertEqual(result.shape, (4, 1, 3, 3, 2)) result = right_tensor & left_tensor self.assertEqual(result.shape, (4, 1, 3, 3, 2)) with self.assertRaises(TypeError): ht.bitwise_and(ht.ones((1, 2)), "wrong type") with self.assertRaises(NotImplementedError): ht.bitwise_or(ht.ones((1, 2), dtype=ht.int32, split=0), ht.ones((1, 2), dtype=ht.int32, split=1)) a = ht.ones((4, 4), split=None) b = ht.zeros((4, 4), split=0) self.assertTrue(ht.equal(a * b, b)) self.assertTrue(ht.equal(b * a, b)) self.assertTrue(ht.equal(a[0] * b[0], b[0])) self.assertTrue(ht.equal(b[0] * a[0], b[0])) self.assertTrue(ht.equal(a * b[0:1], b)) self.assertTrue(ht.equal(b[0:1] * a, b)) self.assertTrue(ht.equal(a[0:1] * b, b)) self.assertTrue(ht.equal(b * a[0:1], b)) c = ht.array([1, 2, 3, 4], comm=ht.MPI_SELF) with self.assertRaises(NotImplementedError): b + c with self.assertRaises(TypeError): ht.minimum(a, np.float128(1)) with self.assertRaises(TypeError): ht.minimum(np.float128(1), a) with self.assertRaises(NotImplementedError): a.resplit(1) * b with self.assertRaises(ValueError): a[2:] * b