Beispiel #1
0
    def test_norm(self):
        a = ht.arange(9, dtype=ht.float32, split=0) - 4
        self.assertTrue(
            ht.allclose(ht.linalg.norm(a),
                        ht.float32(np.linalg.norm(a.numpy())).item(),
                        atol=1e-5))
        a.resplit_(axis=None)
        self.assertTrue(
            ht.allclose(ht.linalg.norm(a),
                        ht.float32(np.linalg.norm(a.numpy())).item(),
                        atol=1e-5))

        b = ht.array([[-4.0, -3.0, -2.0], [-1.0, 0.0, 1.0], [2.0, 3.0, 4.0]],
                     split=0)
        self.assertTrue(
            ht.allclose(ht.linalg.norm(b),
                        ht.float32(np.linalg.norm(b.numpy())).item(),
                        atol=1e-5))
        b.resplit_(axis=1)
        self.assertTrue(
            ht.allclose(ht.linalg.norm(b),
                        ht.float32(np.linalg.norm(b.numpy())).item(),
                        atol=1e-5))

        with self.assertRaises(TypeError):
            c = np.arange(9) - 4
            ht.linalg.norm(c)
Beispiel #2
0
    def test_full_like(self):
        # scalar
        like_int = ht.full_like(3, 4)
        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.float32)
        self.assertTrue(ht.allclose(like_int, ht.float32(4)))

        # sequence
        like_str = ht.full_like('abc', 2)
        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.assertTrue(ht.allclose(like_str, ht.float32(2)))

        # elaborate tensor
        zeros = ht.zeros((
            2,
            3,
        ), dtype=ht.uint8)
        like_zeros = ht.full_like(zeros, 7)
        self.assertIsInstance(like_zeros, ht.tensor)
        self.assertEqual(like_zeros.shape, (
            2,
            3,
        ))
        self.assertEqual(like_zeros.lshape, (
            2,
            3,
        ))
        self.assertEqual(like_zeros.split, None)
        self.assertEqual(like_zeros.dtype, ht.float32)
        self.assertTrue(ht.allclose(like_zeros, ht.float32(7)))

        # elaborate tensor with split
        zeros_split = ht.zeros((
            2,
            3,
        ), dtype=ht.uint8, split=0)
        like_zeros_split = ht.full_like(zeros_split, 6)
        self.assertIsInstance(like_zeros_split, ht.tensor)
        self.assertEqual(like_zeros_split.shape, (
            2,
            3,
        ))
        self.assertLessEqual(like_zeros_split.lshape[0], 2)
        self.assertEqual(like_zeros_split.lshape[1], 3)
        self.assertEqual(like_zeros_split.split, 0)
        self.assertEqual(like_zeros_split.dtype, ht.float32)
        self.assertTrue(ht.allclose(like_zeros_split, ht.float32(6)))

        # exceptions
        with self.assertRaises(TypeError):
            ht.ones_like(zeros, dtype='abc')
        with self.assertRaises(TypeError):
            ht.ones_like(zeros, split='axis')
Beispiel #3
0
    def test_logaddexp2(self):
        elements = 15
        tmp = torch.arange(1,
                           elements,
                           dtype=torch.float64,
                           device=self.device.torch_device)
        tmp = tmp.logaddexp2(tmp)
        comparison = ht.array(tmp)

        # logaddexp2 of float32
        float32_tensor = ht.arange(1, elements, dtype=ht.float32)
        float32_logaddexp2 = ht.logaddexp2(float32_tensor, float32_tensor)
        self.assertIsInstance(float32_logaddexp2, ht.DNDarray)
        self.assertEqual(float32_logaddexp2.dtype, ht.float32)
        self.assertTrue(
            ht.allclose(float32_logaddexp2, comparison.astype(ht.float32)))

        # logaddexp2 of float64
        float64_tensor = ht.arange(1, elements, dtype=ht.float64)
        float64_logaddexp2 = ht.logaddexp2(float64_tensor, float64_tensor)
        self.assertIsInstance(float64_logaddexp2, ht.DNDarray)
        self.assertEqual(float64_logaddexp2.dtype, ht.float64)
        self.assertTrue(ht.allclose(float64_logaddexp2, comparison))

        # check exceptions
        with self.assertRaises(TypeError):
            ht.logaddexp2([1, 2, 3], [1, 2, 3])
        with self.assertRaises(TypeError):
            ht.logaddexp2("hello world", "hello world")
Beispiel #4
0
    def test_allclose(self):
        a = ht.float32([[2, 2], [2, 2]])
        b = ht.float32([[2.00005, 2.00005], [2.00005, 2.00005]])

        self.assertFalse(ht.allclose(a, b))
        self.assertTrue(ht.allclose(a, b, atol=1e-04))
        self.assertTrue(ht.allclose(a, b, rtol=1e-04))

        with self.assertRaises(TypeError):
            ht.allclose(a, (2, 2, 2, 2))
Beispiel #5
0
    def test_exp(self):
        elements = 10
        tmp = torch.arange(elements,
                           dtype=torch.float64,
                           device=self.device.torch_device).exp()
        comparison = ht.array(tmp)

        # exponential of float32
        float32_tensor = ht.arange(elements, dtype=ht.float32)
        float32_exp = ht.exp(float32_tensor)
        self.assertIsInstance(float32_exp, ht.DNDarray)
        self.assertEqual(float32_exp.dtype, ht.float32)
        self.assertTrue(ht.allclose(float32_exp,
                                    comparison.astype(ht.float32)))

        # exponential of float64
        float64_tensor = ht.arange(elements, dtype=ht.float64)
        float64_exp = ht.exp(float64_tensor)
        self.assertIsInstance(float64_exp, ht.DNDarray)
        self.assertEqual(float64_exp.dtype, ht.float64)
        self.assertTrue(ht.allclose(float64_exp, comparison))

        # exponential of ints, automatic conversion to intermediate floats
        int32_tensor = ht.arange(elements, dtype=ht.int32)
        int32_exp = ht.exp(int32_tensor)
        self.assertIsInstance(int32_exp, ht.DNDarray)
        self.assertEqual(int32_exp.dtype, ht.float32)
        self.assertTrue(ht.allclose(int32_exp, ht.float32(comparison)))

        # exponential of longs, automatic conversion to intermediate floats
        int64_tensor = ht.arange(elements, dtype=ht.int64)
        int64_exp = int64_tensor.exp()
        self.assertIsInstance(int64_exp, ht.DNDarray)
        self.assertEqual(int64_exp.dtype, ht.float64)
        self.assertTrue(ht.allclose(int64_exp, comparison))

        # check exceptions
        with self.assertRaises(TypeError):
            ht.exp([1, 2, 3])
        with self.assertRaises(TypeError):
            ht.exp("hello world")

        # Tests with split
        expected = torch.arange(10,
                                dtype=torch.float32,
                                device=self.device.torch_device).exp()
        actual = ht.arange(10, split=0, dtype=ht.float32).exp()
        self.assertEqual(actual.gshape, tuple(expected.shape))
        self.assertEqual(actual.split, 0)
        actual = actual.resplit_(None)
        self.assertEqual(actual.lshape, expected.shape)
        self.assertTrue(torch.equal(expected, actual.larray))
        self.assertEqual(actual.dtype, ht.float32)
