def test_equality(): assert Tensor.from_lol([0, 2, 0], format='d') == Tensor.from_lol([0, 2, 0], format='s') assert Tensor.from_lol([0, 1, 0], format='d') != Tensor.from_lol( [0, 2, 0], format='s') assert Tensor.from_lol([0, 1, 0], format='d') != 1
def test_matrix_multiply(a, b, c): a = Tensor.from_lol(a) b = Tensor.from_lol(b) if isinstance(c, list): c = Tensor.from_lol(c) actual = a @ b assert actual == c
def test_subtract(a, b, c): if isinstance(a, list): a = Tensor.from_lol(a) if isinstance(b, list): b = Tensor.from_lol(b) expected = Tensor.from_lol(c) actual = a - b assert actual == expected
def test_multiply(a, b, c): if isinstance(a, list): a = Tensor.from_lol(a) if isinstance(b, list): b = Tensor.from_lol(b) expected = Tensor.from_lol(c) actual = a * b assert actual == expected
def test_binary_mismatched_dimensions(): a = Tensor.from_lol([3, 2, 5]) b = Tensor.from_lol([1, 2, 0, 4]) with pytest.raises(ValueError): _ = a + b with pytest.raises(ValueError): _ = a - b with pytest.raises(ValueError): _ = a * b
def assert_same_as_dense(expression, format_out, **tensor_pairs): tensors_in_format = { name: Tensor.from_lol(data, format=format) for name, (data, format) in tensor_pairs.items() } tensors_as_dense = { name: Tensor.from_lol(data) for name, (data, _) in tensor_pairs.items() } actual = evaluate(expression, format_out, **tensors_in_format) expected = evaluate(expression, ''.join('d' for c in format_out if c in ('d', 's')), **tensors_as_dense) assert actual == expected
def test_from_dense_lil_scalar(): format = Format((), ()) x = Tensor.from_lol(2.0, dimensions=(), format=format) assert x.order == 0 assert x.dimensions == () assert x.modes == () assert x.mode_ordering == () assert x.format == format assert x.to_dok() == {(): 2.0}
def test_matrix_multiply_too_many_dimensions(): a = Tensor.from_lol([3, 2, 5]) b = Tensor.from_dok( { (0, 0, 0): 4.5, (1, 0, 1): 3.2, (1, 1, 2): -3.0, (0, 1, 1): 5.0, }, dimensions=(3, 3, 3)) with pytest.raises(ValueError): _ = a @ b
def test_from_dense_lil(): format = Format((Mode.dense, Mode.dense), (0, 1)) x = Tensor.from_lol( [[0, -4.0, 4.5], [0, -3.5, 2.5]], dimensions=(2, 3), format=format, ) assert x.order == 2 assert x.dimensions == (2, 3) assert x.modes == (Mode.dense, Mode.dense) assert x.mode_ordering == (0, 1) assert x.format == format assert x.to_dok() == { (0, 1): -4.0, (0, 2): 4.5, (1, 1): -3.5, (1, 2): 2.5, }
def test_copy_2(dense, format_in, format_out): a = Tensor.from_lol(dense, format=format_in) actual = evaluate('b(i,j) = a(i,j)', format_out, a=a) assert actual == a
def test_figure_2a(format, indices, vals): data = [5, 1, 0, 0, 2, 0, 8, 0] a = Tensor.from_lol(data, dimensions=(8, ), format=format) assert a.taco_indices == indices assert a.taco_vals == vals
def test_str_repr(): # Just make sure these run a = Tensor.from_lol([0, 2, 1, 0]) str(a) repr(a)
def test_matrix_multiply_mismatched_dimensions(): a = Tensor.from_lol([3, 2, 5]) b = Tensor.from_lol([1, 2, 0, 4]) with pytest.raises(ValueError): _ = a @ b
def test_nonscalar_to_float(): x = Tensor.from_lol([1, 2]) with pytest.raises(ValueError): _ = float(x)
def test_figure_5(format, indices, vals): data = [[6, 0, 9, 8], [0, 0, 0, 0], [5, 0, 0, 7]] A = Tensor.from_lol(data, dimensions=(3, 4), format=format) assert A.taco_indices == indices assert A.taco_vals == vals
return tensor; } """ ffi = FFI() ffi.include(tensor_cdefs) ffi.cdef(""" taco_tensor_t create_tensor(); taco_tensor_t* create_pointer_to_tensor(); """) ffi.set_source( 'taco_kernel', taco_define_header + taco_type_header + source, extra_compile_args=['-Wno-unused-variable', '-Wno-unknown-pragmas']) expected_tensor = Tensor.from_lol([[6, 0, 9, 8], [0, 0, 0, 0], [5, 0, 0, 7]], format='ds') with tempfile.TemporaryDirectory() as temp_dir: # Lock because FFI.compile is not thread safe: https://foss.heptapod.net/pypy/cffi/-/issues/490 with lock: # Create shared object in temporary directory lib_path = ffi.compile(tmpdir=temp_dir) # Load the shared object lib = ffi.dlopen(lib_path) def test_take_ownership_of_tensor_on_returned_struct(): cffi_tensor = lib.create_tensor() take_ownership_of_tensor_members(cffi_tensor) tensor = Tensor(cffi_tensor)