def test_tensordot_basic(app_inst: ArrayApplication): shape = 2, 4, 10, 15 npX = np.arange(np.product(shape)).reshape(*shape) rX = app_inst.array(npX, block_shape=(1, 2, 10, 3)) rResult = rX.T.tensordot(rX, axes=1) assert np.allclose(rResult.get(), (np.tensordot(npX.T, npX, axes=1))) common.check_block_integrity(rResult)
def test_tensordot_all_shapes(app_inst: ArrayApplication): for axes in [0, 1, 2]: if axes == 2: a = np.arange(7 * 6 * 4).reshape((7, 6, 4)) b = np.arange(6 * 4 * 9).reshape((6, 4, 9)) c = np.tensordot(a, b, axes=axes) elif axes in (1, 0): a = np.arange(7 * 6 * 4).reshape((7, 6, 4)) b = np.arange(6 * 4 * 9).reshape((4, 6, 9)) c = np.tensordot(a, b, axes=axes) else: raise Exception() a_block_shapes = list( itertools.product( *list(map(lambda x: list(range(1, x + 1)), a.shape)))) b_block_shapes = list( itertools.product( *list(map(lambda x: list(range(1, x + 1)), b.shape)))) pbar = tqdm.tqdm(total=np.product( [len(a_block_shapes), len(b_block_shapes)])) for a_block_shape in a_block_shapes: for b_block_shape in b_block_shapes: pbar.update(1) if a_block_shape[-axes:] != b_block_shape[:axes]: continue pbar.set_description( "axes=%s %s @ %s" % (str(axes), str(a_block_shape), str(b_block_shape))) block_a = app_inst.array(a, block_shape=a_block_shape) block_b = app_inst.array(b, block_shape=b_block_shape) block_c = block_a.tensordot(block_b, axes=axes) assert np.allclose(block_c.get(), c) common.check_block_integrity(block_c)
def test_bops(bop_data: tuple): X, Y, npX, npY = bop_data # Add Z = X + Y npZ = npX + npY assert np.allclose(Z.get(), npZ) common.check_block_integrity(Z) # Subtract Z = X - Y npZ = npX - npY assert np.allclose(Z.get(), npZ) common.check_block_integrity(Z) # Multiply Z = X * Y npZ = npX * npY assert np.allclose(Z.get(), npZ) common.check_block_integrity(Z) # Divide Z = X / Y npZ = npX / npY assert np.allclose(Z.get(), npZ) common.check_block_integrity(Z) # Power Z = X**Y npZ = npX**npY assert np.allclose(Z.get(), npZ) common.check_block_integrity(Z)
def test_tensordot_large_shape(app_inst: ArrayApplication): a = np.arange(4 * 6 * 10 * 90).reshape((90, 10, 6, 4)) b = np.arange(4 * 6 * 10 * 75).reshape((4, 6, 10, 75)) c = np.tensordot(a, b, axes=1) block_a = app_inst.array(a, block_shape=(30, 5, 3, 2)) block_b = app_inst.array(b, block_shape=(2, 3, 5, 25)) block_c = block_a.tensordot(block_b, axes=1) assert np.allclose(block_c.get(), c) common.check_block_integrity(block_c)
def test_concatenate(app_inst: ArrayApplication): axis = 1 real_X, _ = BimodalGaussian.get_dataset(1000, 9) real_ones = np.ones(shape=(1000, 1)) X = app_inst.array(real_X, block_shape=(100, 9)) ones = app_inst.ones((1000, 1), (100, 1), dtype=X.dtype) X_concated = app_inst.concatenate([X, ones], axis=axis, axis_block_size=X.block_shape[axis]) common.check_block_integrity(X_concated) real_X_concated = np.concatenate([real_X, real_ones], axis=axis) assert np.allclose(X_concated.get(), real_X_concated) real_X2 = np.random.random_sample(1000 * 17).reshape(1000, 17) X2 = app_inst.array(real_X2, block_shape=(X.block_shape[0], 3)) X_concated = app_inst.concatenate([X, ones, X2], axis=axis, axis_block_size=X.block_shape[axis]) common.check_block_integrity(X_concated) real_X_concated = np.concatenate([real_X, real_ones, real_X2], axis=axis) assert np.allclose(X_concated.get(), real_X_concated) y1 = app_inst.zeros(shape=(50, ), block_shape=(10, ), dtype=int) y2 = app_inst.ones(shape=(50, ), block_shape=(10, ), dtype=int) y = app_inst.concatenate([y1, y2], axis=0) common.check_block_integrity(y)
def test_conversions(conversions_data: tuple): X, npX, Y = conversions_data # Add Z = X + Y npZ = npX + Y assert np.allclose(Z.get(), npZ) common.check_block_integrity(Z) if isinstance(Y, np.ndarray): with pytest.raises(TypeError): Z = Y + X else: Z = Y + X npZ = Y + npX assert np.allclose(Z.get(), npZ) common.check_block_integrity(Z) # Subtract Z = X - Y npZ = npX - Y assert np.allclose(Z.get(), npZ) common.check_block_integrity(Z) if isinstance(Y, np.ndarray): with pytest.raises(TypeError): Z = Y - X else: Z = Y - X npZ = Y - npX assert np.allclose(Z.get(), npZ) common.check_block_integrity(Z) # Multiply Z = X * Y npZ = npX * Y assert np.allclose(Z.get(), npZ) common.check_block_integrity(Z) if isinstance(Y, np.ndarray): with pytest.raises(TypeError): Z = Y * X else: Z = Y * X npZ = Y * npX assert np.allclose(Z.get(), npZ) common.check_block_integrity(Z) # Divide Z = X / Y npZ = npX / Y assert np.allclose(Z.get(), npZ) common.check_block_integrity(Z) if isinstance(Y, np.ndarray): with pytest.raises(TypeError): Z = Y / X else: Z = Y / X npZ = Y / npX assert np.allclose(Z.get(), npZ) common.check_block_integrity(Z) # Power Z = X**Y npZ = npX**Y assert np.allclose(Z.get(), npZ) common.check_block_integrity(Z) if isinstance(Y, np.ndarray): with pytest.raises(TypeError): Z = Y**X else: Z = Y**X npZ = Y**npX assert np.allclose(Z.get(), npZ) common.check_block_integrity(Z)
def test_array_integrity(app_inst: ArrayApplication): shape = 12, 21 npX = np.arange(np.product(shape)).reshape(*shape) X = app_inst.array(npX, block_shape=(6, 7)) common.check_block_integrity(X)