Beispiel #6
0
 def test_qr_sp1_ext(self):
     st_whole = torch.randn(70, 70, device=self.device.torch_device)
     sp = 1
     for m in range(50, st_whole.shape[0] + 1, 1):
         for n in range(50, st_whole.shape[1] + 1, 1):
             for t in range(1, 3):
                 st = st_whole[:m, :n].clone()
                 a_comp = ht.array(st, split=0)
                 a = ht.array(st, split=sp)
                 qr = a.qr(tiles_per_proc=t)
                 self.assertTrue(ht.allclose(a_comp, qr.Q @ qr.R, rtol=1e-5, atol=1e-5))
                 self.assertTrue(ht.allclose(qr.Q.T @ qr.Q, ht.eye(m), rtol=1e-5, atol=1e-5))
                 self.assertTrue(ht.allclose(ht.eye(m), qr.Q @ qr.Q.T, rtol=1e-5, atol=1e-5))
Beispiel #7
0
    def test_arctan2(self):
        float32_y = torch.randn(30, device=self.device.torch_device)
        float32_x = torch.randn(30, device=self.device.torch_device)

        float32_comparison = torch.atan2(float32_y, float32_x)
        float32_arctan2 = ht.arctan2(ht.array(float32_y), ht.array(float32_x))

        self.assertIsInstance(float32_arctan2, ht.DNDarray)
        self.assertEqual(float32_arctan2.dtype, ht.float32)
        self.assertTrue(
            torch.allclose(float32_arctan2._DNDarray__array,
                           float32_comparison))

        float64_y = torch.randn(30,
                                dtype=torch.float64,
                                device=self.device.torch_device)
        float64_x = torch.randn(30,
                                dtype=torch.float64,
                                device=self.device.torch_device)

        float64_comparison = torch.atan2(float64_y, float64_x)
        float64_arctan2 = ht.atan2(ht.array(float64_y), ht.array(float64_x))

        self.assertIsInstance(float64_arctan2, ht.DNDarray)
        self.assertEqual(float64_arctan2.dtype, ht.float64)
        self.assertTrue(
            torch.allclose(float64_arctan2._DNDarray__array,
                           float64_comparison))

        # Rare Special Case with integers
        int32_x = ht.array([-1, +1, +1, -1])
        int32_y = ht.array([-1, -1, +1, +1])

        int32_comparison = ht.array([-135.0, -45.0, 45.0, 135.0],
                                    dtype=ht.float64)
        int32_arctan2 = ht.arctan2(int32_y, int32_x) * 180 / ht.pi

        self.assertIsInstance(int32_arctan2, ht.DNDarray)
        self.assertEqual(int32_arctan2.dtype, ht.float64)
        self.assertTrue(ht.allclose(int32_arctan2, int32_comparison))

        int16_x = ht.array([-1, +1, +1, -1], dtype=ht.int16)
        int16_y = ht.array([-1, -1, +1, +1], dtype=ht.int16)

        int16_comparison = ht.array([-135.0, -45.0, 45.0, 135.0],
                                    dtype=ht.float32)
        int16_arctan2 = ht.arctan2(int16_y, int16_x) * 180.0 / ht.pi

        self.assertIsInstance(int16_arctan2, ht.DNDarray)
        self.assertEqual(int16_arctan2.dtype, ht.float32)
        self.assertTrue(ht.allclose(int16_arctan2, int16_comparison))
Beispiel #8
0
    def test_full_like(self):
        # scalar
        like_int = ht.full_like(3, 4)
        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.float32)
        self.assertTrue(ht.allclose(like_int, ht.float32(4, device=ht_device)))

        # sequence
        like_str = ht.full_like("abc", 2)
        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)
        self.assertTrue(ht.allclose(like_str, ht.float32(2, device=ht_device)))

        # elaborate tensor
        zeros = ht.zeros((2, 3), dtype=ht.uint8, device=ht_device)
        like_zeros = ht.full_like(zeros, 7)
        self.assertIsInstance(like_zeros, ht.DNDarray)
        self.assertEqual(like_zeros.shape, (2, 3))
        self.assertEqual(like_zeros.lshape, (2, 3))
        self.assertEqual(like_zeros.split, None)
        self.assertEqual(like_zeros.dtype, ht.float32)
        self.assertTrue(
            ht.allclose(like_zeros, ht.float32(7, device=ht_device)))

        # elaborate tensor with split
        zeros_split = ht.zeros((2, 3),
                               dtype=ht.uint8,
                               split=0,
                               device=ht_device)
        like_zeros_split = ht.full_like(zeros_split, 6)
        self.assertIsInstance(like_zeros_split, ht.DNDarray)
        self.assertEqual(like_zeros_split.shape, (2, 3))
        self.assertLessEqual(like_zeros_split.lshape[0], 2)
        self.assertEqual(like_zeros_split.lshape[1], 3)
        self.assertEqual(like_zeros_split.split, 0)
        self.assertEqual(like_zeros_split.dtype, ht.float32)
        self.assertTrue(
            ht.allclose(like_zeros_split, ht.float32(6, device=ht_device)))

        # exceptions
        with self.assertRaises(TypeError):
            ht.ones_like(zeros, dtype="abc", device=ht_device)
        with self.assertRaises(TypeError):
            ht.ones_like(zeros, split="axis", device=ht_device)
Beispiel #9
0
    def test_fmod(self):
        result = ht.array([[1.0, 0.0], [1.0, 0.0]])
        an_int_tensor = ht.array([[5, 3], [4, 1]])
        integer_result = ht.array([[1, 1], [0, 1]])
        commutated_result = ht.array([[0.0, 0.0], [2.0, 2.0]])
        zero_tensor = ht.zeros((2, 2))

        a_float = ht.array([5.3])
        another_float = ht.array([1.9])
        result_float = ht.array([1.5])

        self.assertTrue(ht.equal(ht.fmod(self.a_scalar, self.a_scalar), ht.float32([0.0])))
        self.assertTrue(ht.equal(ht.fmod(self.a_tensor, self.a_tensor), zero_tensor))
        self.assertTrue(ht.equal(ht.fmod(self.a_tensor, self.an_int_scalar), result))
        self.assertTrue(ht.equal(ht.fmod(self.a_tensor, self.another_tensor), result))
        self.assertTrue(ht.equal(ht.fmod(self.a_tensor, self.a_vector), result))
        self.assertTrue(ht.equal(ht.fmod(self.a_tensor, self.an_int_scalar), result))
        self.assertTrue(ht.equal(ht.fmod(an_int_tensor, self.an_int_scalar), integer_result))
        self.assertTrue(ht.equal(ht.fmod(self.a_scalar, self.a_tensor), commutated_result))
        self.assertTrue(ht.equal(ht.fmod(self.a_split_tensor, self.a_tensor), commutated_result))
        self.assertTrue(ht.allclose(ht.fmod(a_float, another_float), result_float))

        with self.assertRaises(ValueError):
            ht.fmod(self.a_tensor, self.another_vector)
        with self.assertRaises(TypeError):
            ht.fmod(self.a_tensor, self.errorneous_type)
        with self.assertRaises(TypeError):
            ht.fmod("T", "s")
