<commit_before>import pytest
import numpy as np
import numpy.testing as npt
import scipy.sparse as spp
from itertools import product

import slo
from slo.backends import available_backends

BACKENDS = available_backends()

@pytest.mark.parametrize("backend,L,M,N,K,density",
    list(product( BACKENDS, [3,4], [5,6], [7,8], [1,8,9,17], [1,0.01,0.1,0.5] ))
)
def test_Realize_Product(backend, L, M, N, K, density):
    b = backend()
    A0_h = slo.util.randM(L, M, density)
    A1_h = slo.util.randM(M, N, density)
    A0 = b.SpMatrix(A0_h, name='A0')
    A1 = b.SpMatrix(A1_h, name='A1')
    A = A0 * A1
    A = A.optimize()

    # forward
    x = b.rand_array((N,K))
    y = b.rand_array((L,K))
    A.eval(y, x)

    x_h = x.to_host()
    y_act = y.to_host()
    y_exp = A0_h @ (A1_h @ x_h)
    npt.assert_allclose(y_act, y_exp, rtol=1e-5)

    # adjoint
    x = b.rand_array((L,K))
    y = b.rand_array((N,K))
    A.H.eval(y, x)

    x_h = x.to_host()
    y_act = y.to_host()
    y_exp = A1_h.H @ (A0_h.H @ x_h)
    npt.assert_allclose(y_act, y_exp, rtol=1e-5)

    # shape
    assert A.shape == (L,N)
    assert A.H.shape == (N,L)

    # dtype
    assert A.dtype == np.dtype('complex64')


@pytest.mark.parametrize("backend,M,N,K,density",
    list(product( BACKENDS, [3,4], [5,6], [7,8], [1,0.01,0.1,0.5] ))
)
def test_StoreMatricesInAdjointOrder(backend, M, N, K, density):
    b = backend()
    A_h = slo.util.randM(M, K, density)
    A = b.SpMatrix(A_h, name='A')

    from slo.transforms import StoreMatricesInAdjointOrder

    AHH = StoreMatricesInAdjointOrder().visit(A)

    x = b.rand_array((K,N))
    y_act = b.zero_array((M,N), dtype=x.dtype)
    y_exp = b.zero_array((M,N), dtype=x.dtype)

    A.eval(y_exp, x)
    AHH.eval(y_act, x)

    npt.assert_allclose( y_exp.to_host(), y_act.to_host(), rtol=1e-4 )<commit_msg>bugfix: update name in tests<commit_after>import pytest
import numpy as np
import numpy.testing as npt
import scipy.sparse as spp
from itertools import product

import slo
from slo.backends import available_backends

BACKENDS = available_backends()

@pytest.mark.parametrize("backend,L,M,N,K,density",
    list(product( BACKENDS, [3,4], [5,6], [7,8], [1,8,9,17], [1,0.01,0.1,0.5] ))
)
def test_Realize_Product(backend, L, M, N, K, density):
    b = backend()
    A0_h = slo.util.randM(L, M, density)
    A1_h = slo.util.randM(M, N, density)
    A0 = b.SpMatrix(A0_h, name='A0')
    A1 = b.SpMatrix(A1_h, name='A1')
    A = A0 * A1
    A = A.optimize()

    # forward
    x = b.rand_array((N,K))
    y = b.rand_array((L,K))
    A.eval(y, x)

    x_h = x.to_host()
    y_act = y.to_host()
    y_exp = A0_h @ (A1_h @ x_h)
    npt.assert_allclose(y_act, y_exp, rtol=1e-5)

    # adjoint
    x = b.rand_array((L,K))
    y = b.rand_array((N,K))
    A.H.eval(y, x)

    x_h = x.to_host()
    y_act = y.to_host()
    y_exp = A1_h.H @ (A0_h.H @ x_h)
    npt.assert_allclose(y_act, y_exp, rtol=1e-5)

    # shape
    assert A.shape == (L,N)
    assert A.H.shape == (N,L)

    # dtype
    assert A.dtype == np.dtype('complex64')


@pytest.mark.parametrize("backend,M,N,K,density",
    list(product( BACKENDS, [3,4], [5,6], [7,8], [1,0.01,0.1,0.5] ))
)
def test_StoreMatricesInBestOrder(backend, M, N, K, density):
    b = backend()
    A_h = slo.util.randM(M, K, density)
    A = b.SpMatrix(A_h, name='A')

    from slo.transforms import StoreMatricesInBestOrder

    AHH = StoreMatricesInBestOrder().visit(A)

    x = b.rand_array((K,N))
    y_act = b.zero_array((M,N), dtype=x.dtype)
    y_exp = b.zero_array((M,N), dtype=x.dtype)

    A.eval(y_exp, x)
    AHH.eval(y_act, x)

    npt.assert_allclose( y_exp.to_host(), y_act.to_host(), rtol=1e-4 )
{'fraction_non_alphanumeric': 0.17520435967302453, 'fraction_numerical': 0.02997275204359673, 'mean_word_length': 3.2243958573072495, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7354416', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1685, 'n_words': 310}