File: test_benchmark_coo.py

package info (click to toggle)
python-sparse 0.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,816 kB
  • sloc: python: 11,223; sh: 54; javascript: 10; makefile: 8
file content (173 lines) | stat: -rw-r--r-- 4,445 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import itertools
import operator

import sparse

import pytest

import numpy as np

DENSITY = 0.01


def format_id(format):
    return f"{format=}"


@pytest.mark.parametrize("format", ["coo", "gcxs"])
def test_matmul(benchmark, sides, format, seed, max_size, ids=format_id):
    m, n, p = sides

    if m * n >= max_size or n * p >= max_size:
        pytest.skip()

    rng = np.random.default_rng(seed=seed)
    x = sparse.random((m, n), density=DENSITY, format=format, random_state=rng)
    y = sparse.random((n, p), density=DENSITY, format=format, random_state=rng)

    x @ y  # Numba compilation

    @benchmark
    def bench():
        x @ y


def get_test_id(params):
    side, rank, format = params
    return f"{side=}-{rank=}-{format=}"


@pytest.fixture(params=itertools.product([100, 500, 1000], [1, 2, 3, 4], ["coo", "gcxs"]), ids=get_test_id)
def elemwise_args(request, seed, max_size):
    side, rank, format = request.param
    if side**rank >= max_size:
        pytest.skip()
    rng = np.random.default_rng(seed=seed)
    shape = (side,) * rank
    x = sparse.random(shape, density=DENSITY, format=format, random_state=rng)
    y = sparse.random(shape, density=DENSITY, format=format, random_state=rng)
    return x, y


@pytest.mark.parametrize("f", [operator.add, operator.mul])
def test_elemwise(benchmark, f, elemwise_args):
    x, y = elemwise_args
    f(x, y)

    @benchmark
    def bench():
        f(x, y)


def get_elemwise_ids(params):
    side, format = params
    return f"{side=}-{format=}"


@pytest.fixture(params=itertools.product([100, 500, 1000], ["coo", "gcxs"]), ids=get_elemwise_ids)
def elemwise_broadcast_args(request, seed, max_size):
    side, format = request.param
    if side**2 >= max_size:
        pytest.skip()
    rng = np.random.default_rng(seed=seed)
    x = sparse.random((side, 1, side), density=DENSITY, format=format, random_state=rng)
    y = sparse.random((side, side), density=DENSITY, format=format, random_state=rng)
    return x, y


@pytest.mark.parametrize("f", [operator.add, operator.mul])
def test_elemwise_broadcast(benchmark, f, elemwise_broadcast_args):
    x, y = elemwise_broadcast_args
    f(x, y)

    @benchmark
    def bench():
        f(x, y)


@pytest.fixture(params=itertools.product([100, 500, 1000], [1, 2, 3], ["coo", "gcxs"]), ids=get_test_id)
def indexing_args(request, seed, max_size):
    side, rank, format = request.param
    if side**rank >= max_size:
        pytest.skip()
    rng = np.random.default_rng(seed=seed)
    shape = (side,) * rank

    return sparse.random(shape, density=DENSITY, format=format, random_state=rng)


def test_index_scalar(benchmark, indexing_args):
    x = indexing_args
    side = x.shape[0]
    rank = x.ndim

    x[(side // 2,) * rank]  # Numba compilation

    @benchmark
    def bench():
        x[(side // 2,) * rank]


def test_index_slice(benchmark, indexing_args):
    x = indexing_args
    side = x.shape[0]
    rank = x.ndim

    x[(slice(side // 2),) * rank]  # Numba compilation

    @benchmark
    def bench():
        x[(slice(side // 2),) * rank]


def test_index_fancy(benchmark, indexing_args, seed):
    x = indexing_args
    side = x.shape[0]
    rng = np.random.default_rng(seed=seed)
    index = rng.integers(0, side, size=(side // 2,))

    x[index]  # Numba compilation

    @benchmark
    def bench():
        x[index]


def get_sides_ids(param):
    m, n, p = param
    return f"{m=}-{n=}-{p=}"


@pytest.fixture(params=itertools.product([200, 500, 1000], [200, 500, 1000], [200, 500, 1000]), ids=get_sides_ids)
def sides(request):
    m, n, p = request.param
    return m, n, p


@pytest.fixture(params=([(0, "coo"), (0, "gcxs"), (1, "gcxs")]), ids=["coo", "gcxs-0-axis", "gcxs-1-axis"])
def densemul_args(request, sides, seed, max_size):
    compressed_axis, format = request.param
    m, n, p = sides
    if m * n >= max_size or n * p >= max_size:
        pytest.skip()
    rng = np.random.default_rng(seed=seed)
    if format == "coo":
        x = sparse.random((m, n), density=DENSITY / 10, format=format, random_state=rng)
    else:
        x = sparse.random((m, n), density=DENSITY / 10, format=format, random_state=rng).change_compressed_axes(
            (compressed_axis,)
        )
    t = rng.random((n, p))

    return x, t


def test_gcxs_dot_ndarray(benchmark, densemul_args):
    x, t = densemul_args

    # Numba compilation
    x @ t

    @benchmark
    def bench():
        x @ t