Beispiel #10
0
    def test_log1p(self):
        elements = 15
        tmp = torch.arange(1,
                           elements,
                           dtype=torch.float64,
                           device=self.device.torch_device).log1p()
        comparison = ht.array(tmp)

        # logarithm of float32
        float32_tensor = ht.arange(1, elements, dtype=ht.float32)
        float32_log1p = ht.log1p(float32_tensor)
        self.assertIsInstance(float32_log1p, ht.DNDarray)
        self.assertEqual(float32_log1p.dtype, ht.float32)
        self.assertEqual(float32_log1p.dtype, ht.float32)
        self.assertTrue(
            ht.allclose(float32_log1p, comparison.astype(ht.float32)))

        # logarithm of float64
        float64_tensor = ht.arange(1, elements, dtype=ht.float64)
        float64_log1p = ht.log1p(float64_tensor)
        self.assertIsInstance(float64_log1p, ht.DNDarray)
        self.assertEqual(float64_log1p.dtype, ht.float64)
        self.assertEqual(float64_log1p.dtype, ht.float64)
        self.assertTrue(ht.allclose(float64_log1p, comparison))

        # logarithm of ints, automatic conversion to intermediate floats
        int32_tensor = ht.arange(1, elements, dtype=ht.int32)
        int32_log1p = ht.log1p(int32_tensor)
        self.assertIsInstance(int32_log1p, ht.DNDarray)
        self.assertEqual(int32_log1p.dtype, ht.float64)
        self.assertEqual(int32_log1p.dtype, ht.float64)
        self.assertTrue(ht.allclose(int32_log1p, comparison))

        # logarithm of longs, automatic conversion to intermediate floats
        int64_tensor = ht.arange(1, elements, dtype=ht.int64)
        int64_log1p = int64_tensor.log1p()
        self.assertIsInstance(int64_log1p, ht.DNDarray)
        self.assertEqual(int64_log1p.dtype, ht.float64)
        self.assertEqual(int64_log1p.dtype, ht.float64)
        self.assertTrue(ht.allclose(int64_log1p, comparison))

        # check exceptions
        with self.assertRaises(TypeError):
            ht.log1p([1, 2, 3])
        with self.assertRaises(TypeError):
            ht.log1p("hello world")
Beispiel #11
0
    def test_sqrt(self):
        elements = 25
        tmp = torch.arange(elements,
                           dtype=torch.float64,
                           device=self.device.torch_device).sqrt()
        comparison = ht.array(tmp)

        # square roots of float32
        float32_tensor = ht.arange(elements, dtype=ht.float32)
        float32_sqrt = ht.sqrt(float32_tensor)
        self.assertIsInstance(float32_sqrt, ht.DNDarray)
        self.assertEqual(float32_sqrt.dtype, ht.float32)
        self.assertEqual(float32_sqrt.dtype, ht.float32)
        self.assertTrue(
            ht.allclose(float32_sqrt, comparison.astype(ht.float32), 1e-06))

        # square roots of float64
        float64_tensor = ht.arange(elements, dtype=ht.float64)
        float64_sqrt = ht.sqrt(float64_tensor)
        self.assertIsInstance(float64_sqrt, ht.DNDarray)
        self.assertEqual(float64_sqrt.dtype, ht.float64)
        self.assertEqual(float64_sqrt.dtype, ht.float64)
        self.assertTrue(ht.allclose(float64_sqrt, comparison, 1e-06))

        # square roots of ints, automatic conversion to intermediate floats
        int32_tensor = ht.arange(elements, dtype=ht.int32)
        int32_sqrt = ht.sqrt(int32_tensor)
        self.assertIsInstance(int32_sqrt, ht.DNDarray)
        self.assertEqual(int32_sqrt.dtype, ht.float64)
        self.assertEqual(int32_sqrt.dtype, ht.float64)
        self.assertTrue(ht.allclose(int32_sqrt, comparison, 1e-06))

        # square roots of longs, automatic conversion to intermediate floats
        int64_tensor = ht.arange(elements, dtype=ht.int64)
        int64_sqrt = int64_tensor.sqrt()
        self.assertIsInstance(int64_sqrt, ht.DNDarray)
        self.assertEqual(int64_sqrt.dtype, ht.float64)
        self.assertEqual(int64_sqrt.dtype, ht.float64)
        self.assertTrue(ht.allclose(int64_sqrt, comparison, 1e-06))

        # check exceptions
        with self.assertRaises(TypeError):
            ht.sqrt([1, 2, 3])
        with self.assertRaises(TypeError):
            ht.sqrt("hello world")
Beispiel #12
0
    def test_exp2(self):
        elements = 10
        tmp = np.exp2(torch.arange(elements, dtype=torch.float64))
        comparison = ht.array(tmp)

        # exponential of float32
        float32_tensor = ht.arange(elements, dtype=ht.float32)
        float32_exp2 = ht.exp2(float32_tensor)
        self.assertIsInstance(float32_exp2, ht.DNDarray)
        self.assertEqual(float32_exp2.dtype, ht.float32)
        self.assertEqual(float32_exp2.dtype, ht.float32)
        self.assertTrue(
            ht.allclose(float32_exp2, comparison.astype(ht.float32)))

        # exponential of float64
        float64_tensor = ht.arange(elements, dtype=ht.float64)
        float64_exp2 = ht.exp2(float64_tensor)
        self.assertIsInstance(float64_exp2, ht.DNDarray)
        self.assertEqual(float64_exp2.dtype, ht.float64)
        self.assertEqual(float64_exp2.dtype, ht.float64)
        self.assertTrue(ht.allclose(float64_exp2, comparison))

        # exponential of ints, automatic conversion to intermediate floats
        int32_tensor = ht.arange(elements, dtype=ht.int32)
        int32_exp2 = ht.exp2(int32_tensor)
        self.assertIsInstance(int32_exp2, ht.DNDarray)
        self.assertEqual(int32_exp2.dtype, ht.float64)
        self.assertEqual(int32_exp2.dtype, ht.float64)
        self.assertTrue(ht.allclose(int32_exp2, comparison))

        # exponential of longs, automatic conversion to intermediate floats
        int64_tensor = ht.arange(elements, dtype=ht.int64)
        int64_exp2 = int64_tensor.exp2()
        self.assertIsInstance(int64_exp2, ht.DNDarray)
        self.assertEqual(int64_exp2.dtype, ht.float64)
        self.assertEqual(int64_exp2.dtype, ht.float64)
        self.assertTrue(ht.allclose(int64_exp2, comparison))

        # check exceptions
        with self.assertRaises(TypeError):
            ht.exp2([1, 2, 3])
        with self.assertRaises(TypeError):
            ht.exp2("hello world")
