forked from pymc-devs/pymc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_parallel.py
250 lines (205 loc) · 7.39 KB
/
test_parallel.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# Copyright 2024 The PyMC Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import multiprocessing
import os
import platform
import sys
import warnings
import cloudpickle
import numpy as np
import pytensor
import pytensor.tensor as pt
import pytest
from pytensor.compile.ops import as_op
from pytensor.tensor.type import TensorType
import pymc as pm
import pymc.sampling.parallel as ps
from pymc.pytensorf import floatX
def test_context():
with pm.Model():
pm.Normal("x")
ctx = multiprocessing.get_context("spawn")
with warnings.catch_warnings():
warnings.filterwarnings("ignore", ".*number of samples.*", UserWarning)
pm.sample(tune=2, draws=2, chains=2, cores=2, mp_ctx=ctx)
class NoUnpickle:
def __getstate__(self):
return self.__dict__.copy()
def __setstate__(self, state):
raise AttributeError("This fails")
def test_bad_unpickle():
with pm.Model() as model:
pm.Normal("x")
with model:
step = pm.NUTS()
step.no_unpickle = NoUnpickle()
with pytest.raises(Exception) as exc_info:
pm.sample(
tune=2,
draws=2,
mp_ctx="spawn",
step=step,
cores=2,
chains=2,
compute_convergence_checks=False,
)
assert "could not be unpickled" in str(exc_info.getrepr(style="short"))
at_vector = TensorType(pytensor.config.floatX, [False])
@as_op([at_vector, pt.iscalar], [at_vector])
def _crash_remote_process(a, master_pid):
if os.getpid() != master_pid:
sys.exit(0)
return 2 * np.array(a)
def test_remote_pipe_closed():
master_pid = os.getpid()
with pm.Model():
x = pm.Normal("x", shape=2, mu=0.1)
at_pid = pt.as_tensor_variable(np.array(master_pid, dtype="int32"))
pm.Normal("y", mu=_crash_remote_process(x, at_pid), shape=2)
step = pm.Metropolis()
with pytest.raises(ps.ParallelSamplingError, match="Chain [0-9] failed with") as ex:
pm.sample(step=step, mp_ctx="spawn", tune=2, draws=2, cores=2, chains=2)
@pytest.mark.skip(reason="Unclear")
@pytest.mark.parametrize("mp_start_method", ["spawn", "fork"])
def test_abort(mp_start_method):
with pm.Model() as model:
a = pm.Normal("a", shape=1)
b = pm.HalfNormal("b")
step1 = pm.NUTS([model.rvs_to_values[a]])
step2 = pm.Metropolis([model.rvs_to_values[b]])
step = pm.CompoundStep([step1, step2])
# on Windows we cannot fork
if platform.system() == "Windows" and mp_start_method == "fork":
return
if mp_start_method == "spawn":
step_method_pickled = cloudpickle.dumps(step, protocol=-1)
else:
step_method_pickled = None
for abort in [False, True]:
ctx = multiprocessing.get_context(mp_start_method)
proc = ps.ProcessAdapter(
10,
10,
step,
chain=3,
seed=1,
mp_ctx=ctx,
start={"a": floatX(np.array([1.0])), "b_log__": floatX(np.array(2.0))},
step_method_pickled=step_method_pickled,
)
proc.start()
while True:
proc.write_next()
out = ps.ProcessAdapter.recv_draw([proc])
if out[1]:
break
if abort:
proc.abort()
proc.join()
@pytest.mark.parametrize("mp_start_method", ["spawn", "fork"])
def test_explicit_sample(mp_start_method):
with pm.Model() as model:
a = pm.Normal("a", shape=1)
b = pm.HalfNormal("b")
step1 = pm.NUTS([model.rvs_to_values[a]])
step2 = pm.Metropolis([model.rvs_to_values[b]])
step = pm.CompoundStep([step1, step2])
# on Windows we cannot fork
if platform.system() == "Windows" and mp_start_method == "fork":
return
if mp_start_method == "spawn":
step_method_pickled = cloudpickle.dumps(step, protocol=-1)
else:
step_method_pickled = None
ctx = multiprocessing.get_context(mp_start_method)
proc = ps.ProcessAdapter(
10,
10,
step,
chain=3,
rng=np.random.default_rng(1),
mp_ctx=ctx,
start={"a": floatX(np.array([1.0])), "b_log__": floatX(np.array(2.0))},
step_method_pickled=step_method_pickled,
blas_cores=None,
)
proc.start()
while True:
proc.write_next()
out = ps.ProcessAdapter.recv_draw([proc])
view = proc.shared_point_view
for name in view:
view[name].copy()
if out[1]:
break
proc.join()
def test_iterator():
with pm.Model() as model:
a = pm.Normal("a", shape=1)
b = pm.HalfNormal("b")
step1 = pm.NUTS([model.rvs_to_values[a]])
step2 = pm.Metropolis([model.rvs_to_values[b]])
step = pm.CompoundStep([step1, step2])
start = {"a": floatX(np.array([1.0])), "b_log__": floatX(np.array(2.0))}
sampler = ps.ParallelSampler(
draws=10,
tune=10,
chains=3,
cores=2,
rngs=np.random.default_rng(1).spawn(3),
start_points=[start] * 3,
step_method=step,
progressbar=False,
blas_cores=None,
)
with sampler:
for draw in sampler:
pass
def test_spawn_densitydist_function():
with pm.Model() as model:
mu = pm.Normal("mu", 0, 1)
def func(x):
return -2 * (x**2).sum()
obs = pm.CustomDist("density_dist", logp=func, observed=np.random.randn(100))
with warnings.catch_warnings():
warnings.filterwarnings("ignore", ".*number of samples.*", UserWarning)
pm.sample(draws=10, tune=10, step=pm.Metropolis(), cores=2, mp_ctx="spawn")
def test_spawn_densitydist_bound_method():
N = 100
with pm.Model() as model:
mu = pm.Normal("mu", 0, 1)
def logp(x, mu):
normal_dist = pm.Normal.dist(mu, 1, size=N)
out = pm.logp(normal_dist, x)
return out
obs = pm.CustomDist("density_dist", mu, logp=logp, observed=np.random.randn(N), size=N)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", ".*number of samples.*", UserWarning)
pm.sample(draws=10, tune=10, step=pm.Metropolis(), cores=2, mp_ctx="spawn")
@pytest.mark.parametrize("cores", (1, 2))
def test_sampling_with_random_generator_matches(cores):
# Regression test for https://github.com/pymc-devs/pymc/issues/7612
kwargs = {
"chains": 2,
"cores": cores,
"tune": 10,
"draws": 10,
"compute_convergence_checks": False,
"progress_bar": False,
}
with pm.Model() as m:
x = pm.Normal("x")
post1 = pm.sample(random_seed=np.random.default_rng(42), **kwargs).posterior
post2 = pm.sample(random_seed=np.random.default_rng(42), **kwargs).posterior
assert post1.equals(post2), (post1["x"].mean().item(), post2["x"].mean().item())