Skip to content

uncomment test from #4297 #4302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions pymc3/distributions/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"vectorized_ppc", default=None
) # type: contextvars.ContextVar[Optional[Callable]]

PLATFORM = sys.platform


class _Unpickling:
pass
Expand Down Expand Up @@ -510,17 +512,17 @@ def __init__(
super().__init__(shape, dtype, testval, *args, **kwargs)
self.logp = logp
if type(self.logp) == types.MethodType:
if sys.platform != "linux":
if PLATFORM != "linux":
warnings.warn(
"You are passing a bound method as logp for DensityDist, this can lead to "
+ "errors when sampling on platforms other than Linux. Consider using a "
+ "plain function instead, or subclass Distribution."
"errors when sampling on platforms other than Linux. Consider using a "
"plain function instead, or subclass Distribution."
)
elif type(multiprocessing.get_context()) != multiprocessing.context.ForkContext:
warnings.warn(
"You are passing a bound method as logp for DensityDist, this can lead to "
+ "errors when sampling when multiprocessing cannot rely on forking. Consider using a "
+ "plain function instead, or subclass Distribution."
"errors when sampling when multiprocessing cannot rely on forking. Consider using a "
"plain function instead, or subclass Distribution."
)
self.rand = random
self.wrap_random_with_dist_shape = wrap_random_with_dist_shape
Expand Down
24 changes: 11 additions & 13 deletions pymc3/tests/test_parallel_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,23 @@ def func(x):
trace = pm.sample(draws=10, tune=10, step=pm.Metropolis(), cores=2, mp_ctx="spawn")


@pytest.mark.xfail(raises=ValueError)
def test_spawn_densitydist_bound_method():
with pm.Model() as model:
mu = pm.Normal("mu", 0, 1)
normal_dist = pm.Normal.dist(mu, 1)
obs = pm.DensityDist("density_dist", normal_dist.logp, observed=np.random.randn(100))
trace = pm.sample(draws=10, tune=10, step=pm.Metropolis(), cores=2, mp_ctx="spawn")
msg = "logp for DensityDist is a bound method, leading to RecursionError while serializing"
with pytest.raises(ValueError, match=msg):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered pytest.raises, but the code in DensityDist is not guaranteed to raise the error. It will try to serialize as normal, and only if a RecursionError occurs (at the moment, expected to happen), it's wrapped and re-raised. It's possible that a later fix in dill or so will prevent the error from happening altogether. So I decided on xfail instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see - thanks for explaining!

It's possible that a later fix in dill or so will prevent the error from happening altogether

If that happens, maybe it's good that this test fails (i.e. doesn't raise a ValueError) because then we will get a loud and clear notice that the error no longer happens and then we can set a minimum dill version?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, sounds good to me :)

trace = pm.sample(draws=10, tune=10, step=pm.Metropolis(), cores=2, mp_ctx="spawn")


# cannot test this properly: monkeypatching sys.platform messes up Theano
# def test_spawn_densitydist_syswarning(monkeypatch):
# monkeypatch.setattr(sys, "platform", "win32")
# with pm.Model() as model:
# mu = pm.Normal('mu', 0, 1)
# normal_dist = pm.Normal.dist(mu, 1)
# with pytest.warns(UserWarning) as w:
# obs = pm.DensityDist('density_dist', normal_dist.logp, observed=np.random.randn(100))
# assert len(w) == 1 and "errors when sampling on platforms" in w[0].message.args[0]
def test_spawn_densitydist_syswarning(monkeypatch):
monkeypatch.setattr("pymc3.distributions.distribution.PLATFORM", "win32")
with pm.Model() as model:
mu = pm.Normal("mu", 0, 1)
normal_dist = pm.Normal.dist(mu, 1)
with pytest.warns(UserWarning, match="errors when sampling on platforms"):
obs = pm.DensityDist("density_dist", normal_dist.logp, observed=np.random.randn(100))


def test_spawn_densitydist_mpctxwarning(monkeypatch):
Expand All @@ -198,6 +197,5 @@ def test_spawn_densitydist_mpctxwarning(monkeypatch):
with pm.Model() as model:
mu = pm.Normal("mu", 0, 1)
normal_dist = pm.Normal.dist(mu, 1)
with pytest.warns(UserWarning) as w:
with pytest.warns(UserWarning, match="errors when sampling when multiprocessing"):
obs = pm.DensityDist("density_dist", normal_dist.logp, observed=np.random.randn(100))
assert len(w) == 1 and "errors when sampling when multiprocessing" in w[0].message.args[0]