Beispiel #13
0
    def test_full(self):
        # simple tensor
        data = ht.full((10, 2), 4, device=ht_device)
        self.assertIsInstance(data, ht.DNDarray)
        self.assertEqual(data.shape, (10, 2))
        self.assertEqual(data.lshape, (10, 2))
        self.assertEqual(data.dtype, ht.float32)
        self.assertEqual(data._DNDarray__array.dtype, torch.float32)
        self.assertEqual(data.split, None)
        self.assertTrue(ht.allclose(data, ht.float32(4.0, device=ht_device)))

        # non-standard dtype tensor
        data = ht.full((10, 2), 4, dtype=ht.int32, device=ht_device)
        self.assertIsInstance(data, ht.DNDarray)
        self.assertEqual(data.shape, (10, 2))
        self.assertEqual(data.lshape, (10, 2))
        self.assertEqual(data.dtype, ht.int32)
        self.assertEqual(data._DNDarray__array.dtype, torch.int32)
        self.assertEqual(data.split, None)
        self.assertTrue(ht.allclose(data, ht.int32(4, device=ht_device)))

        # split tensor
        data = ht.full((10, 2), 4, split=0, device=ht_device)
        self.assertIsInstance(data, ht.DNDarray)
        self.assertEqual(data.shape, (10, 2))
        self.assertLessEqual(data.lshape[0], 10)
        self.assertEqual(data.lshape[1], 2)
        self.assertEqual(data.dtype, ht.float32)
        self.assertEqual(data._DNDarray__array.dtype, torch.float32)
        self.assertEqual(data.split, 0)
        self.assertTrue(ht.allclose(data, ht.float32(4.0, device=ht_device)))

        # exceptions
        with self.assertRaises(TypeError):
            ht.full("(2, 3,)", 4, dtype=ht.float64, device=ht_device)
        with self.assertRaises(ValueError):
            ht.full((-1, 3), 2, dtype=ht.float64, device=ht_device)
        with self.assertRaises(TypeError):
            ht.full((2, 3), dtype=ht.float64, split="axis", device=ht_device)
Beispiel #14
0
    def test_sqrt_method(self):
        elements = 25
        comparison = ht.arange(elements, dtype=ht.float64).sqrt()

        # square roots of float32
        float32_sqrt = ht.arange(elements, dtype=ht.float32).sqrt()
        self.assertIsInstance(float32_sqrt, ht.tensor)
        self.assertEqual(float32_sqrt.dtype, ht.float32)
        self.assertEqual(float32_sqrt.dtype, ht.float32)
        self.assertTrue(ht.allclose(float32_sqrt, comparison.astype(ht.float32), 1e-05))

        # square roots of float64
        float64_sqrt = ht.arange(elements, dtype=ht.float64).sqrt()
        self.assertIsInstance(float64_sqrt, ht.tensor)
        self.assertEqual(float64_sqrt.dtype, ht.float64)
        self.assertEqual(float64_sqrt.dtype, ht.float64)
        self.assertTrue(ht.allclose(float64_sqrt, comparison, 1e-05))

        # square roots of ints, automatic conversion to intermediate floats
        int32_sqrt = ht.arange(elements, dtype=ht.int32).sqrt()
        self.assertIsInstance(int32_sqrt, ht.tensor)
        self.assertEqual(int32_sqrt.dtype, ht.float64)
        self.assertEqual(int32_sqrt.dtype, ht.float64)
        self.assertTrue(ht.allclose(int32_sqrt, comparison, 1e-05))

        # square roots of longs, automatic conversion to intermediate floats
        int64_sqrt = ht.arange(elements, dtype=ht.int64).sqrt()
        self.assertIsInstance(int64_sqrt, ht.tensor)
        self.assertEqual(int64_sqrt.dtype, ht.float64)
        self.assertEqual(int64_sqrt.dtype, ht.float64)
        self.assertTrue(ht.allclose(int64_sqrt, comparison, 1e-05))

        # check exceptions
        with self.assertRaises(TypeError):
            ht.sqrt([1, 2, 3])
        with self.assertRaises(TypeError):
            ht.sqrt('hello world')
    def test_sign(self):
        # floats 1d
        a = ht.array([-1, -0.5, 0, 0.5, 1])
        signed = ht.sign(a)
        comparison = ht.array([-1.0, -1, 0, 1, 1])

        self.assertEqual(signed.dtype, comparison.dtype)
        self.assertEqual(signed.shape, comparison.shape)
        self.assertEqual(signed.device, a.device)
        self.assertEqual(signed.split, a.split)
        self.assertTrue(ht.equal(signed, comparison))

        # complex + 2d + split
        a = ht.array([[1 - 2j, -0.5 + 1j], [0, 4 + 6j]], split=0)
        signed = ht.sign(a)
        comparison = ht.array([[1 + 0j, -1 + 0j], [0 + 0j, 1 + 0j]], split=0)

        self.assertEqual(signed.dtype, comparison.dtype)
        self.assertEqual(signed.shape, comparison.shape)
        self.assertEqual(signed.device, a.device)
        self.assertEqual(signed.split, a.split)
        self.assertTrue(ht.allclose(signed.real, comparison.real))
        self.assertTrue(ht.allclose(signed.imag, comparison.imag, atol=2e-5))

        # complex + split + out
        a = ht.array([[1 - 2j, -0.5 + 1j], [0, 4 + 6j]], split=1)
        b = ht.empty_like(a)
        signed = ht.sign(a, b)
        comparison = ht.array([[1 + 0j, -1 + 0j], [0 + 0j, 1 + 0j]], split=1)

        self.assertIs(b, signed)
        self.assertEqual(signed.dtype, comparison.dtype)
        self.assertEqual(signed.shape, comparison.shape)
        self.assertEqual(signed.device, a.device)
        self.assertEqual(signed.split, a.split)
        self.assertTrue(ht.allclose(signed.real, comparison.real))
        self.assertTrue(ht.allclose(signed.imag, comparison.imag, atol=2e-5))

        # zeros + 3d + complex + split
        a = ht.zeros((4, 4, 4), dtype=ht.complex128, split=2)
        signed = ht.sign(a)
        comparison = ht.zeros((4, 4, 4), dtype=ht.complex128, split=2)

        self.assertEqual(signed.dtype, comparison.dtype)
        self.assertEqual(signed.shape, comparison.shape)
        self.assertEqual(signed.device, a.device)
        self.assertEqual(signed.split, a.split)
        self.assertTrue(ht.allclose(signed.real, comparison.real))
        self.assertTrue(ht.allclose(signed.imag, comparison.imag, atol=2e-5))
Beispiel #16
0
    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)
Beispiel #17
0
    def test_allclose(self):
        a = ht.float32([[2, 2], [2, 2]], device=ht_device)
        b = ht.float32([[2.00005, 2.00005], [2.00005, 2.00005]],
                       device=ht_device)
        c = ht.zeros((4, 6), split=0, device=ht_device)
        d = ht.zeros((4, 6), split=1, device=ht_device)
        e = ht.zeros((4, 6), device=ht_device)

        self.assertFalse(ht.allclose(a, b))
        self.assertTrue(ht.allclose(a, b, atol=1e-04))
        self.assertTrue(ht.allclose(a, b, rtol=1e-04))
        self.assertTrue(ht.allclose(a, 2))
        self.assertTrue(ht.allclose(a, 2.0))
        self.assertTrue(ht.allclose(2, a))
        self.assertTrue(ht.allclose(c, d))
        self.assertTrue(ht.allclose(c, e))
        self.assertTrue(e.allclose(c))

        with self.assertRaises(TypeError):
            ht.allclose(a, (2, 2, 2, 2))
        with self.assertRaises(TypeError):
            ht.allclose(a, "?")
        with self.assertRaises(TypeError):
            ht.allclose("?", a)
