def _test_lincomb(fn, a, b): # Validate lincomb against the result on host with randomized # data and given a,b, contiguous and non-contiguous # Unaliased arguments [xarr, yarr, zarr], [x, y, z] = example_vectors(fn, 3) zarr[:] = a * xarr + b * yarr fn.lincomb(a, x, b, y, out=z) assert all_almost_equal([x, y, z], [xarr, yarr, zarr]) # First argument aliased with output [xarr, yarr, zarr], [x, y, z] = example_vectors(fn, 3) zarr[:] = a * zarr + b * yarr fn.lincomb(a, z, b, y, out=z) assert all_almost_equal([x, y, z], [xarr, yarr, zarr]) # Second argument aliased with output [xarr, yarr, zarr], [x, y, z] = example_vectors(fn, 3) zarr[:] = a * xarr + b * zarr fn.lincomb(a, x, b, z, out=z) assert all_almost_equal([x, y, z], [xarr, yarr, zarr]) # Both arguments aliased with each other [xarr, yarr, zarr], [x, y, z] = example_vectors(fn, 3) zarr[:] = a * xarr + b * xarr fn.lincomb(a, x, b, x, out=z) assert all_almost_equal([x, y, z], [xarr, yarr, zarr]) # All aliased [xarr, yarr, zarr], [x, y, z] = example_vectors(fn, 3) zarr[:] = a * zarr + b * zarr fn.lincomb(a, z, b, z, out=z) assert all_almost_equal([x, y, z], [xarr, yarr, zarr])
def test_multiply(fn): # space method [x_arr, y_arr, out_arr], [x, y, out] = example_vectors(fn, 3) out_arr = x_arr * y_arr fn.multiply(x, y, out) assert all_almost_equal([x_arr, y_arr, out_arr], [x, y, out]) # member method [x_arr, y_arr, out_arr], [x, y, out] = example_vectors(fn, 3) out_arr = x_arr * y_arr out.multiply(x, y) assert all_almost_equal([x_arr, y_arr, out_arr], [x, y, out])
def test_vector_norm(exponent): rn = CudaRn(5) xarr, x = example_vectors(rn) weight = _pos_vector(CudaRn(5)) weighting = CudaFnVectorWeighting(weight, exponent=exponent) if exponent in (1.0, float('inf')): true_norm = np.linalg.norm(weight.asarray() * xarr, ord=exponent) else: true_norm = np.linalg.norm(weight.asarray() ** (1 / exponent) * xarr, ord=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): weighting.norm(x) else: assert almost_equal(weighting.norm(x), true_norm) # Same with free function pnorm = odl.cu_weighted_norm(weight, exponent=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): pnorm(x) else: assert almost_equal(pnorm(x), true_norm)
def test_const_dist(exponent): rn = CudaRn(5) [xarr, yarr], [x, y] = example_vectors(rn, n=2) constant = 1.5 weighting = CudaFnConstWeighting(constant, exponent=exponent) factor = 1 if exponent == float('inf') else constant ** (1 / exponent) true_dist = factor * np.linalg.norm(xarr - yarr, ord=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): weighting.dist(x, y) else: assert almost_equal(weighting.dist(x, y), true_dist) # Same with free function pdist = odl.cu_weighted_dist(constant, exponent=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): pdist(x, y) else: assert almost_equal(pdist(x, y), true_dist)
def test_custom_inner(fn): [xarr, yarr], [x, y] = example_vectors(fn, 2) def inner(x, y): return np.vdot(y, x) w = CudaFnCustomInnerProduct(inner) w_same = CudaFnCustomInnerProduct(inner) w_other = CudaFnCustomInnerProduct(np.dot) w_d = CudaFnCustomInnerProduct(inner, dist_using_inner=False) assert w == w assert w == w_same assert w != w_other assert w != w_d true_inner = inner(xarr, yarr) assert almost_equal(w.inner(x, y), true_inner) true_norm = np.linalg.norm(xarr) assert almost_equal(w.norm(x), true_norm) true_dist = np.linalg.norm(xarr - yarr) # Using 3 places (single precision default) since the result is always # double even if the underlying computation was only single precision assert almost_equal(w.dist(x, y), true_dist, places=3) assert almost_equal(w_d.dist(x, y), true_dist) with pytest.raises(TypeError): CudaFnCustomInnerProduct(1)
def test_const_norm(exponent): rn = CudaRn(5) xarr, x = example_vectors(rn) constant = 1.5 weighting = CudaFnConstWeighting(constant, exponent=exponent) factor = 1 if exponent == float('inf') else constant ** (1 / exponent) true_norm = factor * np.linalg.norm(xarr, ord=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): weighting.norm(x) else: assert almost_equal(weighting.norm(x), true_norm) # Same with free function pnorm = odl.cu_weighted_norm(constant, exponent=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): pnorm(x) else: assert almost_equal(pnorm(x), true_norm)
def test_multiply(fn): # Validates multiply against the result on host with randomized data [xarr, yarr, zarr], [x_device, y_device, z_device] = example_vectors(fn, 3) # Host side calculation zarr[:] = xarr * yarr # Device side calculation fn.multiply(x_device, y_device, out=z_device) assert all_almost_equal([x_device, y_device, z_device], [xarr, yarr, zarr]) # Aliased zarr[:] = xarr * zarr fn.multiply(z_device, x_device, out=z_device) assert all_almost_equal([x_device, z_device], [xarr, zarr]) # Aliased zarr[:] = zarr * zarr fn.multiply(z_device, z_device, out=z_device) assert all_almost_equal(z_device, zarr)
def test_dist(fn): [xarr, yarr], [x, y] = example_vectors(fn, n=2) correct_dist = np.linalg.norm(xarr - yarr) assert almost_equal(fn.dist(x, y), correct_dist) assert almost_equal(x.dist(y), correct_dist)
def test_pnorm(exponent): for fn in (Rn(3, exponent=exponent), Cn(3, exponent=exponent)): xarr, x = example_vectors(fn) correct_norm = np.linalg.norm(xarr, ord=exponent) assert almost_equal(fn.norm(x), correct_norm) assert almost_equal(x.norm(), correct_norm)
def test_norm(fn): xarr, x = example_vectors(fn) correct_norm = np.linalg.norm(xarr) assert almost_equal(fn.norm(x), correct_norm) assert almost_equal(x.norm(), correct_norm)
def _impl_test_reduction(fn, name): ufunc = getattr(np, name) # Create some data x_arr, x = example_vectors(fn, 1) assert ufunc(x_arr) == getattr(x.ufunc, name)()
def test_custom_dist(fn): [xarr, yarr], [x, y] = example_vectors(fn, 2) def dist(x, y): return np.linalg.norm(x - y) def other_dist(x, y): return np.linalg.norm(x - y, ord=1) w = CudaFnCustomDist(dist) w_same = CudaFnCustomDist(dist) w_other = CudaFnCustomDist(other_dist) assert w == w assert w == w_same assert w != w_other with pytest.raises(NotImplementedError): w.inner(x, y) with pytest.raises(NotImplementedError): w.norm(x) true_dist = np.linalg.norm(xarr - yarr) assert almost_equal(w.dist(x, y), true_dist) with pytest.raises(TypeError): CudaFnCustomDist(1)
def test_vector_dist(exponent): rn = CudaRn(5) [xarr, yarr], [x, y] = example_vectors(rn, n=2) weight = _pos_vector(CudaRn(5)) weighting = CudaFnVectorWeighting(weight, exponent=exponent) if exponent in (1.0, float('inf')): true_dist = np.linalg.norm(weight.asarray() * (xarr - yarr), ord=exponent) else: true_dist = np.linalg.norm( weight.asarray() ** (1 / exponent) * (xarr - yarr), ord=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): weighting.dist(x, y) else: assert almost_equal(weighting.dist(x, y), true_dist) # Same with free function pdist = odl.cu_weighted_dist(weight, exponent=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): pdist(x, y) else: assert almost_equal(pdist(x, y), true_dist)
def test_custom_norm(fn): [xarr, yarr], [x, y] = example_vectors(fn, 2) norm = np.linalg.norm def other_norm(x): return np.linalg.norm(x, ord=1) w = FnCustomNorm(norm) w_same = FnCustomNorm(norm) w_other = FnCustomNorm(other_norm) assert w == w assert w == w_same assert w != w_other with pytest.raises(NotImplementedError): w.inner(x, y) true_norm = np.linalg.norm(xarr) assert almost_equal(w.norm(x), true_norm) true_dist = np.linalg.norm(xarr - yarr) assert almost_equal(w.dist(x, y), true_dist) with pytest.raises(TypeError): FnCustomNorm(1)
def test_pdist(exponent): for fn in (Rn(3, exponent=exponent), Cn(3, exponent=exponent)): [xarr, yarr], [x, y] = example_vectors(fn, n=2) correct_dist = np.linalg.norm(xarr - yarr, ord=exponent) assert almost_equal(fn.dist(x, y), correct_dist) assert almost_equal(x.dist(y), correct_dist)
def test_conj(fn): xarr, x = example_vectors(fn) xconj = x.conj() assert all_equal(xconj, xarr.conj()) y = x.copy() xconj = x.conj(out=y) assert xconj is y assert all_equal(y, xarr.conj())
def test_array_wrap_method(fn): # Verify that the __array_wrap__ method works. This enables numpy ufuncs # on vectors x_h, x = example_vectors(fn) y_h = np.sin(x_h) y = np.sin(x) assert all_equal(y, y_h) assert y in fn
def test_reductions(fn, reduction): name, _ = reduction ufunc = getattr(np, name) # Create some data x_arr, x = example_vectors(fn, 1) assert almost_equal(ufunc(x_arr), getattr(x.ufunc, name)())
def test_const_inner(): rn = CudaRn(5) [xarr, yarr], [x, y] = example_vectors(rn, 2) constant = 1.5 weighting = CudaFnConstWeighting(constant) true_inner = constant * np.vdot(yarr, xarr) assert almost_equal(weighting.inner(x, y), true_inner)
def test_norm(fn): weighting = np.sqrt(fn_weighting(fn)) xarr, x = example_vectors(fn) correct_norm = np.linalg.norm(xarr) * weighting assert almost_equal(fn.norm(x), correct_norm, places=2) assert almost_equal(x.norm(), correct_norm, places=2)
def test_dist(fn): weighting = np.sqrt(fn_weighting(fn)) [xarr, yarr], [x, y] = example_vectors(fn, 2) correct_dist = np.linalg.norm(xarr - yarr) * weighting assert almost_equal(fn.dist(x, y), correct_dist, places=2) assert almost_equal(x.dist(y), correct_dist, places=2)
def _test_binary_operator(discr, function): # Verify that the statement z=function(x,y) gives equivalent results # to NumPy [x_arr, y_arr], [x, y] = example_vectors(discr, 2) z_arr = function(x_arr, y_arr) z = function(x, y) assert all_almost_equal([x, y, z], [x_arr, y_arr, z_arr])
def _test_unary_operator(spc, function): # Verify that the statement y=function(x) gives equivalent # results to Numpy. x_arr, x = example_vectors(spc) y_arr = function(x_arr) y = function(x) assert all_almost_equal([x, y], [x_arr, y_arr])
def _test_binary_operator(spc, function): # Verify that the statement z=function(x,y) gives equivalent # results to Numpy. [x_arr, y_arr], [x, y] = example_vectors(spc, 2) z_arr = function(x_arr, y_arr) z = function(x, y) assert all_almost_equal([x, y, z], [x_arr, y_arr, z_arr])
def test_inner(fn): weighting = fn_weighting(fn) [xarr, yarr], [x, y] = example_vectors(fn, 2) correct_inner = np.vdot(yarr, xarr) * weighting assert almost_equal(fn.inner(x, y), correct_inner, places=2) assert almost_equal(x.inner(y), correct_inner, places=2)
def test_reduction(impl, reduction): space = odl.uniform_discr([0, 0], [1, 1], [2, 2], impl=impl) name, _ = reduction ufunc = getattr(np, name) # Create some data x_arr, x = example_vectors(space, 1) assert almost_equal(ufunc(x_arr), getattr(x.ufunc, name)())
def _test_lincomb(fn, a, b): # Validates lincomb against the result on host with randomized # data and given a,b # Unaliased arguments [x_arr, y_arr, z_arr], [x, y, z] = example_vectors(fn, 3) z_arr[:] = a * x_arr + b * y_arr fn.lincomb(a, x, b, y, out=z) assert all_almost_equal([x, y, z], [x_arr, y_arr, z_arr]) # First argument aliased with output [x_arr, y_arr, z_arr], [x, y, z] = example_vectors(fn, 3) z_arr[:] = a * z_arr + b * y_arr fn.lincomb(a, z, b, y, out=z) assert all_almost_equal([x, y, z], [x_arr, y_arr, z_arr]) # Second argument aliased with output [x_arr, y_arr, z_arr], [x, y, z] = example_vectors(fn, 3) z_arr[:] = a * x_arr + b * z_arr fn.lincomb(a, x, b, z, out=z) assert all_almost_equal([x, y, z], [x_arr, y_arr, z_arr]) # Both arguments aliased with each other [x_arr, y_arr, z_arr], [x, y, z] = example_vectors(fn, 3) z_arr[:] = a * x_arr + b * x_arr fn.lincomb(a, x, b, x, out=z) assert all_almost_equal([x, y, z], [x_arr, y_arr, z_arr]) # All aliased [x_arr, y_arr, z_arr], [x, y, z] = example_vectors(fn, 3) z_arr[:] = a * z_arr + b * z_arr fn.lincomb(a, z, b, z, out=z) assert all_almost_equal([x, y, z], [x_arr, y_arr, z_arr])
def test_ufuncs(fn, ufunc): name, n_args, n_out, _ = ufunc if (np.issubsctype(fn.dtype, np.floating) or np.issubsctype(fn.dtype, np.complexfloating) and name in [ 'bitwise_and', 'bitwise_or', 'bitwise_xor', 'invert', 'left_shift', 'right_shift' ]): # Skip integer only methods if floating point type return if (np.issubsctype(fn.dtype, np.complexfloating) and name in [ 'remainder', 'trunc', 'signbit', 'invert', 'left_shift', 'right_shift', 'rad2deg', 'deg2rad', 'copysign', 'mod', 'modf', 'fmod', 'logaddexp2', 'logaddexp', 'hypot', 'arctan2', 'floor', 'ceil' ]): # Skip real only methods for complex return # Get the ufunc from numpy as reference npufunc = getattr(np, name) # Create some data arrays, vectors = example_vectors(fn, n_args + n_out) in_arrays = arrays[:n_args] out_arrays = arrays[n_args:] data_vector = vectors[0] in_vectors = vectors[1:n_args] out_vectors = vectors[n_args:] # Out of place: np_result = npufunc(*in_arrays) vec_fun = getattr(data_vector.ufunc, name) odl_result = vec_fun(*in_vectors) assert all_almost_equal(np_result, odl_result) # Test type of output if n_out == 1: assert isinstance(odl_result, fn.element_type) elif n_out > 1: for i in range(n_out): assert isinstance(odl_result[i], fn.element_type) # In place: np_result = npufunc(*(in_arrays + out_arrays)) vec_fun = getattr(data_vector.ufunc, name) odl_result = vec_fun(*(in_vectors + out_vectors)) assert all_almost_equal(np_result, odl_result) # Test inplace actually holds: if n_out == 1: assert odl_result is out_vectors[0] elif n_out > 1: for i in range(n_out): assert odl_result[i] is out_vectors[i]
def _test_lincomb(fn, a, b): # Validates lincomb against the result on host with randomized # data and given a,b # Unaliased arguments [x_arr, y_arr, z_arr], [x, y, z] = example_vectors(fn, 3) z_arr[:] = a * x_arr + b * y_arr z.lincomb(a, x, b, y) order = getattr(z, 'order', None) assert all_almost_equal(z.asarray().ravel(order), z_arr, places=2)
def test_member_multiply(fn): # Validate vector member multiply against the result on host # with randomized data [x_host, y_host], [x_device, y_device] = example_vectors(fn, 2) # Host side calculation y_host *= x_host # Device side calculation y_device *= x_device # Cuda only uses floats, so require 5 places assert all_almost_equal(y_device, y_host)
def test_constant_inner(fn): [xarr, yarr], [x, y] = example_vectors(fn, 2) constant = 1.5 true_result_const = constant * np.vdot(yarr, xarr) w_const = FnConstWeighting(constant) assert almost_equal(w_const.inner(x, y), true_result_const) # Exponent != 2 -> no inner w_const = FnConstWeighting(constant, exponent=1) with pytest.raises(NotImplementedError): w_const.inner(x, y)
def _test_member_lincomb(spc, a): # Validates vector member lincomb against the result on host # Generate vectors [x_host, y_host], [x_device, y_device] = example_vectors(spc, 2) # Host side calculation y_host[:] = a * x_host # Device side calculation y_device.lincomb(a, x_device) # Cuda only uses floats, so require 5 places assert all_almost_equal(y_device, y_host, places=2)
def test_norm(exponent): r3 = CudaRn(3, exponent=exponent) xarr, x = example_vectors(r3) correct_norm = np.linalg.norm(xarr, ord=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): r3.norm(x) x.norm() else: assert almost_equal(r3.norm(x), correct_norm) assert almost_equal(x.norm(), correct_norm)
def test_dist(exponent): r3 = CudaRn(3, exponent=exponent) [xarr, yarr], [x, y] = example_vectors(r3, n=2) correct_dist = np.linalg.norm(xarr - yarr, ord=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): r3.dist(x, y) with pytest.raises(NotImplementedError): x.dist(y) else: assert almost_equal(r3.dist(x, y), correct_dist) assert almost_equal(x.dist(y), correct_dist)
def test_ufuncs(fn, ufunc): name, n_args, n_out, _ = ufunc if (np.issubsctype(fn.dtype, np.floating) and name in ['bitwise_and', 'bitwise_or', 'bitwise_xor', 'invert', 'left_shift', 'right_shift']): # Skip integer only methods if floating point type return # Get the ufunc from numpy as reference ufunc = getattr(np, name) # Create some data arrays, vectors = example_vectors(fn, n_args + n_out) in_arrays = arrays[:n_args] out_arrays = arrays[n_args:] data_vector = vectors[0] in_vectors = vectors[1:n_args] out_vectors = vectors[n_args:] # Out of place: np_result = ufunc(*in_arrays) vec_fun = getattr(data_vector.ufunc, name) odl_result = vec_fun(*in_vectors) assert all_almost_equal(np_result, odl_result) # Test type of output if n_out == 1: assert isinstance(odl_result, fn.element_type) elif n_out > 1: for i in range(n_out): assert isinstance(odl_result[i], fn.element_type) # In place: np_result = ufunc(*(in_arrays + out_arrays)) vec_fun = getattr(data_vector.ufunc, name) odl_result = vec_fun(*(in_vectors + out_vectors)) assert all_almost_equal(np_result, odl_result) # Test inplace actually holds: if n_out == 1: assert odl_result is out_vectors[0] elif n_out > 1: for i in range(n_out): assert odl_result[i] is out_vectors[i]
def test_vector_inner(): rn = CudaRn(5) [xarr, yarr], [x, y] = example_vectors(rn, 2) weight = _pos_vector(CudaRn(5)) weighting = CudaFnVectorWeighting(weight) true_inner = np.vdot(yarr, xarr * weight.asarray()) assert almost_equal(weighting.inner(x, y), true_inner) # Same with free function inner_vec = odl.cu_weighted_inner(weight) assert almost_equal(inner_vec(x, y), true_inner) # Exponent != 2 -> no inner product, should raise with pytest.raises(NotImplementedError): CudaFnVectorWeighting(weight, exponent=1.0).inner(x, y)
def test_ufunc(impl, ufunc): space = odl.uniform_discr([0, 0], [1, 1], (2, 2), impl=impl) name, n_args, n_out, _ = ufunc if (np.issubsctype(space.dtype, np.floating) and name in ['bitwise_and', 'bitwise_or', 'bitwise_xor', 'invert', 'left_shift', 'right_shift']): # Skip integer only methods if floating point type return # Get the ufunc from numpy as reference ufunc = getattr(np, name) # Create some data arrays, vectors = example_vectors(space, n_args + n_out) in_arrays = arrays[:n_args] out_arrays = arrays[n_args:] data_vector = vectors[0] in_vectors = vectors[1:n_args] out_vectors = vectors[n_args:] # Verify type assert isinstance(data_vector.ufunc, odl.util.ufuncs.DiscreteLpUFuncs) # Out of place: np_result = ufunc(*in_arrays) vec_fun = getattr(data_vector.ufunc, name) odl_result = vec_fun(*in_vectors) assert all_almost_equal(np_result, odl_result) # Test type of output if n_out == 1: assert isinstance(odl_result, space.element_type) elif n_out > 1: for i in range(n_out): assert isinstance(odl_result[i], space.element_type) # In place: np_result = ufunc(*(in_arrays + out_arrays)) vec_fun = getattr(data_vector.ufunc, name) odl_result = vec_fun(*(in_vectors + out_vectors)) assert all_almost_equal(np_result, odl_result) # Test inplace actually holds: if n_out == 1: assert odl_result is out_vectors[0] elif n_out > 1: for i in range(n_out): assert odl_result[i] is out_vectors[i] # Test out of place with np data np_result = ufunc(*in_arrays) vec_fun = getattr(data_vector.ufunc, name) odl_result = vec_fun(*in_arrays[1:]) assert all_almost_equal(np_result, odl_result) # Test type of output if n_out == 1: assert isinstance(odl_result, space.element_type) elif n_out > 1: for i in range(n_out): assert isinstance(odl_result[i], space.element_type)