Пример #1
0
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
Пример #2
0
def test_unsorted_coordinates(format, indices, vals, permutation):
    data = [
        ((0, 0), 6),
        ((0, 2), 9),
        ((0, 3), 8),
        ((2, 0), 5),
        ((2, 3), 7),
    ]

    permutated_data = [data[i] for i in permutation]
    coordinates, values = zip(*permutated_data)

    A = Tensor.from_aos(coordinates, values, dimensions=(3, 4), format=format)

    assert A.taco_indices == indices
    assert A.taco_vals == vals

    A = Tensor.from_soa(zip(*coordinates),
                        values,
                        dimensions=(3, 4),
                        format=format)

    assert A.taco_indices == indices
    assert A.taco_vals == vals

    A = Tensor.from_dok(dict(permutated_data),
                        dimensions=(3, 4),
                        format=format)

    assert A.taco_indices == indices
    assert A.taco_vals == vals
Пример #3
0
def test_multithread_evaluation():
    # As of version 1.14.4 of cffi, the FFI.compile method is not thread safe. This tests that evaluation of different
    # kernels is thread safe.
    A = Tensor.from_aos([[1, 0], [0, 1], [1, 2]], [2.0, -2.0, 4.0],
                        dimensions=(2, 3),
                        format='ds')

    x = Tensor.from_aos([[0], [1], [2]], [3.0, 2.5, 2.0],
                        dimensions=(3, ),
                        format='d')

    def run_eval():
        # Generate a random expression so that the cache cannot be hit
        return evaluate(f'y{randrange(1024)}(i) = A(i,j) * x(j)',
                        'd',
                        A=A,
                        x=x)

    n = 4
    with ThreadPool(n) as p:
        results = p.starmap(run_eval, [()] * n)

    expected = run_eval()

    for actual in results:
        assert actual == expected
Пример #4
0
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
Пример #5
0
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
Пример #6
0
def test_to_from_numpy(array, format):
    numpy = pytest.importorskip('numpy')

    expected = numpy.array(array)

    tensor = Tensor.from_numpy(expected, format=format * expected.ndim)
    actual = Tensor.to_numpy(tensor)

    assert numpy.array_equal(actual, expected)
Пример #7
0
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
Пример #8
0
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
Пример #9
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
Пример #10
0
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
Пример #11
0
def test_from_scalar():
    x = Tensor.from_scalar(-2.0)

    assert x.order == 0
    assert x.dimensions == ()
    assert x.modes == ()
    assert x.mode_ordering == ()
    assert x.format == Format((), ())
    assert x.to_dok() == {(): -2.0}
Пример #12
0
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}
Пример #13
0
def test_to_dok():
    dok = {
        (2, 3): 2.0,
        (0, 1): 0.0,
        (1, 2): -1.0,
        (0, 3): 0.0,
    }
    dok_no_zeros = {key: value for key, value in dok.items() if value != 0}
    x = Tensor.from_dok(dok)

    assert x.to_dok() == dok_no_zeros
    assert x.to_dok(explicit_zeros=True) == dok
Пример #14
0
def test_csr_matrix_vector_product():
    A = Tensor.from_aos([[1, 0], [0, 1], [1, 2]], [2.0, -2.0, 4.0],
                        dimensions=(2, 3),
                        format='ds')

    x = Tensor.from_aos([[0], [1], [2]], [3.0, 2.5, 2.0],
                        dimensions=(3, ),
                        format='d')

    expected = Tensor.from_aos([[0], [1]], [-5.0, 14.0],
                               dimensions=(2, ),
                               format='d')

    function = tensor_method('y(i) = A(i,j) * x(j)', dict(A='ds', x='d'), 'd')

    actual = function(A, x)

    assert actual == expected

    actual = evaluate('y(i) = A(i,j) * x(j)', 'd', A=A, x=x)

    assert actual == expected
Пример #15
0
def test_figure_2e(format, indices, vals):
    data = {
        (0, 0): 5,
        (0, 1): 1,
        (1, 0): 7,
        (1, 1): 3,
        (3, 0): 8,
        (3, 3): 4,
        (3, 4): 9,
    }
    a = Tensor.from_dok(data, dimensions=(4, 6), format=format)

    assert a.taco_indices == indices
    assert a.taco_vals == vals