Beispiel #18
0
    def test_cdist(self):
        n = ht.communication.MPI_WORLD.size
        X = ht.ones((n * 2, 4), dtype=ht.float32, split=None)
        Y = ht.zeros((n * 2, 4), dtype=ht.float32, split=None)
        res_XX_cdist = ht.zeros((n * 2, n * 2), dtype=ht.float32, split=None)
        res_XX_rbf = ht.ones((n * 2, n * 2), dtype=ht.float32, split=None)
        res_XY_cdist = ht.ones(
            (n * 2, n * 2), dtype=ht.float32, split=None) * 2
        res_XY_rbf = ht.ones(
            (n * 2, n * 2), dtype=ht.float32, split=None) * math.exp(-1.0)

        # Case 1a: X.split == None, Y == None
        d = ht.spatial.cdist(X, quadratic_expansion=False)
        self.assertTrue(ht.equal(d, res_XX_cdist))
        self.assertEqual(d.split, None)

        d = ht.spatial.cdist(X, quadratic_expansion=True)
        self.assertTrue(ht.equal(d, res_XX_cdist))
        self.assertEqual(d.split, None)

        d = ht.spatial.rbf(X, quadratic_expansion=False)
        self.assertTrue(ht.equal(d, res_XX_rbf))
        self.assertEqual(d.split, None)

        d = ht.spatial.rbf(X, quadratic_expansion=True)
        self.assertTrue(ht.equal(d, res_XX_rbf))
        self.assertEqual(d.split, None)

        # Case 1b: X.split == None, Y != None, Y.split == None
        d = ht.spatial.cdist(X, Y, quadratic_expansion=False)
        self.assertTrue(ht.equal(d, res_XY_cdist))
        self.assertEqual(d.split, None)

        d = ht.spatial.cdist(X, Y, quadratic_expansion=True)
        self.assertTrue(ht.equal(d, res_XY_cdist))
        self.assertEqual(d.split, None)

        d = ht.spatial.rbf(X,
                           Y,
                           sigma=math.sqrt(2.0),
                           quadratic_expansion=False)
        self.assertTrue(ht.equal(d, res_XY_rbf))
        self.assertEqual(d.split, None)

        d = ht.spatial.rbf(X,
                           Y,
                           sigma=math.sqrt(2.0),
                           quadratic_expansion=True)
        self.assertTrue(ht.equal(d, res_XY_rbf))
        self.assertEqual(d.split, None)

        # Case 1c: X.split == None, Y != None, Y.split == 0
        Y = ht.zeros((n * 2, 4), dtype=ht.float32, split=0)
        res_XX_cdist = ht.zeros((n * 2, n * 2), dtype=ht.float32, split=1)
        res_XX_rbf = ht.ones((n * 2, n * 2), dtype=ht.float32, split=1)
        res_XY_cdist = ht.ones((n * 2, n * 2), dtype=ht.float32, split=1) * 2
        res_XY_rbf = ht.ones(
            (n * 2, n * 2), dtype=ht.float32, split=1) * math.exp(-1.0)

        d = ht.spatial.cdist(X, Y, quadratic_expansion=False)
        self.assertTrue(ht.equal(d, res_XY_cdist))
        self.assertEqual(d.split, 1)

        d = ht.spatial.cdist(X, Y, quadratic_expansion=True)
        self.assertTrue(ht.equal(d, res_XY_cdist))
        self.assertEqual(d.split, 1)

        d = ht.spatial.rbf(X,
                           Y,
                           sigma=math.sqrt(2.0),
                           quadratic_expansion=False)
        self.assertTrue(ht.equal(d, res_XY_rbf))
        self.assertEqual(d.split, 1)

        d = ht.spatial.rbf(X,
                           Y,
                           sigma=math.sqrt(2.0),
                           quadratic_expansion=True)
        self.assertTrue(ht.equal(d, res_XY_rbf))
        self.assertEqual(d.split, 1)

        # Case 2a: X.split == 0, Y == None
        X = ht.ones((n * 2, 4), dtype=ht.float32, split=0)
        Y = ht.zeros((n * 2, 4), dtype=ht.float32, split=None)
        res_XX_cdist = ht.zeros((n * 2, n * 2), dtype=ht.float32, split=0)
        res_XX_rbf = ht.ones((n * 2, n * 2), dtype=ht.float32, split=0)
        res_XY_cdist = ht.ones((n * 2, n * 2), dtype=ht.float32, split=0) * 2
        res_XY_rbf = ht.ones(
            (n * 2, n * 2), dtype=ht.float32, split=0) * math.exp(-1.0)

        d = ht.spatial.cdist(X, quadratic_expansion=False)
        self.assertTrue(ht.equal(d, res_XX_cdist))
        self.assertEqual(d.split, 0)

        d = ht.spatial.cdist(X, quadratic_expansion=True)
        self.assertTrue(ht.equal(d, res_XX_cdist))
        self.assertEqual(d.split, 0)

        d = ht.spatial.rbf(X, quadratic_expansion=False)
        self.assertTrue(ht.equal(d, res_XX_rbf))
        self.assertEqual(d.split, 0)

        d = ht.spatial.rbf(X, quadratic_expansion=True)
        self.assertTrue(ht.equal(d, res_XX_rbf))
        self.assertEqual(d.split, 0)

        # Case 2b: X.split == 0, Y != None, Y.split == None
        d = ht.spatial.cdist(X, Y, quadratic_expansion=False)
        self.assertTrue(ht.equal(d, res_XY_cdist))
        self.assertEqual(d.split, 0)

        d = ht.spatial.cdist(X, Y, quadratic_expansion=True)
        self.assertTrue(ht.equal(d, res_XY_cdist))
        self.assertEqual(d.split, 0)

        d = ht.spatial.rbf(X,
                           Y,
                           sigma=math.sqrt(2.0),
                           quadratic_expansion=False)
        self.assertTrue(ht.equal(d, res_XY_rbf))
        self.assertEqual(d.split, 0)

        d = ht.spatial.rbf(X,
                           Y,
                           sigma=math.sqrt(2.0),
                           quadratic_expansion=True)
        self.assertTrue(ht.equal(d, res_XY_rbf))
        self.assertEqual(d.split, 0)

        # Case 2c: X.split == 0, Y != None, Y.split == 0
        Y = ht.zeros((n * 2, 4), dtype=ht.float32, split=0)

        d = ht.spatial.cdist(X, Y, quadratic_expansion=False)
        self.assertTrue(ht.equal(d, res_XY_cdist))
        self.assertEqual(d.split, 0)

        d = ht.spatial.cdist(X, Y, quadratic_expansion=True)
        self.assertTrue(ht.equal(d, res_XY_cdist))
        self.assertEqual(d.split, 0)

        d = ht.spatial.rbf(X,
                           Y,
                           sigma=math.sqrt(2.0),
                           quadratic_expansion=False)
        self.assertTrue(ht.equal(d, res_XY_rbf))
        self.assertEqual(d.split, 0)

        d = ht.spatial.rbf(X,
                           Y,
                           sigma=math.sqrt(2.0),
                           quadratic_expansion=True)
        self.assertTrue(ht.equal(d, res_XY_rbf))
        self.assertEqual(d.split, 0)

        # Case 3 X.split == 1
        X = ht.ones((n * 2, 4), dtype=ht.float32, split=1)
        with self.assertRaises(NotImplementedError):
            ht.spatial.cdist(X)
        with self.assertRaises(NotImplementedError):
            ht.spatial.cdist(X, Y, quadratic_expansion=False)
        X = ht.ones((n * 2, 4), dtype=ht.float32, split=None)
        Y = ht.zeros((n * 2, 4), dtype=ht.float32, split=1)
        with self.assertRaises(NotImplementedError):
            ht.spatial.cdist(X, Y, quadratic_expansion=False)

        Z = ht.ones((n * 2, 6, 3), dtype=ht.float32, split=None)
        with self.assertRaises(NotImplementedError):
            ht.spatial.cdist(Z, quadratic_expansion=False)
        with self.assertRaises(NotImplementedError):
            ht.spatial.cdist(X, Z, quadratic_expansion=False)

        n = ht.communication.MPI_WORLD.size
        A = ht.ones((n * 2, 6), dtype=ht.float32, split=None)
        for i in range(n):
            A[2 * i, :] = A[2 * i, :] * (2 * i)
            A[2 * i + 1, :] = A[2 * i + 1, :] * (2 * i + 1)
        res = torch.cdist(A._DNDarray__array, A._DNDarray__array)

        A = ht.ones((n * 2, 6), dtype=ht.float32, split=0)
        for i in range(n):
            A[2 * i, :] = A[2 * i, :] * (2 * i)
            A[2 * i + 1, :] = A[2 * i + 1, :] * (2 * i + 1)
        B = A.astype(ht.int32)

        d = ht.spatial.cdist(A, B, quadratic_expansion=False)
        result = ht.array(res, dtype=ht.float64, split=0)
        self.assertTrue(ht.allclose(d, result, atol=1e-5))

        n = ht.communication.MPI_WORLD.size
        A = ht.ones((n * 2, 6), dtype=ht.float32, split=None)
        for i in range(n):
            A[2 * i, :] = A[2 * i, :] * (2 * i)
            A[2 * i + 1, :] = A[2 * i + 1, :] * (2 * i + 1)
        res = torch.cdist(A._DNDarray__array, A._DNDarray__array)

        A = ht.ones((n * 2, 6), dtype=ht.float32, split=0)
        for i in range(n):
            A[2 * i, :] = A[2 * i, :] * (2 * i)
            A[2 * i + 1, :] = A[2 * i + 1, :] * (2 * i + 1)
        B = A.astype(ht.int32)

        d = ht.spatial.cdist(A, B, quadratic_expansion=False)
        result = ht.array(res, dtype=ht.float64, split=0)
        self.assertTrue(ht.allclose(d, result, atol=1e-8))

        B = A.astype(ht.float64)
        d = ht.spatial.cdist(A, B, quadratic_expansion=False)
        result = ht.array(res, dtype=ht.float64, split=0)
        self.assertTrue(ht.allclose(d, result, atol=1e-8))

        B = A.astype(ht.int16)
        d = ht.spatial.cdist(A, B, quadratic_expansion=False)
        result = ht.array(res, dtype=ht.float32, split=0)
        self.assertTrue(ht.allclose(d, result, atol=1e-8))

        d = ht.spatial.cdist(B, quadratic_expansion=False)
        result = ht.array(res, dtype=ht.float32, split=0)
        self.assertTrue(ht.allclose(d, result, atol=1e-8))

        B = A.astype(ht.int32)
        d = ht.spatial.cdist(B, quadratic_expansion=False)
        result = ht.array(res, dtype=ht.float64, split=0)
        self.assertTrue(ht.allclose(d, result, atol=1e-8))

        B = A.astype(ht.float64)
        d = ht.spatial.cdist(B, quadratic_expansion=False)
        result = ht.array(res, dtype=ht.float64, split=0)
        self.assertTrue(ht.allclose(d, result, atol=1e-8))
