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")
def test_abs(self): float32_tensor = ht.arange(-10, 10, dtype=ht.float32, split=0) absolute_values = ht.abs(float32_tensor) # basic absolute test self.assertIsInstance(absolute_values, ht.tensor) self.assertEqual(absolute_values.dtype, ht.float32) self.assertEqual(absolute_values.sum(axis=0), 100) # check whether output works output_tensor = ht.zeros(20, split=0) self.assertEqual(output_tensor.sum(axis=0), 0) ht.absolute(float32_tensor, out=output_tensor) self.assertEqual(output_tensor.sum(axis=0), 100) # dtype parameter int64_tensor = ht.arange(-10, 10, dtype=ht.int64) absolute_values = ht.abs(int64_tensor, dtype=ht.float32) self.assertIsInstance(absolute_values, ht.tensor) self.assertEqual(absolute_values.sum(axis=0), 100) self.assertEqual(absolute_values.dtype, ht.float32) self.assertEqual(absolute_values._tensor__array.dtype, torch.float32) # exceptions with self.assertRaises(TypeError): ht.absolute('hello') with self.assertRaises(TypeError): float32_tensor.abs(out=1) with self.assertRaises(TypeError): float32_tensor.absolute(out=float32_tensor, dtype=3.2)
def test_save(self): if ht.io.supports_hdf5(): # local range local_range = ht.arange(100) local_range.save(self.HDF5_OUT_PATH, self.HDF5_DATASET) if local_range.comm.rank == 0: with ht.io.h5py.File(self.HDF5_OUT_PATH, 'r') as handle: comparison = torch.tensor(handle[self.HDF5_DATASET], dtype=torch.int32) self.assertTrue((local_range._tensor__array == comparison).all()) # split range split_range = ht.arange(100, split=0) split_range.save(self.HDF5_OUT_PATH, self.HDF5_DATASET) if split_range.comm.rank == 0: with ht.io.h5py.File(self.HDF5_OUT_PATH, 'r') as handle: comparison = torch.tensor(handle[self.HDF5_DATASET], dtype=torch.int32) self.assertTrue((local_range._tensor__array == comparison).all()) if ht.io.supports_netcdf(): # local range local_range = ht.arange(100) local_range.save(self.NETCDF_OUT_PATH, self.NETCDF_VARIABLE) if local_range.comm.rank == 0: with ht.io.nc.Dataset(self.NETCDF_OUT_PATH, 'r') as handle: comparison = torch.tensor(handle[self.NETCDF_VARIABLE][:], dtype=torch.int32) self.assertTrue((local_range._tensor__array == comparison).all()) # split range split_range = ht.arange(100, split=0) split_range.save(self.NETCDF_OUT_PATH, self.NETCDF_VARIABLE) if split_range.comm.rank == 0: with ht.io.nc.Dataset(self.NETCDF_OUT_PATH, 'r') as handle: comparison = torch.tensor(handle[self.NETCDF_VARIABLE][:], dtype=torch.int32) self.assertTrue((local_range._tensor__array == comparison).all())
def test_unique(self): size = ht.MPI_WORLD.size rank = ht.MPI_WORLD.rank torch_array = torch.arange(size, dtype=torch.int32, device=device).expand(size, size) split_zero = ht.array(torch_array, split=0, device=ht_device) exp_axis_none = ht.array([rank], dtype=ht.int32, device=ht_device) res = split_zero.unique(sorted=True) self.assertTrue((res._DNDarray__array == exp_axis_none._DNDarray__array).all()) exp_axis_zero = ht.arange(size, dtype=ht.int32, device=ht_device).expand_dims(0) res = ht.unique(split_zero, sorted=True, axis=0) self.assertTrue((res._DNDarray__array == exp_axis_zero._DNDarray__array).all()) exp_axis_one = ht.array([rank], dtype=ht.int32, device=ht_device).expand_dims(0) split_zero_transposed = ht.array(torch_array.transpose(0, 1), split=0, device=ht_device) res = ht.unique(split_zero_transposed, sorted=False, axis=1) self.assertTrue((res._DNDarray__array == exp_axis_one._DNDarray__array).all()) split_one = ht.array(torch_array, dtype=ht.int32, split=1, device=ht_device) exp_axis_none = ht.arange(size, dtype=ht.int32, device=ht_device) res = ht.unique(split_one, sorted=True) self.assertTrue((res._DNDarray__array == exp_axis_none._DNDarray__array).all()) exp_axis_zero = ht.array([rank], dtype=ht.int32, device=ht_device).expand_dims(0) res = ht.unique(split_one, sorted=False, axis=0) self.assertTrue((res._DNDarray__array == exp_axis_zero._DNDarray__array).all()) exp_axis_one = ht.array([rank] * size, dtype=ht.int32, device=ht_device).expand_dims(1) res = ht.unique(split_one, sorted=True, axis=1) self.assertTrue((res._DNDarray__array == exp_axis_one._DNDarray__array).all()) torch_array = torch.tensor( [[1, 2], [2, 3], [1, 2], [2, 3], [1, 2]], dtype=torch.int32, device=device ) data = ht.array(torch_array, split=0, device=ht_device) res, inv = ht.unique(data, return_inverse=True, axis=0) _, exp_inv = torch_array.unique(dim=0, return_inverse=True, sorted=True) self.assertTrue(torch.equal(inv, exp_inv.to(dtype=inv.dtype))) res, inv = ht.unique(data, return_inverse=True, axis=1) _, exp_inv = torch_array.unique(dim=1, return_inverse=True, sorted=True) self.assertTrue(torch.equal(inv, exp_inv.to(dtype=inv.dtype))) torch_array = torch.tensor( [[1, 1, 2], [1, 2, 2], [2, 1, 2], [1, 3, 2], [0, 1, 2]], dtype=torch.int32, device=device, ) exp_res, exp_inv = torch_array.unique(return_inverse=True, sorted=True) data_split_none = ht.array(torch_array, device=ht_device) res, inv = ht.unique(data_split_none, return_inverse=True, sorted=True) self.assertTrue(torch.equal(inv, exp_inv.to(dtype=inv.dtype))) data_split_zero = ht.array(torch_array, split=0, device=ht_device) res, inv = ht.unique(data_split_zero, return_inverse=True, sorted=True) self.assertTrue(torch.equal(inv, exp_inv.to(dtype=inv.dtype)))
def test_clip(self): elements = 20 # float tensor float32_tensor = ht.arange(elements, dtype=ht.float32, split=0) clipped = float32_tensor.clip(5, 15) self.assertIsInstance(clipped, ht.DNDarray) self.assertEqual(clipped.dtype, ht.float32) self.assertEqual(clipped.sum(axis=0), 195) # long tensor int64_tensor = ht.arange(elements, dtype=ht.int64, split=0) clipped = int64_tensor.clip(4, 16) self.assertIsInstance(clipped, ht.DNDarray) self.assertEqual(clipped.dtype, ht.int64) self.assertEqual(clipped.sum(axis=0), 194) # test the exceptions with self.assertRaises(TypeError): ht.clip(torch.arange(10, device=self.device.torch_device), 2, 5) with self.assertRaises(ValueError): ht.arange(20).clip(None, None) with self.assertRaises(TypeError): ht.clip(ht.arange(20), 5, 15, out=torch.arange(20, device=self.device.torch_device))
def test_ceil(self): start, end, step = -5.0, 5.0, 1.4 comparison = torch.arange(start, end, step, dtype=torch.float64, device=self.device.torch_device).ceil() # exponential of float32 float32_tensor = ht.arange(start, end, step, dtype=ht.float32) float32_floor = float32_tensor.ceil() self.assertIsInstance(float32_floor, ht.DNDarray) self.assertEqual(float32_floor.dtype, ht.float32) self.assertEqual(float32_floor.dtype, ht.float32) self.assertTrue( (float32_floor._DNDarray__array == comparison.float()).all()) # exponential of float64 float64_tensor = ht.arange(start, end, step, dtype=ht.float64) float64_floor = float64_tensor.ceil() self.assertIsInstance(float64_floor, ht.DNDarray) self.assertEqual(float64_floor.dtype, ht.float64) self.assertEqual(float64_floor.dtype, ht.float64) self.assertTrue((float64_floor._DNDarray__array == comparison).all()) # check exceptions with self.assertRaises(TypeError): ht.ceil([0, 1, 2, 3]) with self.assertRaises(TypeError): ht.ceil(object())
def test_save_hdf5(self): # HDF5 support is optional if not ht.io.supports_hdf5(): return # local unsplit data local_data = ht.arange(100) ht.save_hdf5(local_data, self.HDF5_OUT_PATH, self.HDF5_DATASET, dtype=local_data.dtype.char()) if local_data.comm.rank == 0: with ht.io.h5py.File(self.HDF5_OUT_PATH, "r") as handle: comparison = torch.tensor(handle[self.HDF5_DATASET], dtype=torch.int32, device=self.device.torch_device) self.assertTrue((local_data.larray == comparison).all()) # distributed data range split_data = ht.arange(100, split=0) ht.save_hdf5(split_data, self.HDF5_OUT_PATH, self.HDF5_DATASET, dtype=split_data.dtype.char()) if split_data.comm.rank == 0: with ht.io.h5py.File(self.HDF5_OUT_PATH, "r") as handle: comparison = torch.tensor(handle[self.HDF5_DATASET], dtype=torch.int32, device=self.device.torch_device) self.assertTrue((local_data.larray == comparison).all())
def test_floor(self): start, end, step = -5.0, 5.0, 1.4 comparison = torch.arange(start, end, step, dtype=torch.float64).floor() # exponential of float32 float32_tensor = ht.arange(start, end, step, dtype=ht.float32) float32_floor = float32_tensor.floor() self.assertIsInstance(float32_floor, ht.tensor) self.assertEqual(float32_floor.dtype, ht.float32) self.assertEqual(float32_floor.dtype, ht.float32) self.assertTrue((float32_floor._tensor__array == comparison.type( torch.float32)).all()) # exponential of float64 float64_tensor = ht.arange(start, end, step, dtype=ht.float64) float64_floor = float64_tensor.floor() self.assertIsInstance(float64_floor, ht.tensor) self.assertEqual(float64_floor.dtype, ht.float64) self.assertEqual(float64_floor.dtype, ht.float64) self.assertTrue((float64_floor._tensor__array == comparison).all()) # check exceptions with self.assertRaises(TypeError): ht.floor([0, 1, 2, 3]) with self.assertRaises(TypeError): ht.floor(object())
def test_save_netcdf(self): # netcdf support is optional if not ht.io.supports_netcdf(): return # local unsplit data local_data = ht.arange(100) ht.save_netcdf(local_data, self.NETCDF_OUT_PATH, self.NETCDF_VARIABLE) if local_data.comm.rank == 0: with ht.io.nc.Dataset(self.NETCDF_OUT_PATH, "r") as handle: comparison = torch.tensor( handle[self.NETCDF_VARIABLE][:], dtype=torch.int32, device=self.device.torch_device, ) self.assertTrue((local_data.larray == comparison).all()) # distributed data range split_data = ht.arange(100, split=0) ht.save_netcdf(split_data, self.NETCDF_OUT_PATH, self.NETCDF_VARIABLE) if split_data.comm.rank == 0: with ht.io.nc.Dataset(self.NETCDF_OUT_PATH, "r") as handle: comparison = torch.tensor( handle[self.NETCDF_VARIABLE][:], dtype=torch.int32, device=self.device.torch_device, ) self.assertTrue((local_data.larray == comparison).all())
def test_init_raises(self): # need to test the raises here with self.assertRaises(TypeError): ht.core.tiling.SquareDiagTiles("sdkd", tiles_per_proc=1) with self.assertRaises(TypeError): ht.core.tiling.SquareDiagTiles(ht.arange(2), tiles_per_proc="sdf") with self.assertRaises(ValueError): ht.core.tiling.SquareDiagTiles(ht.arange(2), tiles_per_proc=0) with self.assertRaises(ValueError): ht.core.tiling.SquareDiagTiles(ht.arange(2), tiles_per_proc=1)
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)
def test_cos(self): # base elements elements = 30 comparison = torch.arange(elements, dtype=torch.float64).cos() # cosine of float32 float32_tensor = ht.arange(elements, dtype=ht.float32) float32_cos = ht.cos(float32_tensor) self.assertIsInstance(float32_cos, ht.tensor) self.assertEqual(float32_cos.dtype, ht.float32) self.assertEqual(float32_cos.dtype, ht.float32) self.assertTrue( torch.allclose(float32_cos._tensor__array.type(torch.double), comparison)) # cosine of float64 float64_tensor = ht.arange(elements, dtype=ht.float64) float64_cos = ht.cos(float64_tensor) self.assertIsInstance(float64_cos, ht.tensor) self.assertEqual(float64_cos.dtype, ht.float64) self.assertEqual(float64_cos.dtype, ht.float64) self.assertTrue( torch.allclose(float64_cos._tensor__array.type(torch.double), comparison)) # cosine of ints, automatic conversion to intermediate floats int32_tensor = ht.arange(elements, dtype=ht.int32) int32_cos = ht.cos(int32_tensor) self.assertIsInstance(int32_cos, ht.tensor) self.assertEqual(int32_cos.dtype, ht.float64) self.assertEqual(int32_cos.dtype, ht.float64) self.assertTrue( torch.allclose(float32_cos._tensor__array.type(torch.double), comparison)) # cosine of longs, automatic conversion to intermediate floats int64_tensor = ht.arange(elements, dtype=ht.int64) int64_cos = ht.cos(int64_tensor) self.assertIsInstance(int64_cos, ht.tensor) self.assertEqual(int64_cos.dtype, ht.float64) self.assertEqual(int64_cos.dtype, ht.float64) self.assertTrue( torch.allclose(int64_cos._tensor__array.type(torch.double), comparison)) # check exceptions with self.assertRaises(TypeError): ht.cos([1, 2, 3]) with self.assertRaises(TypeError): ht.cos('hello world')
def test_save_exception(self): data = ht.arange(1) if ht.io.supports_hdf5(): with self.assertRaises(TypeError): ht.save(1, self.HDF5_OUT_PATH, self.HDF5_DATASET) with self.assertRaises(TypeError): ht.save(data, 1, self.HDF5_DATASET) with self.assertRaises(TypeError): ht.save(data, self.HDF5_OUT_PATH, 1) else: with self.assertRaises(RuntimeError): ht.save(data, self.HDF5_OUT_PATH, self.HDF5_DATASET) if ht.io.supports_netcdf(): with self.assertRaises(TypeError): ht.save(1, self.NETCDF_OUT_PATH, self.NETCDF_VARIABLE) with self.assertRaises(TypeError): ht.save(data, 1, self.NETCDF_VARIABLE) with self.assertRaises(TypeError): ht.save(data, self.NETCDF_OUT_PATH, 1) with self.assertRaises(ValueError): ht.save(data, self.NETCDF_OUT_PATH, self.NETCDF_VARIABLE, mode="r") with self.assertRaises((ValueError, IndexError)): ht.save(data, self.NETCDF_OUT_PATH, self.NETCDF_VARIABLE) ht.save( ht.arange(2, split=0), self.NETCDF_OUT_PATH, self.NETCDF_VARIABLE, file_slices=slice(None), mode="a", ) with self.assertRaises((ValueError, IndexError)): ht.save(data, self.NETCDF_OUT_PATH, self.NETCDF_VARIABLE) ht.save( ht.arange(2, split=None), self.NETCDF_OUT_PATH, self.NETCDF_VARIABLE, file_slices=slice(None), mode="a", ) else: with self.assertRaises(RuntimeError): ht.save(data, self.NETCDF_OUT_PATH, self.NETCDF_VARIABLE) with self.assertRaises(ValueError): ht.save(1, "data.dat")
def test_split_1_below_threshold(self): ht.set_printoptions(sci_mode=True) dndarray = ht.arange(0.5, 4 * 5 * 6 + 0.5, dtype=ht.float64).reshape( (4, 5, 6)).resplit_(1) comparison = ( "DNDarray([[[5.0000e-01, 1.5000e+00, 2.5000e+00, 3.5000e+00, 4.5000e+00, 5.5000e+00],\n" " [6.5000e+00, 7.5000e+00, 8.5000e+00, 9.5000e+00, 1.0500e+01, 1.1500e+01],\n" " [1.2500e+01, 1.3500e+01, 1.4500e+01, 1.5500e+01, 1.6500e+01, 1.7500e+01],\n" " [1.8500e+01, 1.9500e+01, 2.0500e+01, 2.1500e+01, 2.2500e+01, 2.3500e+01],\n" " [2.4500e+01, 2.5500e+01, 2.6500e+01, 2.7500e+01, 2.8500e+01, 2.9500e+01]],\n" "\n" " [[3.0500e+01, 3.1500e+01, 3.2500e+01, 3.3500e+01, 3.4500e+01, 3.5500e+01],\n" " [3.6500e+01, 3.7500e+01, 3.8500e+01, 3.9500e+01, 4.0500e+01, 4.1500e+01],\n" " [4.2500e+01, 4.3500e+01, 4.4500e+01, 4.5500e+01, 4.6500e+01, 4.7500e+01],\n" " [4.8500e+01, 4.9500e+01, 5.0500e+01, 5.1500e+01, 5.2500e+01, 5.3500e+01],\n" " [5.4500e+01, 5.5500e+01, 5.6500e+01, 5.7500e+01, 5.8500e+01, 5.9500e+01]],\n" "\n" " [[6.0500e+01, 6.1500e+01, 6.2500e+01, 6.3500e+01, 6.4500e+01, 6.5500e+01],\n" " [6.6500e+01, 6.7500e+01, 6.8500e+01, 6.9500e+01, 7.0500e+01, 7.1500e+01],\n" " [7.2500e+01, 7.3500e+01, 7.4500e+01, 7.5500e+01, 7.6500e+01, 7.7500e+01],\n" " [7.8500e+01, 7.9500e+01, 8.0500e+01, 8.1500e+01, 8.2500e+01, 8.3500e+01],\n" " [8.4500e+01, 8.5500e+01, 8.6500e+01, 8.7500e+01, 8.8500e+01, 8.9500e+01]],\n" "\n" " [[9.0500e+01, 9.1500e+01, 9.2500e+01, 9.3500e+01, 9.4500e+01, 9.5500e+01],\n" " [9.6500e+01, 9.7500e+01, 9.8500e+01, 9.9500e+01, 1.0050e+02, 1.0150e+02],\n" " [1.0250e+02, 1.0350e+02, 1.0450e+02, 1.0550e+02, 1.0650e+02, 1.0750e+02],\n" " [1.0850e+02, 1.0950e+02, 1.1050e+02, 1.1150e+02, 1.1250e+02, 1.1350e+02],\n" " [1.1450e+02, 1.1550e+02, 1.1650e+02, 1.1750e+02, 1.1850e+02, 1.1950e+02]]], dtype=ht.float64, device=cpu:0, split=1)" ) __str = str(dndarray) if dndarray.comm.rank == 0: self.assertEqual(comparison, __str)
def test_split_2_below_threshold(self): dndarray = ht.arange(4 * 5 * 6, dtype=ht.uint8).reshape( (4, 5, 6)).resplit_(2) comparison = ( "DNDarray([[[ 0, 1, 2, 3, 4, 5],\n" " [ 6, 7, 8, 9, 10, 11],\n" " [ 12, 13, 14, 15, 16, 17],\n" " [ 18, 19, 20, 21, 22, 23],\n" " [ 24, 25, 26, 27, 28, 29]],\n" "\n" " [[ 30, 31, 32, 33, 34, 35],\n" " [ 36, 37, 38, 39, 40, 41],\n" " [ 42, 43, 44, 45, 46, 47],\n" " [ 48, 49, 50, 51, 52, 53],\n" " [ 54, 55, 56, 57, 58, 59]],\n" "\n" " [[ 60, 61, 62, 63, 64, 65],\n" " [ 66, 67, 68, 69, 70, 71],\n" " [ 72, 73, 74, 75, 76, 77],\n" " [ 78, 79, 80, 81, 82, 83],\n" " [ 84, 85, 86, 87, 88, 89]],\n" "\n" " [[ 90, 91, 92, 93, 94, 95],\n" " [ 96, 97, 98, 99, 100, 101],\n" " [102, 103, 104, 105, 106, 107],\n" " [108, 109, 110, 111, 112, 113],\n" " [114, 115, 116, 117, 118, 119]]], dtype=ht.uint8, device=cpu:0, split=2)" ) __str = str(dndarray) if dndarray.comm.rank == 0: self.assertEqual(comparison, __str)
def test_sqrt_out_of_place(self): elements = 30 output_shape = (3, elements) number_range = ht.arange(elements, dtype=ht.float32) output_buffer = ht.zeros(output_shape, dtype=ht.float32) # square roots float32_sqrt = ht.sqrt(number_range, out=output_buffer) comparison = torch.arange(elements, dtype=torch.float32).sqrt() # check whether the input range remain unchanged self.assertIsInstance(number_range, ht.tensor) self.assertEqual(number_range.sum(axis=0), 190) # gaussian sum self.assertEqual(number_range.gshape, (elements,)) # check whether the output buffer still has the correct shape self.assertIsInstance(float32_sqrt, ht.tensor) self.assertEqual(float32_sqrt.dtype, ht.float32) self.assertEqual(float32_sqrt._tensor__array.shape, output_shape) for row in range(output_shape[0]): self.assertTrue((float32_sqrt._tensor__array[row] == comparison).all()) # exception with self.assertRaises(TypeError): ht.sqrt(number_range, 'hello world')
def setUpClass(cls): N = ht.MPI_WORLD.size cls.reference_tensor = ht.zeros((N, N + 1, 2 * N), device=ht_device) for n in range(N): for m in range(N + 1): cls.reference_tensor[n, m, :] = ht.arange(0, 2 * N) + m * 10 + n * 100
def test_split_2_above_threshold(self): ht.set_printoptions(threshold=1) dndarray = ht.arange(3 * 10 * 12).reshape((3, 10, 12)).resplit_(2) comparison = ( "DNDarray([[[ 0, 1, 2, ..., 9, 10, 11],\n" " [ 12, 13, 14, ..., 21, 22, 23],\n" " [ 24, 25, 26, ..., 33, 34, 35],\n" " ...,\n" " [ 84, 85, 86, ..., 93, 94, 95],\n" " [ 96, 97, 98, ..., 105, 106, 107],\n" " [108, 109, 110, ..., 117, 118, 119]],\n" "\n" " [[120, 121, 122, ..., 129, 130, 131],\n" " [132, 133, 134, ..., 141, 142, 143],\n" " [144, 145, 146, ..., 153, 154, 155],\n" " ...,\n" " [204, 205, 206, ..., 213, 214, 215],\n" " [216, 217, 218, ..., 225, 226, 227],\n" " [228, 229, 230, ..., 237, 238, 239]],\n" "\n" " [[240, 241, 242, ..., 249, 250, 251],\n" " [252, 253, 254, ..., 261, 262, 263],\n" " [264, 265, 266, ..., 273, 274, 275],\n" " ...,\n" " [324, 325, 326, ..., 333, 334, 335],\n" " [336, 337, 338, ..., 345, 346, 347],\n" " [348, 349, 350, ..., 357, 358, 359]]], dtype=ht.int32, device=cpu:0, split=2)" ) __str = str(dndarray) if dndarray.comm.rank == 0: self.assertEqual(comparison, __str)
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)
def test_local_printing(self): x = ht.arange(15 * 5, dtype=ht.float).reshape((15, 5)).resplit(0) global_comp = ( "DNDarray([[ 0., 1., 2., 3., 4.],\n" " [ 5., 6., 7., 8., 9.],\n" " [10., 11., 12., 13., 14.],\n" " [15., 16., 17., 18., 19.],\n" " [20., 21., 22., 23., 24.],\n" " [25., 26., 27., 28., 29.],\n" " [30., 31., 32., 33., 34.],\n" " [35., 36., 37., 38., 39.],\n" " [40., 41., 42., 43., 44.],\n" " [45., 46., 47., 48., 49.],\n" " [50., 51., 52., 53., 54.],\n" " [55., 56., 57., 58., 59.],\n" " [60., 61., 62., 63., 64.],\n" " [65., 66., 67., 68., 69.],\n" " [70., 71., 72., 73., 74.]], dtype=ht.float32, device=cpu:0, split=0)" ) if x.comm.rank == 0: self.assertEqual(str(x), global_comp) else: self.assertEqual(str(x), "") ht.local_printing() local_comp = ("[[ 0., 1., 2., 3., 4.],\n" " [ 5., 6., 7., 8., 9.],\n" " [10., 11., 12., 13., 14.],\n" " [15., 16., 17., 18., 19.],\n" " [20., 21., 22., 23., 24.]], device: cpu:0, split: 0") if x.comm.rank == 0 and x.comm.size == 3: self.assertEqual(str(x), local_comp) ht.global_printing( ) # needed to keep things correct for the other tests
def test_split_1_above_threshold(self): ht.set_printoptions(edgeitems=2) dndarray = ht.arange(10 * 11 * 12).reshape((10, 11, 12)).resplit_(1) comparison = ( "DNDarray([[[ 0, 1, ..., 10, 11],\n" " [ 12, 13, ..., 22, 23],\n" " ...,\n" " [ 108, 109, ..., 118, 119],\n" " [ 120, 121, ..., 130, 131]],\n" "\n" " [[ 132, 133, ..., 142, 143],\n" " [ 144, 145, ..., 154, 155],\n" " ...,\n" " [ 240, 241, ..., 250, 251],\n" " [ 252, 253, ..., 262, 263]],\n" "\n" " ...,\n" "\n" " [[1056, 1057, ..., 1066, 1067],\n" " [1068, 1069, ..., 1078, 1079],\n" " ...,\n" " [1164, 1165, ..., 1174, 1175],\n" " [1176, 1177, ..., 1186, 1187]],\n" "\n" " [[1188, 1189, ..., 1198, 1199],\n" " [1200, 1201, ..., 1210, 1211],\n" " ...,\n" " [1296, 1297, ..., 1306, 1307],\n" " [1308, 1309, ..., 1318, 1319]]], dtype=ht.int32, device=cpu:0, split=1)" ) __str = str(dndarray) if dndarray.comm.rank == 0: self.assertEqual(comparison, __str)
def test_arctan(self): # base elements elements = 30 comparison = torch.arange(elements, dtype=torch.float64, device=self.device.torch_device).atan() # arctan of float32 float32_tensor = ht.arange(elements, dtype=ht.float32) float32_arctan = ht.arctan(float32_tensor) self.assertIsInstance(float32_arctan, ht.DNDarray) self.assertEqual(float32_arctan.dtype, ht.float32) self.assertTrue( torch.allclose(float32_arctan._DNDarray__array.double(), comparison)) # arctan of float64 float64_tensor = ht.arange(elements, dtype=ht.float64) float64_arctan = ht.arctan(float64_tensor) self.assertIsInstance(float64_arctan, ht.DNDarray) self.assertEqual(float64_arctan.dtype, ht.float64) self.assertTrue( torch.allclose(float64_arctan._DNDarray__array.double(), comparison)) # arctan of ints, automatic conversion to intermediate floats int32_tensor = ht.arange(elements, dtype=ht.int32) int32_arctan = ht.arctan(int32_tensor) self.assertIsInstance(int32_arctan, ht.DNDarray) self.assertEqual(int32_arctan.dtype, ht.float32) self.assertTrue( torch.allclose(float32_arctan._DNDarray__array.double(), comparison)) # arctan of longs, automatic conversion to intermediate floats int64_tensor = ht.arange(elements, dtype=ht.int64) int64_arctan = ht.atan(int64_tensor) self.assertIsInstance(int64_arctan, ht.DNDarray) self.assertEqual(int64_arctan.dtype, ht.float64) self.assertTrue( torch.allclose(int64_arctan._DNDarray__array.double(), comparison)) # check exceptions with self.assertRaises(TypeError): ht.arctan([1, 2, 3]) with self.assertRaises(TypeError): ht.arctan("hello world")
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")
def setUpClass(cls): super(TestDNDarray, cls).setUpClass() N = ht.MPI_WORLD.size cls.reference_tensor = ht.zeros((N, N + 1, 2 * N)) for n in range(N): for m in range(N + 1): cls.reference_tensor[n, m, :] = ht.arange(0, 2 * N) + m * 10 + n * 100
def test_log10(self): elements = 15 comparison = torch.arange(1, elements, dtype=torch.float64).log10() # logarithm of float32 float32_tensor = ht.arange(1, elements, dtype=ht.float32) float32_log10 = ht.log10(float32_tensor) self.assertIsInstance(float32_log10, ht.tensor) self.assertEqual(float32_log10.dtype, ht.float32) self.assertEqual(float32_log10.dtype, ht.float32) in_range = (float32_log10._tensor__array - comparison.type(torch.float32)) < FLOAT_EPSILON self.assertTrue(in_range.all()) # logarithm of float64 float64_tensor = ht.arange(1, elements, dtype=ht.float64) float64_log10 = ht.log10(float64_tensor) self.assertIsInstance(float64_log10, ht.tensor) self.assertEqual(float64_log10.dtype, ht.float64) self.assertEqual(float64_log10.dtype, ht.float64) in_range = (float64_log10._tensor__array - comparison) < FLOAT_EPSILON self.assertTrue(in_range.all()) # logarithm of ints, automatic conversion to intermediate floats int32_tensor = ht.arange(1, elements, dtype=ht.int32) int32_log10 = ht.log10(int32_tensor) self.assertIsInstance(int32_log10, ht.tensor) self.assertEqual(int32_log10.dtype, ht.float64) self.assertEqual(int32_log10.dtype, ht.float64) in_range = (int32_log10._tensor__array - comparison) < FLOAT_EPSILON self.assertTrue(in_range.all()) # logarithm of longs, automatic conversion to intermediate floats int64_tensor = ht.arange(1, elements, dtype=ht.int64) int64_log10 = ht.log10(int64_tensor) self.assertIsInstance(int64_log10, ht.tensor) self.assertEqual(int64_log10.dtype, ht.float64) self.assertEqual(int64_log10.dtype, ht.float64) in_range = (int64_log10._tensor__array - comparison) < FLOAT_EPSILON self.assertTrue(in_range.all()) # check exceptions with self.assertRaises(TypeError): ht.log10([1, 2, 3]) with self.assertRaises(TypeError): ht.log10('hello world')
def test_exp2(self): elements = 10 comparison = np.exp2(torch.arange(elements, dtype=torch.float64)) # exponential of float32 float32_tensor = ht.arange(elements, dtype=ht.float32) float32_exp2 = ht.exp2(float32_tensor) self.assertIsInstance(float32_exp2, ht.tensor) self.assertEqual(float32_exp2.dtype, ht.float32) self.assertEqual(float32_exp2.dtype, ht.float32) in_range = (float32_exp2._tensor__array - comparison.type(torch.float32)) < FLOAT_EPSILON self.assertTrue(in_range.all()) # exponential of float64 float64_tensor = ht.arange(elements, dtype=ht.float64) float64_exp2 = ht.exp2(float64_tensor) self.assertIsInstance(float64_exp2, ht.tensor) self.assertEqual(float64_exp2.dtype, ht.float64) self.assertEqual(float64_exp2.dtype, ht.float64) in_range = (float64_exp2._tensor__array - comparison) < FLOAT_EPSILON self.assertTrue(in_range.all()) # 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.tensor) self.assertEqual(int32_exp2.dtype, ht.float64) self.assertEqual(int32_exp2.dtype, ht.float64) in_range = (int32_exp2._tensor__array - comparison) < FLOAT_EPSILON self.assertTrue(in_range.all()) # exponential of longs, automatic conversion to intermediate floats int64_tensor = ht.arange(elements, dtype=ht.int64) int64_exp2 = ht.exp2(int64_tensor) self.assertIsInstance(int64_exp2, ht.tensor) self.assertEqual(int64_exp2.dtype, ht.float64) self.assertEqual(int64_exp2.dtype, ht.float64) in_range = (int64_exp2._tensor__array - comparison) < FLOAT_EPSILON self.assertTrue(in_range.all()) # check exceptions with self.assertRaises(TypeError): ht.exp2([1, 2, 3]) with self.assertRaises(TypeError): ht.exp2('hello world')
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")
def test_unsplit_above_threshold(self): dndarray = ht.arange(12 * 13 * 14).reshape((12, 13, 14)) comparison = ( "DNDarray([[[ 0, 1, 2, ..., 11, 12, 13],\n" " [ 14, 15, 16, ..., 25, 26, 27],\n" " [ 28, 29, 30, ..., 39, 40, 41],\n" " ...,\n" " [ 140, 141, 142, ..., 151, 152, 153],\n" " [ 154, 155, 156, ..., 165, 166, 167],\n" " [ 168, 169, 170, ..., 179, 180, 181]],\n" "\n" " [[ 182, 183, 184, ..., 193, 194, 195],\n" " [ 196, 197, 198, ..., 207, 208, 209],\n" " [ 210, 211, 212, ..., 221, 222, 223],\n" " ...,\n" " [ 322, 323, 324, ..., 333, 334, 335],\n" " [ 336, 337, 338, ..., 347, 348, 349],\n" " [ 350, 351, 352, ..., 361, 362, 363]],\n" "\n" " [[ 364, 365, 366, ..., 375, 376, 377],\n" " [ 378, 379, 380, ..., 389, 390, 391],\n" " [ 392, 393, 394, ..., 403, 404, 405],\n" " ...,\n" " [ 504, 505, 506, ..., 515, 516, 517],\n" " [ 518, 519, 520, ..., 529, 530, 531],\n" " [ 532, 533, 534, ..., 543, 544, 545]],\n" "\n" " ...,\n" "\n" " [[1638, 1639, 1640, ..., 1649, 1650, 1651],\n" " [1652, 1653, 1654, ..., 1663, 1664, 1665],\n" " [1666, 1667, 1668, ..., 1677, 1678, 1679],\n" " ...,\n" " [1778, 1779, 1780, ..., 1789, 1790, 1791],\n" " [1792, 1793, 1794, ..., 1803, 1804, 1805],\n" " [1806, 1807, 1808, ..., 1817, 1818, 1819]],\n" "\n" " [[1820, 1821, 1822, ..., 1831, 1832, 1833],\n" " [1834, 1835, 1836, ..., 1845, 1846, 1847],\n" " [1848, 1849, 1850, ..., 1859, 1860, 1861],\n" " ...,\n" " [1960, 1961, 1962, ..., 1971, 1972, 1973],\n" " [1974, 1975, 1976, ..., 1985, 1986, 1987],\n" " [1988, 1989, 1990, ..., 1999, 2000, 2001]],\n" "\n" " [[2002, 2003, 2004, ..., 2013, 2014, 2015],\n" " [2016, 2017, 2018, ..., 2027, 2028, 2029],\n" " [2030, 2031, 2032, ..., 2041, 2042, 2043],\n" " ...,\n" " [2142, 2143, 2144, ..., 2153, 2154, 2155],\n" " [2156, 2157, 2158, ..., 2167, 2168, 2169],\n" " [2170, 2171, 2172, ..., 2181, 2182, 2183]]], dtype=ht.int32, device=cpu:0, split=None)" ) __str = str(dndarray) if dndarray.comm.rank == 0: self.assertEqual(comparison, __str)
def label_to_one_hot(a): max_label = ht.max(a) a = a.expand_dims(1) items = ht.arange(0, max_label.item() + 1) one_hot = ht.stack([items for i in range(a.shape[0])], axis=0) one_hot = ht.where(one_hot == a, 1, 0) return one_hot
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")