def test_eye(self): def get_offset(tensor_array): x, y = tensor_array.shape for k in range(x): for l in range(y): if tensor_array[k][l] == 1: return k, l return x, y shape = 5 eye = ht.eye(shape, dtype=ht.uint8, split=1, device=ht_device) self.assertIsInstance(eye, ht.DNDarray) self.assertEqual(eye.dtype, ht.uint8) self.assertEqual(eye.shape, (shape, shape)) self.assertEqual(eye.split, 1) offset_x, offset_y = get_offset(eye._DNDarray__array) self.assertGreaterEqual(offset_x, 0) self.assertGreaterEqual(offset_y, 0) x, y = eye._DNDarray__array.shape for i in range(x): for j in range(y): expected = 1 if i - offset_x is j - offset_y else 0 self.assertEqual(eye._DNDarray__array[i][j], expected) shape = (10, 20) eye = ht.eye(shape, dtype=ht.float32, device=ht_device) self.assertIsInstance(eye, ht.DNDarray) self.assertEqual(eye.dtype, ht.float32) self.assertEqual(eye.shape, shape) self.assertEqual(eye.split, None) offset_x, offset_y = get_offset(eye._DNDarray__array) self.assertGreaterEqual(offset_x, 0) self.assertGreaterEqual(offset_y, 0) x, y = eye._DNDarray__array.shape for i in range(x): for j in range(y): expected = 1.0 if i - offset_x is j - offset_y else 0.0 self.assertEqual(eye._DNDarray__array[i][j], expected) shape = (10,) eye = ht.eye(shape, dtype=ht.int32, split=0, device=ht_device) self.assertIsInstance(eye, ht.DNDarray) self.assertEqual(eye.dtype, ht.int32) self.assertEqual(eye.shape, shape * 2) self.assertEqual(eye.split, 0) offset_x, offset_y = get_offset(eye._DNDarray__array) self.assertGreaterEqual(offset_x, 0) self.assertGreaterEqual(offset_y, 0) x, y = eye._DNDarray__array.shape for i in range(x): for j in range(y): expected = 1 if i - offset_x is j - offset_y else 0 self.assertEqual(eye._DNDarray__array[i][j], expected)
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))
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_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)))