Beispiel #19
0
    def test_cov(self):
        x = ht.array([[0, 2], [1, 1], [2, 0]], dtype=ht.float, split=1).T
        if x.comm.size < 3:
            cov = ht.cov(x)
            actual = ht.array([[1, -1], [-1, 1]], split=0)
            self.assertTrue(ht.equal(cov, actual))

        data = np.loadtxt("heat/datasets/data/iris.csv", delimiter=";")
        np_cov = np.cov(data[:, 0], data[:, 1:3], rowvar=False)

        htdata = ht.load("heat/datasets/data/iris.csv", sep=";", split=0)
        ht_cov = ht.cov(htdata[:, 0], htdata[:, 1:3], rowvar=False)
        comp = ht.array(np_cov, dtype=ht.float)
        self.assertTrue(ht.allclose(comp - ht_cov, 0, atol=1e-4))

        np_cov = np.cov(data, rowvar=False)
        ht_cov = ht.cov(htdata, rowvar=False)
        self.assertTrue(
            ht.allclose(ht.array(np_cov, dtype=ht.float) - ht_cov,
                        0,
                        atol=1e-4))

        np_cov = np.cov(data, rowvar=False, ddof=1)
        ht_cov = ht.cov(htdata, rowvar=False, ddof=1)
        self.assertTrue(
            ht.allclose(ht.array(np_cov, dtype=ht.float) - ht_cov,
                        0,
                        atol=1e-4))

        np_cov = np.cov(data, rowvar=False, bias=True)
        ht_cov = ht.cov(htdata, rowvar=False, bias=True)
        self.assertTrue(
            ht.allclose(ht.array(np_cov, dtype=ht.float) - ht_cov,
                        0,
                        atol=1e-4))

        if 1 < x.comm.size < 5:
            htdata = ht.load("heat/datasets/data/iris.csv", sep=";", split=1)
            np_cov = np.cov(data, rowvar=False)
            ht_cov = ht.cov(htdata, rowvar=False)
            self.assertTrue(
                ht.allclose(ht.array(np_cov, dtype=ht.float),
                            ht_cov,
                            atol=1e-4))

            np_cov = np.cov(data, data, rowvar=True)

            htdata = ht.load("heat/datasets/data/iris.csv", sep=";", split=0)
            ht_cov = ht.cov(htdata, htdata, rowvar=True)
            self.assertTrue(
                ht.allclose(ht.array(np_cov, dtype=ht.float),
                            ht_cov,
                            atol=1e-4))

            htdata = ht.load("heat/datasets/data/iris.csv", sep=";", split=0)
            with self.assertRaises(RuntimeError):
                ht.cov(htdata[1:], rowvar=False)
            with self.assertRaises(RuntimeError):
                ht.cov(htdata, htdata[1:], rowvar=False)

        with self.assertRaises(TypeError):
            ht.cov(np_cov)
        with self.assertRaises(TypeError):
            ht.cov(htdata, np_cov)
        with self.assertRaises(TypeError):
            ht.cov(htdata, ddof="str")
        with self.assertRaises(ValueError):
            ht.cov(ht.zeros((1, 2, 3)))
        with self.assertRaises(ValueError):
            ht.cov(htdata, ht.zeros((1, 2, 3)))
        with self.assertRaises(ValueError):
            ht.cov(htdata, ddof=10000)