Пример #16
0
def test_figure_2m(format, indices, vals):
    data = {
        (0, 0, 0): 1,
        (0, 0, 1): 7,
        (0, 2, 1): 5,
        (2, 0, 1): 2,
        (2, 2, 0): 4,
        (2, 2, 1): 8,
        (2, 3, 0): 3,
        (2, 3, 1): 9,
    }
    a = Tensor.from_dok(data, dimensions=(3, 4, 2), format=format)

    assert a.taco_indices == indices
    assert a.taco_vals == vals
Пример #17
0
def test_csr_matrix_plus_csr_matrix():
    A = Tensor.from_aos([[1, 0], [0, 1], [1, 2]], [2.0, -2.0, 4.0],
                        dimensions=(2, 3),
                        format='ds')

    B = Tensor.from_aos([[1, 1], [1, 2], [0, 2]], [-3.0, 4.0, 3.5],
                        dimensions=(2, 3),
                        format='ds')

    expected = Tensor.from_aos([[1, 0], [0, 1], [1, 2], [1, 1], [0, 2]],
                               [2.0, -2.0, 8.0, -3.0, 3.5],
                               dimensions=(2, 3),
                               format='ds')

    function = tensor_method('C(i,j) = A(i,j) + B(i,j)', dict(A='ds', B='ds'),
                             'ds')

    actual = function(A, B)

    assert actual == expected

    actual = evaluate('C(i,j) = A(i,j) * B(i,j)', 'ds', A=A, B=B)

    assert actual == expected
Пример #18
0
def test_from_dok():
    data = {
        (2, 2): 2.0,
        (0, 2): -3.0,
        (1, 0): 2.0,
        (2, 3): 5.0,
    }
    format = Format((Mode.compressed, Mode.compressed), (0, 1))
    x = Tensor.from_dok(data, dimensions=(4, 5), format='ss')

    assert x.order == 2
    assert x.dimensions == (4, 5)
    assert x.modes == (Mode.compressed, Mode.compressed)
    assert x.mode_ordering == (0, 1)
    assert x.format == format
    assert x.to_dok() == data
Пример #19
0
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,
    }
Пример #20
0
def test_from_aos():
    format = Format((Mode.dense, Mode.compressed), (1, 0))
    x = Tensor.from_aos(
        [(1, 0), (1, 1), (2, 1), (3, 1)],
        [4.5, 3.2, -3.0, 5.0],
        dimensions=(4, 3),
        format=format,
    )

    assert x.order == 2
    assert x.dimensions == (4, 3)
    assert x.modes == (Mode.dense, Mode.compressed)
    assert x.mode_ordering == (1, 0)
    assert x.format == format
    assert x.to_dok() == {
        (1, 0): 4.5,
        (1, 1): 3.2,
        (2, 1): -3.0,
        (3, 1): 5.0,
    }
Пример #21
0
def test_from_soa():
    format = Format((Mode.dense, Mode.compressed, Mode.compressed), (0, 1, 2))
    x = Tensor.from_soa(
        ([0, 1, 1, 0], [0, 0, 1, 1], [0, 1, 2, 1]),
        [4.5, 3.2, -3.0, 5.0],
        dimensions=(2, 3, 3),
        format=format,
    )

    assert x.order == 3
    assert x.dimensions == (2, 3, 3)
    assert x.modes == (Mode.dense, Mode.compressed, Mode.compressed)
    assert x.mode_ordering == (0, 1, 2)
    assert x.format == format
    assert x.to_dok() == {
        (0, 0, 0): 4.5,
        (1, 0, 1): 3.2,
        (1, 1, 2): -3.0,
        (0, 1, 1): 5.0,
    }
Пример #22
0
def test_to_float():
    x = Tensor.from_scalar(2.0)
    assert float(x) == 2.0
Пример #23
0
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
Пример #24
0
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)
    assert tensor == expected_tensor
Пример #25
0
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
Пример #26
0
def test_str_repr():
    # Just make sure these run
    a = Tensor.from_lol([0, 2, 1, 0])
    str(a)
    repr(a)
Пример #27
0
    with pytest.raises(ValueError):
        _ = a @ b


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


@pytest.mark.parametrize('tensor', [
    Tensor.from_dok({}, dimensions=()),
    Tensor.from_dok({
        (2, 3): 2.0,
        (0, 1): 0.0,
        (1, 2): -1.0,
        (0, 3): 0.0
    },
                    dimensions=(2, 4)),
    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)),
Пример #28
0
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
Пример #29
0
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
Пример #30
0
def test_nonscalar_to_float():
    x = Tensor.from_lol([1, 2])
    with pytest.raises(ValueError):
        _ = float(x)