-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
193 lines (123 loc) · 4.73 KB
/
test.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
from concurrent.futures import ThreadPoolExecutor
import core
import line_profiler
import numpy as np
import pytest
import scipy
"""
pytest --benchmark-min-rounds 200 test.py
"""
NUM_THREADS = 8
os.environ["RAYON_NUM_THREADS"] = str(NUM_THREADS)
os.environ["OMP_NUM_THREADS"] = str(NUM_THREADS)
os.environ["MKL_NUM_THREADS"] = str(NUM_THREADS)
os.environ["OPENBLAS_NUM_THREADS"] = str(NUM_THREADS)
def compute_nbinom_pmf_chunk(args):
data_chunk, n, p = args
return scipy.stats.nbinom.logpmf(data_chunk, n, p)
def compute_betabinom_pmf_chunk(args):
data_chunk, n, a, b = args
return scipy.stats.betabinom.logpmf(data_chunk, n, a, b)
def thread_nbinom(k, n, p, num_threads=NUM_THREADS, executor=None):
if executor is None:
executor = ThreadPoolExecutor(max_workers=NUM_THREADS)
k_chunks = np.array_split(k, num_threads)
n_chunks = np.array_split(n, num_threads)
p_chunks = np.array_split(p, num_threads)
args = (xx for xx in zip(k_chunks, n_chunks, p_chunks))
results = executor.map(compute_nbinom_pmf_chunk, args)
return np.concatenate(list(results))
def thread_betabinom(k, n, a, b, num_threads=NUM_THREADS, executor=None):
if executor is None:
executor = ThreadPoolExecutor(max_workers=NUM_THREADS)
k_chunks = np.array_split(k, num_threads)
n_chunks = np.array_split(n, num_threads)
a_chunks = np.array_split(a, num_threads)
b_chunks = np.array_split(b, num_threads)
args = (xx for xx in zip(k_chunks, n_chunks, a_chunks, b_chunks))
results = executor.map(compute_betabinom_pmf_chunk, args)
return np.concatenate(list(results))
def get_mock_data():
NN = 1_000_000
ns = 100 + np.arange(NN)
ps = 0.5 * np.ones_like(ns)
ks = 10 * np.ones_like(ns)
aa = np.ones_like(ns, dtype=float)
bb = np.ones_like(ns, dtype=float)
sci_py = scipy.stats.nbinom.logpmf(ks, ns, ps)
sci_py_bb = scipy.stats.betabinom.logpmf(ks, ns, aa, bb)
return ks, ns, ps, aa, bb, sci_py, sci_py_bb
@pytest.fixture
def mock_data():
return get_mock_data()
def test_sci_py(mock_data, benchmark):
ks, ns, ps, aa, bb, sci_py, sci_py_bb = mock_data
def wrap_sci_py():
return scipy.stats.nbinom.logpmf(ks, ns, ps)
benchmark.group = "nb"
result = benchmark(wrap_sci_py)
assert np.allclose(sci_py, result)
def test_sci_py_bb(mock_data, benchmark):
ks, ns, ps, aa, bb, sci_py, sci_py_bb = mock_data
def wrap_sci_py():
return scipy.stats.betabinom.logpmf(ks, ns, aa, bb)
benchmark.group = "bb"
result = benchmark(wrap_sci_py)
assert np.allclose(sci_py_bb, result)
def test_rust(mock_data, benchmark):
ks, ns, ps, aa, bb, sci_py, sci_py_bb = mock_data
ks = ks.astype(float)
ns = ns.astype(float)
def wrap_rust():
return core.nb(ks, ns, ps)
benchmark.group = "nb"
rust_result = benchmark(wrap_rust)
assert np.allclose(sci_py, rust_result)
def test_rust_bb(mock_data, benchmark):
ks, ns, ps, aa, bb, sci_py, sci_py_bb = mock_data
ks = ks.astype(float)
ns = ns.astype(float)
def wrap_rust():
return core.bb(ks, ns, aa, bb)
benchmark.group = "bb"
rust_result = benchmark(wrap_rust)
assert np.allclose(sci_py_bb, rust_result)
def test_thread(mock_data, benchmark):
ks, ns, ps, aa, bb, sci_py, sci_py_bb = mock_data
executor = ThreadPoolExecutor(max_workers=NUM_THREADS)
def wrap_thread():
return thread_nbinom(ks, ns, ps, executor=executor)
benchmark.group = "nb"
thread_result = benchmark(wrap_thread)
assert np.allclose(sci_py, thread_result)
def test_thread_bb(mock_data, benchmark):
ks, ns, ps, aa, bb, sci_py, sci_py_bb = mock_data
executor = ThreadPoolExecutor(max_workers=NUM_THREADS)
def wrap_thread():
return thread_betabinom(ks, ns, aa, bb, executor=executor)
benchmark.group = "bb"
result = benchmark(wrap_thread)
assert np.allclose(sci_py_bb, result)
@line_profiler.profile
def profile(ks, ns, ps, aa, bb, sci_py, sci_py_bb, iterations=100):
ks = ks.astype(float)
ns = ns.astype(float)
for _ in range(iterations):
sci_py = scipy.stats.nbinom.logpmf(ks, ns, ps)
rust_result = core.nb(ks, ns, ps)
thread_result = thread_nbinom(ks, ns, ps)
sci_py_bb = scipy.stats.betabinom.logpmf(ks, ns, aa, bb)
rust_bb = core.bb(ks, ns, aa, bb)
assert np.allclose(sci_py, thread_result)
assert np.allclose(sci_py, rust_result)
assert np.allclose(sci_py_bb, rust_bb)
print(sci_py[:5])
print(rust_result[:5])
print(sci_py_bb[:5])
print(rust_bb[:5])
print("Profiling complete.")
if __name__ == "__main__":
mock_data = get_mock_data()
ks, ns, ps, aa, bb, sci_py, sci_py_bb = mock_data
profile(*mock_data)