Beispiel #20
0
    def test_mean(self):
        array_0_len = 5
        array_1_len = 5
        array_2_len = 5

        x = ht.zeros((2, 3, 4))
        with self.assertRaises(ValueError):
            x.mean(axis=10)
        with self.assertRaises(ValueError):
            x.mean(axis=[4])
        with self.assertRaises(ValueError):
            x.mean(axis=[-4])
        with self.assertRaises(TypeError):
            ht.mean(x, axis="01")
        with self.assertRaises(ValueError):
            ht.mean(x, axis=(0, "10"))
        with self.assertRaises(ValueError):
            ht.mean(x, axis=(0, 0))
        with self.assertRaises(ValueError):
            ht.mean(x, axis=torch.Tensor([0, 0]))

        a = ht.arange(1, 5)
        self.assertEqual(a.mean(), 2.5)

        # ones
        dimensions = []

        for d in [array_0_len, array_1_len, array_2_len]:
            dimensions.extend([d])
            hold = list(range(len(dimensions)))
            hold.append(None)
            for split in hold:  # loop over the number of split dimension of the test array
                z = ht.ones(dimensions, split=split)
                res = z.mean()
                total_dims_list = list(z.shape)
                self.assertTrue((res == 1).all())
                for it in range(
                        len(z.shape)
                ):  # loop over the different single dimensions for mean
                    res = z.mean(axis=it)
                    self.assertTrue((res == 1).all())
                    target_dims = [
                        total_dims_list[q] for q in range(len(total_dims_list))
                        if q != it
                    ]
                    if not target_dims:
                        target_dims = ()
                    self.assertEqual(res.gshape, tuple(target_dims))
                    if z.split is None:
                        sp = None
                    else:
                        sp = z.split if it > z.split else z.split - 1
                        if it == split:
                            sp = None
                    self.assertEqual(res.split, sp)
                loop_list = [
                    ",".join(map(str, comb))
                    for comb in combinations(list(range(len(z.shape))), 2)
                ]

                for it in loop_list:  # loop over the different combinations of dimensions for mean
                    lp_split = [int(q) for q in it.split(",")]
                    res = z.mean(axis=lp_split)
                    self.assertTrue((res == 1).all())
                    target_dims = [
                        total_dims_list[q] for q in range(len(total_dims_list))
                        if q not in lp_split
                    ]
                    if not target_dims:
                        target_dims = (1, )
                    if res.gshape:
                        self.assertEqual(res.gshape, tuple(target_dims))
                    if res.split is not None:
                        if any([split >= x for x in lp_split]):
                            self.assertEqual(res.split, len(target_dims) - 1)
                        else:
                            self.assertEqual(res.split, z.split)

        # values for the iris dataset mean measured by libreoffice calc
        ax0 = ht.array(
            [5.84333333333333, 3.054, 3.75866666666667, 1.19866666666667])
        for sp in [None, 0, 1]:
            iris = ht.load("heat/datasets/data/iris.csv", sep=";", split=sp)
            self.assertTrue(ht.allclose(ht.mean(iris), 3.46366666666667))
            self.assertTrue(ht.allclose(ht.mean(iris, axis=0), ax0))
Beispiel #21
0
    def test_var(self):
        array_0_len = ht.MPI_WORLD.size * 2
        array_1_len = ht.MPI_WORLD.size * 2
        array_2_len = ht.MPI_WORLD.size * 2

        # test raises
        x = ht.zeros((2, 3, 4))
        with self.assertRaises(ValueError):
            x.var(axis=10)
        with self.assertRaises(ValueError):
            x.var(axis=[4])
        with self.assertRaises(ValueError):
            x.var(axis=[-4])
        with self.assertRaises(TypeError):
            ht.var(x, axis="01")
        with self.assertRaises(ValueError):
            ht.var(x, axis=(0, "10"))
        with self.assertRaises(ValueError):
            ht.var(x, axis=(0, 0))
        with self.assertRaises(NotImplementedError):
            ht.var(x, ddof=2)
        with self.assertRaises(ValueError):
            ht.var(x, ddof=-2)
        with self.assertRaises(ValueError):
            ht.mean(x, axis=torch.Tensor([0, 0]))

        a = ht.arange(1, 5)
        self.assertEqual(a.var(ddof=1), 1.666666666666666)

        # ones
        dimensions = []
        for d in [array_0_len, array_1_len, array_2_len]:
            dimensions.extend([d])
            hold = list(range(len(dimensions)))
            hold.append(None)
            for split in hold:  # loop over the number of dimensions of the test array
                z = ht.ones(dimensions, split=split)
                res = z.var(ddof=0)
                total_dims_list = list(z.shape)
                self.assertTrue((res == 0).all())
                # loop over the different single dimensions for var
                for it in range(len(z.shape)):
                    res = z.var(axis=it)
                    self.assertTrue(ht.allclose(res, 0))
                    target_dims = [
                        total_dims_list[q] for q in range(len(total_dims_list))
                        if q != it
                    ]
                    if not target_dims:
                        target_dims = ()
                    self.assertEqual(res.gshape, tuple(target_dims))
                    if z.split is None:
                        sp = None
                    else:
                        sp = z.split if it > z.split else z.split - 1
                        if it == split:
                            sp = None
                    self.assertEqual(res.split, sp)
                    if split == it:
                        res = z.var(axis=it)
                        self.assertTrue(ht.allclose(res, 0))
                loop_list = [
                    ",".join(map(str, comb))
                    for comb in combinations(list(range(len(z.shape))), 2)
                ]

                for it in loop_list:  # loop over the different combinations of dimensions for var
                    lp_split = [int(q) for q in it.split(",")]
                    res = z.var(axis=lp_split)
                    self.assertTrue((res == 0).all())
                    target_dims = [
                        total_dims_list[q] for q in range(len(total_dims_list))
                        if q not in lp_split
                    ]
                    if not target_dims:
                        target_dims = (1, )
                    if res.gshape:
                        self.assertEqual(res.gshape, tuple(target_dims))
                    if res.split is not None:
                        if any([split >= x for x in lp_split]):
                            self.assertEqual(res.split, len(target_dims) - 1)
                        else:
                            self.assertEqual(res.split, z.split)

        # values for the iris dataset var measured by libreoffice calc
        for sp in [None, 0, 1]:
            iris = ht.load("heat/datasets/data/iris.csv", sep=";", split=sp)
            self.assertTrue(
                ht.allclose(ht.var(iris, bessel=True), 3.90318519755147))
