def test_zeros_like(self): # scalar like_int = ht.zeros_like(3) self.assertIsInstance(like_int, ht.tensor) self.assertEqual(like_int.shape, (1, )) self.assertEqual(like_int.lshape, (1, )) self.assertEqual(like_int.split, None) self.assertEqual(like_int.dtype, ht.int32) self.assertEqual((like_int._tensor__array == 0).all().item(), 1) # sequence like_str = ht.zeros_like('abc') self.assertIsInstance(like_str, ht.tensor) self.assertEqual(like_str.shape, (3, )) self.assertEqual(like_str.lshape, (3, )) self.assertEqual(like_str.split, None) self.assertEqual(like_str.dtype, ht.float32) self.assertEqual((like_str._tensor__array == 0).all().item(), 1) # elaborate tensor ones = ht.ones(( 2, 3, ), dtype=ht.uint8) like_ones = ht.zeros_like(ones) self.assertIsInstance(like_ones, ht.tensor) self.assertEqual(like_ones.shape, ( 2, 3, )) self.assertEqual(like_ones.lshape, ( 2, 3, )) self.assertEqual(like_ones.split, None) self.assertEqual(like_ones.dtype, ht.uint8) self.assertEqual((like_ones._tensor__array == 0).all().item(), 1) # elaborate tensor with split ones_split = ht.ones(( 2, 3, ), dtype=ht.uint8, split=0) like_ones_split = ht.zeros_like(ones_split) self.assertIsInstance(like_ones_split, ht.tensor) self.assertEqual(like_ones_split.shape, ( 2, 3, )) self.assertLessEqual(like_ones_split.lshape[0], 2) self.assertEqual(like_ones_split.lshape[1], 3) self.assertEqual(like_ones_split.split, 0) self.assertEqual(like_ones_split.dtype, ht.uint8) self.assertEqual((like_ones_split._tensor__array == 0).all().item(), 1) # exceptions with self.assertRaises(TypeError): ht.zeros_like(ones, dtype='abc') with self.assertRaises(TypeError): ht.zeros_like(ones, split='axis')
def test_numpy(self): # ToDo: numpy does not work for distributed tensors du to issue# # Add additional tests if the issue is solved a = np.random.randn(10, 8) b = ht.array(a, device=ht_device) self.assertIsInstance(b.numpy(), np.ndarray) self.assertEqual(b.numpy().shape, a.shape) self.assertEqual(b.numpy().tolist(), b._DNDarray__array.cpu().numpy().tolist()) a = ht.ones((10, 8), dtype=ht.float32, device=ht_device) b = np.ones((2, 2)).astype("float32") self.assertEqual(a.numpy().dtype, b.dtype) a = ht.ones((10, 8), dtype=ht.float64, device=ht_device) b = np.ones((2, 2)).astype("float64") self.assertEqual(a.numpy().dtype, b.dtype) a = ht.ones((10, 8), dtype=ht.int32, device=ht_device) b = np.ones((2, 2)).astype("int32") self.assertEqual(a.numpy().dtype, b.dtype) a = ht.ones((10, 8), dtype=ht.int64, device=ht_device) b = np.ones((2, 2)).astype("int64") self.assertEqual(a.numpy().dtype, b.dtype)
def test_isfinite(self): a = ht.array([1, ht.inf, -ht.inf, ht.nan]) s = ht.array([True, False, False, False]) r = ht.isfinite(a) self.assertEqual(r.shape, s.shape) self.assertEqual(r.dtype, s.dtype) self.assertEqual(r.device, s.device) self.assertTrue(ht.equal(r, s)) a = ht.array([1, ht.inf, -ht.inf, ht.nan], split=0) s = ht.array([True, False, False, False], split=0) r = ht.isfinite(a) self.assertEqual(r.shape, s.shape) self.assertEqual(r.dtype, s.dtype) self.assertEqual(r.device, s.device) self.assertTrue(ht.equal(r, s)) a = ht.ones((6, 6), dtype=ht.bool, split=0) s = ht.ones((6, 6), dtype=ht.bool, split=0) r = ht.isfinite(a) self.assertEqual(r.shape, s.shape) self.assertEqual(r.dtype, s.dtype) self.assertEqual(r.device, s.device) self.assertTrue(ht.equal(r, s)) a = ht.ones((5, 5), dtype=ht.int, split=1) s = ht.ones((5, 5), dtype=ht.bool, split=1) r = ht.isfinite(a) self.assertEqual(r.shape, s.shape) self.assertEqual(r.dtype, s.dtype) self.assertEqual(r.device, s.device) self.assertTrue(ht.equal(r, s))
def test_real(self): a = ht.array([1.0, 1.0j, 1 + 1j, -2 + 2j, 3 - 3j]) real = ht.real(a) res = ht.array([1.0, 0.0, 1.0, -2.0, 3.0], dtype=ht.float32, device=self.device) self.assertIs(real.device, self.device) self.assertIs(real.dtype, ht.float) self.assertEqual(real.shape, (5,)) self.assertTrue(ht.equal(real, res)) a = ht.array([1.0, 1.0j, 1 + 1j, -2 + 2j, 3 - 3j], split=0) real = ht.real(a) res = ht.array([1.0, 0.0, 1.0, -2.0, 3.0], dtype=ht.float32, device=self.device, split=0) self.assertIs(real.device, self.device) self.assertIs(real.dtype, ht.float) self.assertEqual(real.shape, (5,)) self.assertTrue(ht.equal(real, res)) # Not complex a = ht.ones((4, 4), split=1) real = a.real res = ht.ones((4, 4), split=1) self.assertIs(real.device, self.device) self.assertIs(real.dtype, ht.float32) self.assertEqual(real.shape, (4, 4)) self.assertIs(real, a)
def test_isreal(self): a = ht.array([1, 1.2, 1 + 1j, 1 + 0j]) s = ht.array([True, True, False, True]) r = ht.isreal(a) self.assertEqual(r.shape, s.shape) self.assertEqual(r.dtype, s.dtype) self.assertEqual(r.device, s.device) self.assertTrue(ht.equal(r, s)) a = ht.array([1, 1.2, True], split=0) s = ht.array([True, True, True], split=0) r = ht.isreal(a) self.assertEqual(r.shape, s.shape) self.assertEqual(r.dtype, s.dtype) self.assertEqual(r.device, s.device) self.assertTrue(ht.equal(r, s)) a = ht.ones((6, 6), dtype=ht.bool, split=0) s = ht.ones((6, 6), dtype=ht.bool, split=0) r = ht.isreal(a) self.assertEqual(r.shape, s.shape) self.assertEqual(r.dtype, s.dtype) self.assertEqual(r.device, s.device) self.assertTrue(ht.equal(r, s)) a = ht.full((5, 5), 1 + 1j, dtype=ht.int, split=1) s = ht.zeros((5, 5), dtype=ht.bool, split=1) r = ht.isreal(a) self.assertEqual(r.shape, s.shape) self.assertEqual(r.dtype, s.dtype) self.assertEqual(r.device, s.device) self.assertTrue(ht.equal(r, s))
def test_isposinf(self): a = ht.array([1, ht.inf, -ht.inf, ht.nan]) s = ht.array([False, True, False, False]) r = ht.isposinf(a) self.assertEqual(r.shape, s.shape) self.assertEqual(r.dtype, s.dtype) self.assertEqual(r.device, s.device) self.assertTrue(ht.equal(r, s)) a = ht.array([1, ht.inf, -ht.inf, ht.nan], split=0) out = ht.empty(4, dtype=ht.bool, split=0) s = ht.array([False, True, False, False], split=0) ht.isposinf(a, out) self.assertEqual(out.shape, s.shape) self.assertEqual(out.dtype, s.dtype) self.assertEqual(out.device, s.device) self.assertTrue(ht.equal(r, s)) a = ht.ones((6, 6), dtype=ht.bool, split=0) s = ht.zeros((6, 6), dtype=ht.bool, split=0) r = ht.isposinf(a) self.assertEqual(r.shape, s.shape) self.assertEqual(r.dtype, s.dtype) self.assertEqual(r.device, s.device) self.assertTrue(ht.equal(r, s)) a = ht.ones((5, 5), dtype=ht.int, split=1) s = ht.zeros((5, 5), dtype=ht.bool, split=1) r = ht.isposinf(a) self.assertEqual(r.shape, s.shape) self.assertEqual(r.dtype, s.dtype) self.assertEqual(r.device, s.device) self.assertTrue(ht.equal(r, s))
def test_flatten(self): a = ht.ones((4, 4, 4), split=1) result = ht.ones((64, ), split=0) flat = a.flatten() self.assertEqual(flat.shape, result.shape) self.assertTrue(ht.equal(flat, result))
def test_conjugate(self): a = ht.array([1.0, 1.0j, 1 + 1j, -2 + 2j, 3 - 3j]) conj = ht.conjugate(a) res = ht.array([1 - 0j, -1j, 1 - 1j, -2 - 2j, 3 + 3j], dtype=ht.complex64, device=self.device) self.assertIs(conj.device, self.device) self.assertIs(conj.dtype, ht.complex64) self.assertEqual(conj.shape, (5, )) # equal on complex numbers does not work on PyTorch self.assertTrue(ht.equal(ht.real(conj), ht.real(res))) self.assertTrue(ht.equal(ht.imag(conj), ht.imag(res))) a = ht.array([[1.0, 1.0j], [1 + 1j, -2 + 2j], [3 - 3j, -4 - 4j]], split=0) conj = ht.conjugate(a) res = ht.array( [[1 - 0j, -1j], [1 - 1j, -2 - 2j], [3 + 3j, -4 + 4j]], dtype=ht.complex64, device=self.device, split=0, ) self.assertIs(conj.device, self.device) self.assertIs(conj.dtype, ht.complex64) self.assertEqual(conj.shape, (3, 2)) # equal on complex numbers does not work on PyTorch self.assertTrue(ht.equal(ht.real(conj), ht.real(res))) self.assertTrue(ht.equal(ht.imag(conj), ht.imag(res))) a = ht.array([[1.0, 1.0j], [1 + 1j, -2 + 2j], [3 - 3j, -4 - 4j]], dtype=ht.complex128, split=1) conj = ht.conjugate(a) res = ht.array( [[1 - 0j, -1j], [1 - 1j, -2 - 2j], [3 + 3j, -4 + 4j]], dtype=ht.complex128, device=self.device, split=1, ) self.assertIs(conj.device, self.device) self.assertIs(conj.dtype, ht.complex128) self.assertEqual(conj.shape, (3, 2)) # equal on complex numbers does not work on PyTorch self.assertTrue(ht.equal(ht.real(conj), ht.real(res))) self.assertTrue(ht.equal(ht.imag(conj), ht.imag(res))) # Not complex a = ht.ones((4, 4)) conj = ht.conj(a) res = ht.ones((4, 4)) self.assertIs(conj.device, self.device) self.assertIs(conj.dtype, ht.float32) self.assertEqual(conj.shape, (4, 4)) self.assertTrue(ht.equal(conj, res))
def test_where(self): # cases to test # no x and y a = ht.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], split=None) cond = a > 3 wh = ht.where(cond) self.assertEqual(wh.gshape, (6, 2)) self.assertEqual(wh.dtype, ht.int64) self.assertEqual(wh.split, None) # split a = ht.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], split=1) cond = a > 3 wh = ht.where(cond) self.assertEqual(wh.gshape, (6, 2)) self.assertEqual(wh.dtype, ht.int64) self.assertEqual(wh.split, 0) # not split cond a = ht.array([[0.0, 1.0, 2.0], [0.0, 2.0, 4.0], [0.0, 3.0, 6.0]], split=None) res = ht.array([[0.0, 1.0, 2.0], [0.0, 2.0, -1.0], [0.0, 3.0, -1.0]], split=None) wh = ht.where(a < 4.0, a, -1) self.assertTrue( ht.equal(a[ht.nonzero(a < 4)], ht.array([0.0, 1.0, 2.0, 0.0, 2.0, 0.0, 3.0]))) self.assertTrue(ht.equal(wh, res)) self.assertEqual(wh.gshape, (3, 3)) self.assertEqual(wh.dtype, ht.float64) # split cond a = ht.array([[0.0, 1.0, 2.0], [0.0, 2.0, 4.0], [0.0, 3.0, 6.0]], split=0) res = ht.array([[0.0, 1.0, 2.0], [0.0, 2.0, -1.0], [0.0, 3.0, -1.0]], split=0) wh = ht.where(a < 4.0, a, -1) self.assertTrue(ht.all(wh[ht.nonzero(a >= 4)] == -1)) self.assertTrue(ht.equal(wh, res)) self.assertEqual(wh.gshape, (3, 3)) self.assertEqual(wh.dtype, ht.float64) self.assertEqual(wh.split, 0) a = ht.array([[0.0, 1.0, 2.0], [0.0, 2.0, 4.0], [0.0, 3.0, 6.0]], split=1) res = ht.array([[0.0, 1.0, 2.0], [0.0, 2.0, -1.0], [0.0, 3.0, -1.0]], split=1) wh = ht.where(a < 4.0, a, -1.0) self.assertTrue(ht.equal(wh, res)) self.assertEqual(wh.gshape, (3, 3)) self.assertEqual(wh.dtype, ht.float) self.assertEqual(wh.split, 1) with self.assertRaises(TypeError): ht.where(cond, a) with self.assertRaises(NotImplementedError): ht.where(cond, ht.ones((3, 3), split=0), ht.ones((3, 3), split=1))
def test_cumprod(self): a = ht.full((2, 4), 2, dtype=ht.int32) result = ht.array([[2, 4, 8, 16], [2, 4, 8, 16]], dtype=ht.int32) # split = None cumprod = ht.cumprod(a, 1) self.assertTrue(ht.equal(cumprod, result)) # Alias cumprod = ht.cumproduct(a, 1) self.assertTrue(ht.equal(cumprod, result)) a = ht.full((4, 2), 2, dtype=ht.int64, split=0) result = ht.array([[2, 2], [4, 4], [8, 8], [16, 16]], dtype=ht.int64, split=0) cumprod = ht.cumprod(a, 0) self.assertTrue(ht.equal(cumprod, result)) # 3D out = ht.empty((2, 2, 2), dtype=ht.float32, split=0) a = ht.full((2, 2, 2), 2, split=0) result = ht.array([[[2, 2], [2, 2]], [[4, 4], [4, 4]]], dtype=ht.float32, split=0) cumprod = ht.cumprod(a, 0, out=out) self.assertTrue(ht.equal(cumprod, out)) self.assertTrue(ht.equal(cumprod, result)) a = ht.full((2, 2, 2), 2, dtype=ht.int32, split=1) result = ht.array([[[2, 2], [4, 4]], [[2, 2], [4, 4]]], dtype=ht.float32, split=1) cumprod = ht.cumprod(a, 1, dtype=ht.float64) self.assertTrue(ht.equal(cumprod, result)) a = ht.full((2, 2, 2), 2, dtype=ht.float32, split=2) result = ht.array([[[2, 4], [2, 4]], [[2, 4], [2, 4]]], dtype=ht.float32, split=2) cumprod = ht.cumprod(a, 2) self.assertTrue(ht.equal(cumprod, result)) with self.assertRaises(NotImplementedError): ht.cumprod(ht.ones((2, 2)), axis=None) with self.assertRaises(TypeError): ht.cumprod(ht.ones((2, 2)), axis="1") with self.assertRaises(ValueError): ht.cumprod(a, 2, out=out) with self.assertRaises(ValueError): ht.cumprod(ht.ones((2, 2)), 2)
def test_ones(self): # scalar input simple_ones_float = ht.ones(3, device=ht_device) self.assertIsInstance(simple_ones_float, ht.DNDarray) self.assertEqual(simple_ones_float.shape, (3, )) self.assertEqual(simple_ones_float.lshape, (3, )) self.assertEqual(simple_ones_float.split, None) self.assertEqual(simple_ones_float.dtype, ht.float32) self.assertEqual( (simple_ones_float._DNDarray__array == 1).all().item(), 1) # different data type simple_ones_uint = ht.ones(5, dtype=ht.bool, device=ht_device) self.assertIsInstance(simple_ones_uint, ht.DNDarray) self.assertEqual(simple_ones_uint.shape, (5, )) self.assertEqual(simple_ones_uint.lshape, (5, )) self.assertEqual(simple_ones_uint.split, None) self.assertEqual(simple_ones_uint.dtype, ht.bool) self.assertEqual((simple_ones_uint._DNDarray__array == 1).all().item(), 1) # multi-dimensional elaborate_ones_int = ht.ones((2, 3), dtype=ht.int32, device=ht_device) self.assertIsInstance(elaborate_ones_int, ht.DNDarray) self.assertEqual(elaborate_ones_int.shape, (2, 3)) self.assertEqual(elaborate_ones_int.lshape, (2, 3)) self.assertEqual(elaborate_ones_int.split, None) self.assertEqual(elaborate_ones_int.dtype, ht.int32) self.assertEqual( (elaborate_ones_int._DNDarray__array == 1).all().item(), 1) # split axis elaborate_ones_split = ht.ones((6, 4), dtype=ht.int32, split=0, device=ht_device) self.assertIsInstance(elaborate_ones_split, ht.DNDarray) self.assertEqual(elaborate_ones_split.shape, (6, 4)) self.assertLessEqual(elaborate_ones_split.lshape[0], 6) self.assertEqual(elaborate_ones_split.lshape[1], 4) self.assertEqual(elaborate_ones_split.split, 0) self.assertEqual(elaborate_ones_split.dtype, ht.int32) self.assertEqual( (elaborate_ones_split._DNDarray__array == 1).all().item(), 1) # exceptions with self.assertRaises(TypeError): ht.ones("(2, 3,)", dtype=ht.float64, device=ht_device) with self.assertRaises(ValueError): ht.ones((-1, 3), dtype=ht.float64, device=ht_device) with self.assertRaises(TypeError): ht.ones((2, 3), dtype=ht.float64, split="axis", device=ht_device)
def test_int_cast(self): # simple scalar tensor a = ht.ones(1) casted_a = int(a) self.assertEqual(casted_a, 1) self.assertIsInstance(casted_a, int) # multi-dimensional scalar tensor b = ht.zeros((1, 1, 1, 1)) casted_b = int(b) self.assertEqual(casted_b, 0) self.assertIsInstance(casted_b, int) # split scalar tensor c = ht.full((1,), 5, split=0) casted_c = int(c) self.assertEqual(casted_c, 5) self.assertIsInstance(casted_c, int) # exception on non-scalar tensor with self.assertRaises(TypeError): int(ht.empty(1, 2, 1, 1)) # exception on empty tensor with self.assertRaises(TypeError): int(ht.empty((0, 1, 2))) # exception on split tensor, where each chunk has size 1 if ht.MPI_WORLD.size > 1: with self.assertRaises(TypeError): int(ht.full((ht.MPI_WORLD.size,), 2, split=0))
def test_imag(self): a = ht.array([1.0, 1.0j, 1 + 1j, -2 + 2j, 3 - 3j]) imag = ht.imag(a) res = ht.array([0.0, 1.0, 1.0, 2.0, -3.0], dtype=ht.float32, device=self.device) self.assertIs(imag.device, self.device) self.assertIs(imag.dtype, ht.float) self.assertEqual(imag.shape, (5,)) self.assertTrue(ht.equal(imag, res)) a = ht.array([1.0, 1.0j, 1 + 1j, -2 + 2j, 3 - 3j], split=0) imag = ht.imag(a) res = ht.array([0.0, 1.0, 1.0, 2.0, -3.0], dtype=ht.float32, device=self.device, split=0) self.assertIs(imag.device, self.device) self.assertIs(imag.dtype, ht.float) self.assertEqual(imag.shape, (5,)) self.assertTrue(ht.equal(imag, res)) # Not complex a = ht.ones((4, 4)) imag = a.imag res = ht.zeros((4, 4)) self.assertIs(imag.device, self.device) self.assertIs(imag.dtype, ht.float32) self.assertEqual(imag.shape, (4, 4)) self.assertTrue(ht.equal(imag, res))
def test_float_cast(self): # simple scalar tensor a = ht.ones(1, device=ht_device) casted_a = float(a) self.assertEqual(casted_a, 1.0) self.assertIsInstance(casted_a, float) # multi-dimensional scalar tensor b = ht.zeros((1, 1, 1, 1), device=ht_device) casted_b = float(b) self.assertEqual(casted_b, 0.0) self.assertIsInstance(casted_b, float) # split scalar tensor c = ht.full((1,), 5, split=0, device=ht_device) casted_c = float(c) self.assertEqual(casted_c, 5.0) self.assertIsInstance(casted_c, float) # exception on non-scalar tensor with self.assertRaises(TypeError): float(ht.empty(1, 2, 1, 1, device=ht_device)) # exception on empty tensor with self.assertRaises(TypeError): float(ht.empty((0, 1, 2), device=ht_device)) # exception on split tensor, where each chunk has size 1 if ht.MPI_WORLD.size > 1: with self.assertRaises(TypeError): float(ht.full((ht.MPI_WORLD.size,), 2, split=0), device=ht_device)
def test_assert_func_equal_for_tensor(self): array = np.ones((self.get_size(), 20), dtype=np.int8) ht_func = ht.any np_func = np.any self.assert_func_equal_for_tensor(array, ht_func, np_func, distributed_result=False) array = np.array([[1, 2, 4, 1, 3], [1, 4, 7, 5, 1]], dtype=np.int8) ht_func = ht.expand_dims np_func = np.expand_dims ht_args = {"axis": 1} np_args = {"axis": 1} self.assert_func_equal_for_tensor(array, ht_func, np_func, heat_args=ht_args, numpy_args=np_args) array = torch.randn(15, 15) ht_func = ht.exp np_func = np.exp self.assert_func_equal_for_tensor(array, heat_func=ht_func, numpy_func=np_func) array = ht.ones((15, 15)) with self.assertRaises(TypeError): self.assert_func_equal_for_tensor(array, heat_func=ht_func, numpy_func=np_func)
def test_bool_cast(self): # simple scalar tensor a = ht.ones(1, device=ht_device) casted_a = bool(a) self.assertEqual(casted_a, True) self.assertIsInstance(casted_a, bool) # multi-dimensional scalar tensor b = ht.zeros((1, 1, 1, 1), device=ht_device) casted_b = bool(b) self.assertEqual(casted_b, False) self.assertIsInstance(casted_b, bool) # split scalar tensor c = ht.full((1,), 5, split=0, device=ht_device) casted_c = bool(c) self.assertEqual(casted_c, True) self.assertIsInstance(casted_c, bool) # exception on non-scalar tensor with self.assertRaises(TypeError): bool(ht.empty(1, 2, 1, 1, device=ht_device)) # exception on empty tensor with self.assertRaises(TypeError): bool(ht.empty((0, 1, 2), device=ht_device)) # exception on split tensor, where each chunk has size 1 if ht.MPI_WORLD.size > 1: with self.assertRaises(TypeError): bool(ht.full((ht.MPI_WORLD.size,), 2, split=0, device=ht_device))
def test_assert_array_equal(self): heat_array = ht.ones((self.get_size(), 10, 10), dtype=ht.int32, split=1) np_array = np.ones((self.get_size(), 10, 10), dtype=np.int32) self.assert_array_equal(heat_array, np_array) np_array[0, 1, 1] = 0 with self.assertRaises(AssertionError): self.assert_array_equal(heat_array, np_array) heat_array = ht.zeros((25, 13, self.get_size(), 20), dtype=ht.float32, split=2) expected_array = torch.zeros( (25, 13, self.get_size(), 20), dtype=torch.float32, device=heat_array.device.torch_device, ) self.assert_array_equal(heat_array, expected_array) if self.get_rank() == 0: data = torch.arange(self.get_size(), dtype=torch.int32) else: data = torch.empty((0, ), dtype=torch.int32) ht_array = ht.array(data, is_split=0) np_array = np.arange(self.get_size(), dtype=np.int32) self.assert_array_equal(ht_array, np_array)
def test_cumsum(self): a = ht.ones((2, 4), dtype=ht.int32) result = ht.array([[1, 2, 3, 4], [1, 2, 3, 4]], dtype=ht.int32) # split = None cumsum = ht.cumsum(a, 1) self.assertTrue(ht.equal(cumsum, result)) a = ht.ones((4, 2), dtype=ht.int64, split=0) result = ht.array([[1, 1], [2, 2], [3, 3], [4, 4]], dtype=ht.int64, split=0) cumsum = ht.cumsum(a, 0) self.assertTrue(ht.equal(cumsum, result)) # 3D out = ht.empty((2, 2, 2), dtype=ht.float32, split=0) a = ht.ones((2, 2, 2), split=0) result = ht.array([[[1, 1], [1, 1]], [[2, 2], [2, 2]]], dtype=ht.float32, split=0) cumsum = ht.cumsum(a, 0, out=out) self.assertTrue(ht.equal(cumsum, out)) self.assertTrue(ht.equal(cumsum, result)) a = ht.ones((2, 2, 2), dtype=ht.int32, split=1) result = ht.array([[[1, 1], [2, 2]], [[1, 1], [2, 2]]], dtype=ht.float32, split=1) cumsum = ht.cumsum(a, 1, dtype=ht.float64) self.assertTrue(ht.equal(cumsum, result)) a = ht.ones((2, 2, 2), dtype=ht.float32, split=2) result = ht.array([[[1, 2], [1, 2]], [[1, 2], [1, 2]]], dtype=ht.float32, split=2) cumsum = ht.cumsum(a, 2) self.assertTrue(ht.equal(cumsum, result)) with self.assertRaises(NotImplementedError): ht.cumsum(ht.ones((2, 2)), axis=None) with self.assertRaises(TypeError): ht.cumsum(ht.ones((2, 2)), axis="1") with self.assertRaises(ValueError): ht.cumsum(a, 2, out=out) with self.assertRaises(ValueError): ht.cumsum(ht.ones((2, 2)), 2)
def test_fill_diagonal(self): ref = ht.zeros((ht.MPI_WORLD.size * 2, ht.MPI_WORLD.size * 2), dtype=ht.float32, split=0) a = ht.eye(ht.MPI_WORLD.size * 2, dtype=ht.float32, split=0) a.fill_diagonal(0) self.assertTrue(ht.equal(a, ref)) ref = ht.zeros((ht.MPI_WORLD.size * 2, ht.MPI_WORLD.size * 2), dtype=ht.int32, split=0) a = ht.eye(ht.MPI_WORLD.size * 2, dtype=ht.int32, split=0) a.fill_diagonal(0) self.assertTrue(ht.equal(a, ref)) ref = ht.zeros((ht.MPI_WORLD.size * 2, ht.MPI_WORLD.size * 2), dtype=ht.float32, split=1) a = ht.eye(ht.MPI_WORLD.size * 2, dtype=ht.float32, split=1) a.fill_diagonal(0) self.assertTrue(ht.equal(a, ref)) ref = ht.zeros((ht.MPI_WORLD.size * 2, ht.MPI_WORLD.size * 3), dtype=ht.float32, split=0) a = ht.eye((ht.MPI_WORLD.size * 2, ht.MPI_WORLD.size * 3), dtype=ht.float32, split=0) a.fill_diagonal(0) self.assertTrue(ht.equal(a, ref)) # ToDo: uneven tensor dimensions x and y when bug in factories.eye is fixed ref = ht.zeros((ht.MPI_WORLD.size * 3, ht.MPI_WORLD.size * 3), dtype=ht.float32, split=1) a = ht.eye((ht.MPI_WORLD.size * 3, ht.MPI_WORLD.size * 3), dtype=ht.float32, split=1) a.fill_diagonal(0) self.assertTrue(ht.equal(a, ref)) # ToDo: uneven tensor dimensions x and y when bug in factories.eye is fixed ref = ht.zeros((ht.MPI_WORLD.size * 4, ht.MPI_WORLD.size * 4), dtype=ht.float32, split=0) a = ht.eye((ht.MPI_WORLD.size * 4, ht.MPI_WORLD.size * 4), dtype=ht.float32, split=0) a.fill_diagonal(0) self.assertTrue(ht.equal(a, ref)) a = ht.ones((ht.MPI_WORLD.size * 2, ), dtype=ht.float32, split=0) with self.assertRaises(ValueError): a.fill_diagonal(0)
def test_empty_like(self): # scalar like_int = ht.empty_like(3, device=ht_device) self.assertIsInstance(like_int, ht.DNDarray) self.assertEqual(like_int.shape, (1,)) self.assertEqual(like_int.lshape, (1,)) self.assertEqual(like_int.split, None) self.assertEqual(like_int.dtype, ht.int32) # sequence like_str = ht.empty_like("abc", device=ht_device) self.assertIsInstance(like_str, ht.DNDarray) self.assertEqual(like_str.shape, (3,)) self.assertEqual(like_str.lshape, (3,)) self.assertEqual(like_str.split, None) self.assertEqual(like_str.dtype, ht.float32) # elaborate tensor ones = ht.ones((2, 3), dtype=ht.uint8, device=ht_device) like_ones = ht.empty_like(ones, device=ht_device) self.assertIsInstance(like_ones, ht.DNDarray) self.assertEqual(like_ones.shape, (2, 3)) self.assertEqual(like_ones.lshape, (2, 3)) self.assertEqual(like_ones.split, None) self.assertEqual(like_ones.dtype, ht.uint8) # elaborate tensor with split ones_split = ht.ones((2, 3), dtype=ht.uint8, split=0, device=ht_device) like_ones_split = ht.empty_like(ones_split, device=ht_device) self.assertIsInstance(like_ones_split, ht.DNDarray) self.assertEqual(like_ones_split.shape, (2, 3)) self.assertLessEqual(like_ones_split.lshape[0], 2) self.assertEqual(like_ones_split.lshape[1], 3) self.assertEqual(like_ones_split.split, 0) self.assertEqual(like_ones_split.dtype, ht.uint8) # exceptions with self.assertRaises(TypeError): ht.empty_like(ones, dtype="abc", device=ht_device) with self.assertRaises(TypeError): ht.empty_like(ones, split="axis", device=ht_device)
def test_copy(self): tensor = ht.ones(5) copied = tensor.copy() # test identity inequality and value equality self.assertIsNot(tensor, copied) self.assertIsNot(tensor.larray, copied.larray) self.assertTrue((tensor == copied)._DNDarray__array.all()) # test exceptions with self.assertRaises(TypeError): ht.copy("hello world")
def setUpClass(cls): cls.a_scalar = 2.0 cls.an_int_scalar = 2 cls.a_vector = ht.float32([2, 2], device=ht_device) cls.another_vector = ht.float32([2, 2, 2], device=ht_device) cls.a_tensor = ht.array([[1.0, 2.0], [3.0, 4.0]], device=ht_device) cls.another_tensor = ht.array([[2.0, 2.0], [2.0, 2.0]], device=ht_device) cls.a_split_tensor = cls.another_tensor.copy().resplit_(0) cls.split_ones_tensor = ht.ones((2, 2), split=1, device=ht_device) cls.errorneous_type = (2, 2)
def setUpClass(cls): super(TestRelational, cls).setUpClass() cls.a_scalar = 2.0 cls.an_int_scalar = 2 cls.a_vector = ht.float32([2, 2]) cls.another_vector = ht.float32([2, 2, 2]) cls.a_tensor = ht.array([[1.0, 2.0], [3.0, 4.0]]) cls.another_tensor = ht.array([[2.0, 2.0], [2.0, 2.0]]) cls.a_split_tensor = cls.another_tensor.copy().resplit_(0) cls.split_ones_tensor = ht.ones((2, 2), split=1) cls.errorneous_type = (2, 2)
def test_angle(self): a = ht.array([1.0, 1.0j, 1 + 1j, -2 + 2j, 3 - 3j]) angle = ht.angle(a) res = torch.angle(a.larray) self.assertIs(angle.device, self.device) self.assertIs(angle.dtype, ht.float) self.assertEqual(angle.shape, (5, )) self.assertTrue(torch.equal(angle.larray, res)) a = ht.array([1.0, 1.0j, 1 + 1j, -2 + 2j, 3 - 3j], split=0) angle = ht.angle(a) res = torch.angle(a.larray) self.assertIs(angle.device, self.device) self.assertIs(angle.dtype, ht.float) self.assertEqual(angle.shape, (5, )) self.assertTrue(torch.equal(angle.larray, res)) a = ht.array([[1.0, 1.0j], [1 + 1j, -2 + 2j], [3 - 3j, -4 - 4j]], split=1) angle = ht.angle(a, deg=True) res = ht.array( [[0.0, 90.0], [45.0, 135.0], [-45.0, -135.0]], dtype=ht.float32, device=self.device, split=1, ) self.assertIs(angle.device, self.device) self.assertIs(angle.dtype, ht.float32) self.assertEqual(angle.shape, (3, 2)) self.assertTrue(ht.equal(angle, res)) # Not complex a = ht.ones((4, 4), split=1) angle = ht.angle(a) res = ht.zeros((4, 4), split=1) self.assertIs(angle.device, self.device) self.assertIs(angle.dtype, ht.float32) self.assertEqual(angle.shape, (4, 4)) self.assertTrue(ht.equal(angle, res))
def test_cg(self): size = ht.communication.MPI_WORLD.size * 3 b = ht.arange(1, size + 1, dtype=ht.float32, split=0) A = ht.manipulations.diag(b) x0 = ht.random.rand(size, dtype=b.dtype, split=b.split) x = ht.ones(b.shape, dtype=b.dtype, split=b.split) res = ht.linalg.cg(A, b, x0) self.assertTrue(ht.allclose(x, res, atol=1e-3)) b_np = np.arange(1, size + 1) with self.assertRaises(TypeError): ht.linalg.cg(A, b_np, x0) with self.assertRaises(RuntimeError): ht.linalg.cg(A, A, x0) with self.assertRaises(RuntimeError): ht.linalg.cg(b, b, x0) with self.assertRaises(RuntimeError): ht.linalg.cg(A, b, A)
def test_len(self): # vector a = ht.zeros((10,), device=ht_device) a_length = len(a) self.assertIsInstance(a_length, int) self.assertEqual(a_length, 10) # matrix b = ht.ones((50, 2), device=ht_device) b_length = len(b) self.assertIsInstance(b_length, int) self.assertEqual(b_length, 50) # split 5D array c = ht.empty((3, 4, 5, 6, 7), split=-1, device=ht_device) c_length = len(c) self.assertIsInstance(c_length, int) self.assertEqual(c_length, 3)
def _spectral_embedding(self, X): """ Helper function to embed the dataset X into the eigenvectors of the graph Laplacian matrix Returns ------- ht.DNDarray, shape=(m_lanczos): Eigenvalues of the graph's Laplacian matrix. ht.DNDarray, shape=(n, m_lanczos): Eigenvectors of the graph's Laplacian matrix. """ L = self._laplacian.construct(X) # 3. Eigenvalue and -vector calculation via Lanczos Algorithm v0 = ht.ones((L.shape[0], ), dtype=L.dtype, split=0, device=L.device) / math.sqrt(L.shape[0]) V, T = ht.lanczos(L, self.n_lanczos, v0) # 4. Calculate and Sort Eigenvalues and Eigenvectors of tridiagonal matrix T eval, evec = torch.eig(T._DNDarray__array, eigenvectors=True) # If x is an Eigenvector of T, then y = V@x is the corresponding Eigenvector of L eval, idx = torch.sort(eval[:, 0], dim=0) eigenvalues = ht.array(eval) eigenvectors = ht.matmul(V, ht.array(evec))[:, idx] return eigenvalues, eigenvectors
def test_all(self): array_len = 9 # check all over all float elements of 1d tensor locally ones_noaxis = ht.ones(array_len, device=ht_device) x = (ones_noaxis == 1).all() self.assertIsInstance(x, ht.DNDarray) self.assertEqual(x.shape, (1, )) self.assertEqual(x.lshape, (1, )) self.assertEqual(x.dtype, ht.bool) self.assertEqual(x._DNDarray__array.dtype, torch.bool) self.assertEqual(x.split, None) self.assertEqual(x._DNDarray__array, 1) out_noaxis = ht.zeros((1, ), device=ht_device) ht.all(ones_noaxis, out=out_noaxis) self.assertEqual(out_noaxis._DNDarray__array, 1) # check all over all float elements of split 1d tensor ones_noaxis_split = ht.ones(array_len, split=0, device=ht_device) floats_is_one = ones_noaxis_split.all() self.assertIsInstance(floats_is_one, ht.DNDarray) self.assertEqual(floats_is_one.shape, (1, )) self.assertEqual(floats_is_one.lshape, (1, )) self.assertEqual(floats_is_one.dtype, ht.bool) self.assertEqual(floats_is_one._DNDarray__array.dtype, torch.bool) self.assertEqual(floats_is_one.split, None) self.assertEqual(floats_is_one._DNDarray__array, 1) out_noaxis = ht.zeros((1, ), device=ht_device) ht.all(ones_noaxis_split, out=out_noaxis) self.assertEqual(out_noaxis._DNDarray__array, 1) # check all over all integer elements of 1d tensor locally ones_noaxis_int = ht.ones(array_len, device=ht_device).astype(ht.int) int_is_one = ones_noaxis_int.all() self.assertIsInstance(int_is_one, ht.DNDarray) self.assertEqual(int_is_one.shape, (1, )) self.assertEqual(int_is_one.lshape, (1, )) self.assertEqual(int_is_one.dtype, ht.bool) self.assertEqual(int_is_one._DNDarray__array.dtype, torch.bool) self.assertEqual(int_is_one.split, None) self.assertEqual(int_is_one._DNDarray__array, 1) out_noaxis = ht.zeros((1, ), device=ht_device) ht.all(ones_noaxis_int, out=out_noaxis) self.assertEqual(out_noaxis._DNDarray__array, 1) # check all over all integer elements of split 1d tensor ones_noaxis_split_int = ht.ones(array_len, split=0, device=ht_device).astype(ht.int) split_int_is_one = ones_noaxis_split_int.all() self.assertIsInstance(split_int_is_one, ht.DNDarray) self.assertEqual(split_int_is_one.shape, (1, )) self.assertEqual(split_int_is_one.lshape, (1, )) self.assertEqual(split_int_is_one.dtype, ht.bool) self.assertEqual(split_int_is_one._DNDarray__array.dtype, torch.bool) self.assertEqual(split_int_is_one.split, None) self.assertEqual(split_int_is_one._DNDarray__array, 1) out_noaxis = ht.zeros((1, ), device=ht_device) ht.all(ones_noaxis_split_int, out=out_noaxis) self.assertEqual(out_noaxis._DNDarray__array, 1) # check all over all float elements of 3d tensor locally ones_noaxis_volume = ht.ones((3, 3, 3), device=ht_device) volume_is_one = ones_noaxis_volume.all() self.assertIsInstance(volume_is_one, ht.DNDarray) self.assertEqual(volume_is_one.shape, (1, )) self.assertEqual(volume_is_one.lshape, (1, )) self.assertEqual(volume_is_one.dtype, ht.bool) self.assertEqual(volume_is_one._DNDarray__array.dtype, torch.bool) self.assertEqual(volume_is_one.split, None) self.assertEqual(volume_is_one._DNDarray__array, 1) out_noaxis = ht.zeros((1, ), device=ht_device) ht.all(ones_noaxis_volume, out=out_noaxis) self.assertEqual(out_noaxis._DNDarray__array, 1) # check sequence is not all one sequence = ht.arange(array_len, device=ht_device) sequence_is_one = sequence.all() self.assertIsInstance(sequence_is_one, ht.DNDarray) self.assertEqual(sequence_is_one.shape, (1, )) self.assertEqual(sequence_is_one.lshape, (1, )) self.assertEqual(sequence_is_one.dtype, ht.bool) self.assertEqual(sequence_is_one._DNDarray__array.dtype, torch.bool) self.assertEqual(sequence_is_one.split, None) self.assertEqual(sequence_is_one._DNDarray__array, 0) out_noaxis = ht.zeros((1, ), device=ht_device) ht.all(sequence, out=out_noaxis) self.assertEqual(out_noaxis._DNDarray__array, 0) # check all over all float elements of split 3d tensor ones_noaxis_split_axis = ht.ones((3, 3, 3), split=0, device=ht_device) float_volume_is_one = ones_noaxis_split_axis.all(axis=0) self.assertIsInstance(float_volume_is_one, ht.DNDarray) self.assertEqual(float_volume_is_one.shape, (3, 3)) self.assertEqual(float_volume_is_one.all(axis=1).dtype, ht.bool) self.assertEqual(float_volume_is_one._DNDarray__array.dtype, torch.bool) self.assertEqual(float_volume_is_one.split, None) out_noaxis = ht.zeros((3, 3), device=ht_device) ht.all(ones_noaxis_split_axis, axis=0, out=out_noaxis) # check all over all float elements of split 3d tensor with tuple axis ones_noaxis_split_axis = ht.ones((3, 3, 3), split=0, device=ht_device) float_volume_is_one = ones_noaxis_split_axis.all(axis=(0, 1)) self.assertIsInstance(float_volume_is_one, ht.DNDarray) self.assertEqual(float_volume_is_one.shape, (3, )) self.assertEqual(float_volume_is_one.all(axis=0).dtype, ht.bool) self.assertEqual(float_volume_is_one._DNDarray__array.dtype, torch.bool) self.assertEqual(float_volume_is_one.split, None) # check all over all float elements of split 5d tensor with negative axis ones_noaxis_split_axis_neg = ht.zeros((1, 2, 3, 4, 5), split=1, device=ht_device) float_5d_is_one = ones_noaxis_split_axis_neg.all(axis=-2) self.assertIsInstance(float_5d_is_one, ht.DNDarray) self.assertEqual(float_5d_is_one.shape, (1, 2, 3, 5)) self.assertEqual(float_5d_is_one.dtype, ht.bool) self.assertEqual(float_5d_is_one._DNDarray__array.dtype, torch.bool) self.assertEqual(float_5d_is_one.split, 1) out_noaxis = ht.zeros((1, 2, 3, 5), device=ht_device) ht.all(ones_noaxis_split_axis_neg, axis=-2, out=out_noaxis) # exceptions with self.assertRaises(ValueError): ht.ones(array_len, device=ht_device).all(axis=1) with self.assertRaises(ValueError): ht.ones(array_len, device=ht_device).all(axis=-2) with self.assertRaises(ValueError): ht.ones((4, 4), device=ht_device).all(axis=0, out=out_noaxis) with self.assertRaises(TypeError): ht.ones(array_len, device=ht_device).all(axis="bad_axis_type")
def test_sum(self): array_len = 11 # check sum over all float elements of 1d tensor locally shape_noaxis = ht.ones(array_len) no_axis_sum = shape_noaxis.sum() self.assertIsInstance(no_axis_sum, ht.DNDarray) self.assertEqual(no_axis_sum.shape, (1, )) self.assertEqual(no_axis_sum.lshape, (1, )) self.assertEqual(no_axis_sum.dtype, ht.float32) self.assertEqual(no_axis_sum.larray.dtype, torch.float32) self.assertEqual(no_axis_sum.split, None) self.assertEqual(no_axis_sum.larray, array_len) out_noaxis = ht.zeros((1, )) ht.sum(shape_noaxis, out=out_noaxis) self.assertTrue(out_noaxis.larray == shape_noaxis.larray.sum()) # check sum over all float elements of split 1d tensor shape_noaxis_split = ht.arange(array_len, split=0) shape_noaxis_split_sum = shape_noaxis_split.sum() self.assertIsInstance(shape_noaxis_split_sum, ht.DNDarray) self.assertEqual(shape_noaxis_split_sum.shape, (1, )) self.assertEqual(shape_noaxis_split_sum.lshape, (1, )) self.assertEqual(shape_noaxis_split_sum.dtype, ht.int64) self.assertEqual(shape_noaxis_split_sum.larray.dtype, torch.int64) self.assertEqual(shape_noaxis_split_sum.split, None) self.assertEqual(shape_noaxis_split_sum, 55) out_noaxis = ht.zeros((1, )) ht.sum(shape_noaxis_split, out=out_noaxis) self.assertEqual(out_noaxis.larray, 55) # check sum over all float elements of 3d tensor locally shape_noaxis = ht.ones((3, 3, 3)) no_axis_sum = shape_noaxis.sum() self.assertIsInstance(no_axis_sum, ht.DNDarray) self.assertEqual(no_axis_sum.shape, (1, )) self.assertEqual(no_axis_sum.lshape, (1, )) self.assertEqual(no_axis_sum.dtype, ht.float32) self.assertEqual(no_axis_sum.larray.dtype, torch.float32) self.assertEqual(no_axis_sum.split, None) self.assertEqual(no_axis_sum.larray, 27) out_noaxis = ht.zeros((1, )) ht.sum(shape_noaxis, out=out_noaxis) self.assertEqual(out_noaxis.larray, 27) # check sum over all float elements of split 3d tensor shape_noaxis_split_axis = ht.ones((3, 3, 3), split=0) split_axis_sum = shape_noaxis_split_axis.sum(axis=0) self.assertIsInstance(split_axis_sum, ht.DNDarray) self.assertEqual(split_axis_sum.shape, (3, 3)) self.assertEqual(split_axis_sum.dtype, ht.float32) self.assertEqual(split_axis_sum.larray.dtype, torch.float32) self.assertEqual(split_axis_sum.split, None) # check split semantics shape_noaxis_split_axis = ht.ones((3, 3, 3), split=2) split_axis_sum = shape_noaxis_split_axis.sum(axis=1) self.assertIsInstance(split_axis_sum, ht.DNDarray) self.assertEqual(split_axis_sum.shape, (3, 3)) self.assertEqual(split_axis_sum.dtype, ht.float32) self.assertEqual(split_axis_sum.larray.dtype, torch.float32) self.assertEqual(split_axis_sum.split, 1) out_noaxis = ht.zeros((3, 3)) ht.sum(shape_noaxis, axis=0, out=out_noaxis) self.assertTrue((out_noaxis.larray == torch.full( (3, 3), 3, dtype=torch.float, device=self.device.torch_device)).all()) # check sum over all float elements of splitted 5d tensor with negative axis shape_noaxis_split_axis_neg = ht.ones((1, 2, 3, 4, 5), split=1) shape_noaxis_split_axis_neg_sum = shape_noaxis_split_axis_neg.sum( axis=-2) self.assertIsInstance(shape_noaxis_split_axis_neg_sum, ht.DNDarray) self.assertEqual(shape_noaxis_split_axis_neg_sum.shape, (1, 2, 3, 5)) self.assertEqual(shape_noaxis_split_axis_neg_sum.dtype, ht.float32) self.assertEqual(shape_noaxis_split_axis_neg_sum.larray.dtype, torch.float32) self.assertEqual(shape_noaxis_split_axis_neg_sum.split, 1) out_noaxis = ht.zeros((1, 2, 3, 5), split=1) ht.sum(shape_noaxis_split_axis_neg, axis=-2, out=out_noaxis) # check sum over all float elements of splitted 3d tensor with tuple axis shape_split_axis_tuple = ht.ones((3, 4, 5), split=1) shape_split_axis_tuple_sum = shape_split_axis_tuple.sum(axis=(-2, -3)) expected_result = ht.ones((5, )) * 12.0 self.assertIsInstance(shape_split_axis_tuple_sum, ht.DNDarray) self.assertEqual(shape_split_axis_tuple_sum.shape, (5, )) self.assertEqual(shape_split_axis_tuple_sum.dtype, ht.float32) self.assertEqual(shape_split_axis_tuple_sum.larray.dtype, torch.float32) self.assertEqual(shape_split_axis_tuple_sum.split, None) self.assertTrue((shape_split_axis_tuple_sum == expected_result).all()) # exceptions with self.assertRaises(ValueError): ht.ones(array_len).sum(axis=1) with self.assertRaises(ValueError): ht.ones(array_len).sum(axis=-2) with self.assertRaises(ValueError): ht.ones((4, 4)).sum(axis=0, out=out_noaxis) with self.assertRaises(TypeError): ht.ones(array_len).sum(axis="bad_axis_type")
def test_prod(self): array_len = 11 # check sum over all float elements of 1d tensor locally shape_noaxis = ht.ones(array_len) no_axis_prod = shape_noaxis.prod() self.assertIsInstance(no_axis_prod, ht.DNDarray) self.assertEqual(no_axis_prod.shape, (1, )) self.assertEqual(no_axis_prod.lshape, (1, )) self.assertEqual(no_axis_prod.dtype, ht.float32) self.assertEqual(no_axis_prod.larray.dtype, torch.float32) self.assertEqual(no_axis_prod.split, None) self.assertEqual(no_axis_prod.larray, 1) out_noaxis = ht.zeros((1, )) ht.prod(shape_noaxis, out=out_noaxis) self.assertEqual(out_noaxis.larray, 1) # check sum over all float elements of split 1d tensor shape_noaxis_split = ht.arange(1, array_len, split=0) shape_noaxis_split_prod = shape_noaxis_split.prod() self.assertIsInstance(shape_noaxis_split_prod, ht.DNDarray) self.assertEqual(shape_noaxis_split_prod.shape, (1, )) self.assertEqual(shape_noaxis_split_prod.lshape, (1, )) self.assertEqual(shape_noaxis_split_prod.dtype, ht.int64) self.assertEqual(shape_noaxis_split_prod.larray.dtype, torch.int64) self.assertEqual(shape_noaxis_split_prod.split, None) self.assertEqual(shape_noaxis_split_prod, 3628800) out_noaxis = ht.zeros((1, )) ht.prod(shape_noaxis_split, out=out_noaxis) self.assertEqual(out_noaxis.larray, 3628800) # check sum over all float elements of 3d tensor locally shape_noaxis = ht.full((3, 3, 3), 2) no_axis_prod = shape_noaxis.prod() self.assertIsInstance(no_axis_prod, ht.DNDarray) self.assertEqual(no_axis_prod.shape, (1, )) self.assertEqual(no_axis_prod.lshape, (1, )) self.assertEqual(no_axis_prod.dtype, ht.float32) self.assertEqual(no_axis_prod.larray.dtype, torch.float32) self.assertEqual(no_axis_prod.split, None) self.assertEqual(no_axis_prod.larray, 134217728) out_noaxis = ht.zeros((1, )) ht.prod(shape_noaxis, out=out_noaxis) self.assertEqual(out_noaxis.larray, 134217728) # check sum over all float elements of split 3d tensor shape_noaxis_split_axis = ht.full((3, 3, 3), 2, split=0) split_axis_prod = shape_noaxis_split_axis.prod(axis=0) self.assertIsInstance(split_axis_prod, ht.DNDarray) self.assertEqual(split_axis_prod.shape, (3, 3)) self.assertEqual(split_axis_prod.dtype, ht.float32) self.assertEqual(split_axis_prod.larray.dtype, torch.float32) self.assertEqual(split_axis_prod.split, None) out_axis = ht.ones((3, 3)) ht.prod(shape_noaxis, axis=0, out=out_axis) self.assertTrue((out_axis.larray == torch.full( (3, ), 8, dtype=torch.float, device=self.device.torch_device)).all()) # check sum over all float elements of splitted 5d tensor with negative axis shape_noaxis_split_axis_neg = ht.full((1, 2, 3, 4, 5), 2, split=1) shape_noaxis_split_axis_neg_prod = shape_noaxis_split_axis_neg.prod( axis=-2) self.assertIsInstance(shape_noaxis_split_axis_neg_prod, ht.DNDarray) self.assertEqual(shape_noaxis_split_axis_neg_prod.shape, (1, 2, 3, 5)) self.assertEqual(shape_noaxis_split_axis_neg_prod.dtype, ht.float32) self.assertEqual(shape_noaxis_split_axis_neg_prod.larray.dtype, torch.float32) self.assertEqual(shape_noaxis_split_axis_neg_prod.split, 1) out_noaxis = ht.zeros((1, 2, 3, 5), split=1) ht.prod(shape_noaxis_split_axis_neg, axis=-2, out=out_noaxis) # check sum over all float elements of splitted 3d tensor with tuple axis shape_split_axis_tuple = ht.ones((3, 4, 5), split=1) shape_split_axis_tuple_prod = shape_split_axis_tuple.prod(axis=(-2, -3)) expected_result = ht.ones((5, )) self.assertIsInstance(shape_split_axis_tuple_prod, ht.DNDarray) self.assertEqual(shape_split_axis_tuple_prod.shape, (5, )) self.assertEqual(shape_split_axis_tuple_prod.dtype, ht.float32) self.assertEqual(shape_split_axis_tuple_prod.larray.dtype, torch.float32) self.assertEqual(shape_split_axis_tuple_prod.split, None) self.assertTrue((shape_split_axis_tuple_prod == expected_result).all()) # exceptions with self.assertRaises(ValueError): ht.ones(array_len).prod(axis=1) with self.assertRaises(ValueError): ht.ones(array_len).prod(axis=-2) with self.assertRaises(ValueError): ht.ones((4, 4)).prod(axis=0, out=out_noaxis) with self.assertRaises(TypeError): ht.ones(array_len).prod(axis="bad_axis_type")