Beispiel #22
0
    def test_qr(self):
        m, n = 20, 40
        st = torch.randn(m,
                         n,
                         device=self.device.torch_device,
                         dtype=torch.float)
        a_comp = ht.array(st, split=0)
        for t in range(1, 3):
            for sp in range(2):
                a = ht.array(st, split=sp, dtype=torch.float)
                qr = a.qr(tiles_per_proc=t)
                self.assertTrue(
                    ht.allclose((a_comp - (qr.Q @ qr.R)),
                                0,
                                rtol=1e-5,
                                atol=1e-5))
                self.assertTrue(
                    ht.allclose(qr.Q.T @ qr.Q, ht.eye(m), rtol=1e-5,
                                atol=1e-5))
                self.assertTrue(
                    ht.allclose(ht.eye(m), qr.Q @ qr.Q.T, rtol=1e-5,
                                atol=1e-5))
        m, n = 40, 40
        st1 = torch.randn(m, n, device=self.device.torch_device)
        a_comp1 = ht.array(st1, split=0)
        for t in range(1, 3):
            for sp in range(2):
                a1 = ht.array(st1, split=sp)
                qr1 = a1.qr(tiles_per_proc=t)
                self.assertTrue(
                    ht.allclose((a_comp1 - (qr1.Q @ qr1.R)),
                                0,
                                rtol=1e-5,
                                atol=1e-5))
                self.assertTrue(
                    ht.allclose(qr1.Q.T @ qr1.Q,
                                ht.eye(m),
                                rtol=1e-5,
                                atol=1e-5))
                self.assertTrue(
                    ht.allclose(ht.eye(m),
                                qr1.Q @ qr1.Q.T,
                                rtol=1e-5,
                                atol=1e-5))
        m, n = 40, 20
        st2 = torch.randn(m,
                          n,
                          dtype=torch.double,
                          device=self.device.torch_device)
        a_comp2 = ht.array(st2, split=0, dtype=ht.double)
        for t in range(1, 3):
            for sp in range(2):
                a2 = ht.array(st2, split=sp)
                qr2 = a2.qr(tiles_per_proc=t)
                self.assertTrue(
                    ht.allclose(a_comp2, qr2.Q @ qr2.R, rtol=1e-5, atol=1e-5))
                self.assertTrue(
                    ht.allclose(qr2.Q.T @ qr2.Q,
                                ht.eye(m, dtype=ht.double),
                                rtol=1e-5,
                                atol=1e-5))
                self.assertTrue(
                    ht.allclose(ht.eye(m, dtype=ht.double),
                                qr2.Q @ qr2.Q.T,
                                rtol=1e-5,
                                atol=1e-5))
                # test if calc R alone works
                qr = ht.qr(a2, calc_q=False, overwrite_a=True)
                self.assertTrue(qr.Q is None)

        m, n = 40, 20
        st = torch.randn(m, n, device=self.device.torch_device)
        a_comp = ht.array(st, split=None)
        a = ht.array(st, split=None)
        qr = a.qr()
        self.assertTrue(ht.allclose(a_comp, qr.Q @ qr.R, rtol=1e-5, atol=1e-5))
        self.assertTrue(
            ht.allclose(qr.Q.T @ qr.Q, ht.eye(m), rtol=1e-5, atol=1e-5))
        self.assertTrue(
            ht.allclose(ht.eye(m), qr.Q @ qr.Q.T, rtol=1e-5, atol=1e-5))

        # raises
        with self.assertRaises(TypeError):
            ht.qr(np.zeros((10, 10)))
        with self.assertRaises(TypeError):
            ht.qr(a_comp, tiles_per_proc="ls")
        with self.assertRaises(TypeError):
            ht.qr(a_comp, tiles_per_proc=1, calc_q=30)
        with self.assertRaises(TypeError):
            ht.qr(a_comp, tiles_per_proc=1, overwrite_a=30)
        with self.assertRaises(ValueError):
            ht.qr(a_comp, tiles_per_proc=torch.tensor([1, 2, 3]))
        with self.assertRaises(ValueError):
            ht.qr(ht.zeros((3, 4, 5)))
Beispiel #23
0
    def test_var(self):
        array_0_len = ht.MPI_WORLD.size * 2
        array_1_len = ht.MPI_WORLD.size * 2
        array_2_len = ht.MPI_WORLD.size * 2

        # test raises
        x = ht.zeros((2, 3, 4), device=ht_device)
        with self.assertRaises(TypeError):
            ht.var(x, axis=0, bessel=1)
        with self.assertRaises(ValueError):
            ht.var(x, axis=10)
        with self.assertRaises(TypeError):
            ht.var(x, axis="01")

        a = ht.arange(1, 5, device=ht_device)
        self.assertEqual(a.var(), 1.666666666666666)

        # ones
        dimensions = []
        for d in [array_0_len, array_1_len, array_2_len]:
            dimensions.extend([d])
            hold = list(range(len(dimensions)))
            hold.append(None)
            for split in hold:  # loop over the number of dimensions of the test array
                z = ht.ones(dimensions, split=split, device=ht_device)
                res = z.var()
                total_dims_list = list(z.shape)
                self.assertTrue((res == 0).all())
                # loop over the different single dimensions for mean
                for it in range(len(z.shape)):
                    res = z.var(axis=it)
                    self.assertTrue(ht.allclose(res, 0))
                    target_dims = [
                        total_dims_list[q] for q in range(len(total_dims_list))
                        if q != it
                    ]
                    if not target_dims:
                        target_dims = ()
                    # print(split, it, z.shape, res.shape)
                    self.assertEqual(res.gshape, tuple(target_dims))
                    # if res.split is not None:
                    #     if i >= it:
                    #         self.assertEqual(res.split, len(target_dims) - 1)
                    #     else:
                    #         self.assertEqual(res.split, z.split)
                    if z.split is None:
                        sp = None
                    else:
                        sp = z.split if it > z.split else z.split - 1
                        if it == split:
                            sp = None
                    self.assertEqual(res.split, sp)
                    if split == it:
                        res = z.var(axis=it)
                        self.assertTrue(ht.allclose(res, 0))
                z = ht.ones(dimensions, split=split, device=ht_device)
                res = z.var(bessel=False)
                self.assertTrue(ht.allclose(res, 0))

        # values for the iris dataset var measured by libreoffice calc
        for sp in [None, 0, 1]:
            iris = ht.load("heat/datasets/data/iris.csv",
                           sep=";",
                           split=sp,
                           device=ht_device)
            self.assertTrue(
                ht.allclose(ht.var(iris, bessel=True), 3.90318519755147))
Beispiel #24
0
    def test_full(self):
        # simple tensor
        data = ht.full((
            10,
            2,
        ), 4)
        self.assertIsInstance(data, ht.tensor)
        self.assertEqual(data.shape, (
            10,
            2,
        ))
        self.assertEqual(data.lshape, (
            10,
            2,
        ))
        self.assertEqual(data.dtype, ht.float32)
        self.assertEqual(data._tensor__array.dtype, torch.float32)
        self.assertEqual(data.split, None)
        self.assertTrue(ht.allclose(data, ht.float32(4.0)))

        # non-standard dtype tensor
        data = ht.full((
            10,
            2,
        ), 4, dtype=ht.int32)
        self.assertIsInstance(data, ht.tensor)
        self.assertEqual(data.shape, (
            10,
            2,
        ))
        self.assertEqual(data.lshape, (
            10,
            2,
        ))
        self.assertEqual(data.dtype, ht.int32)
        self.assertEqual(data._tensor__array.dtype, torch.int32)
        self.assertEqual(data.split, None)
        self.assertTrue(ht.allclose(data, ht.int32(4)))

        # split tensor
        data = ht.full((
            10,
            2,
        ), 4, split=0)
        self.assertIsInstance(data, ht.tensor)
        self.assertEqual(data.shape, (
            10,
            2,
        ))
        self.assertLessEqual(data.lshape[0], 10)
        self.assertEqual(data.lshape[1], 2)
        self.assertEqual(data.dtype, ht.float32)
        self.assertEqual(data._tensor__array.dtype, torch.float32)
        self.assertEqual(data.split, 0)
        self.assertTrue(ht.allclose(data, ht.float32(4.0)))

        # exceptions
        with self.assertRaises(TypeError):
            ht.full('(2, 3,)', 4, dtype=ht.float64)
        with self.assertRaises(ValueError):
            ht.full((
                -1,
                3,
            ), 2, dtype=ht.float64)
        with self.assertRaises(TypeError):
            ht.full((
                2,
                3,
            ), dtype=ht.float64, split='axis')