Description
Hi,
I am running the piece of code shown below on MacOS 10.15.6 with Python 3.6.11 and the latest version of the master branch in the pymc3-dev.
It is a toy example of using a Theano Op to compute a forward model and then use the output as the mean of a multivariate Normal.
I get the error shown under the code. When I sample with cores=1
in sample()
the error disappears and sampling happens normally.
I played a bit with it and when I rebased the code, removing the commits from PR #3991, the error stops happening. Not sure which part of the changes breaks this and it might happen in MacOS only (a collaborator who works in Linux does not have the same issue).
This might be connected to #4053 #3844 or #3140 but it was not clear it is the same issue so decided to post separately.
Code:
import numpy as np
import theano.tensor as tt
import pymc3 as pm
if __name__ == '__main__':
size = 50
true_intercept = 1
true_slope = 2
sigma = 1
x = np.linspace(0, 1, size)
true_regression_line = true_intercept + true_slope * x
y = true_regression_line + np.random.normal(0, sigma ** 2, size)
s = np.identity(y.shape[0])
np.fill_diagonal(s, sigma ** 2)
# MCMC parameters
ndraws = 300
ntune = 100
nsub = 5
nchains = 4
seed = 98765
class ForwardModel1(tt.Op):
itypes = [tt.dvector]
otypes = [tt.dvector]
def __init__(self, x):
self.x = x
def perform(self, node, inputs, outputs):
intercept = inputs[0][0]
x_coeff = inputs[0][1]
temp = intercept + x_coeff * self.x
outputs[0][0] = temp
with pm.Model():
Sigma_e = pm.Data('Sigma_e', s)
intercept = pm.Normal('Intercept', 0, sigma=20)
x_coeff = pm.Normal('x', 0, sigma=20)
theta = tt.as_tensor_variable([intercept, x_coeff])
f = ForwardModel1(x)
likelihood = pm.MvNormal('y', mu=f(theta), cov=Sigma_e, observed=y)
trace = pm.sample(draws=ndraws, step=pm.Metropolis(),
chains=nchains, tune=ntune,
discard_tuned_samples=True,
random_seed=seed)
Error/traceback:
Traceback (most recent call last):
File "/Users/gmingas/projects/pymc3/pymc3/parallel_sampling.py", line 114, in _unpickle_step_method
self._step_method = pickle.loads(self._step_method)
AttributeError: Can't get attribute 'ForwardModel1' on <module '__mp_main__' from '/Users/gmingas/projects/pymc3/pymc3/examples/test.py'>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/gmingas/projects/pymc3/pymc3/parallel_sampling.py", line 135, in run
self._unpickle_step_method()
File "/Users/gmingas/projects/pymc3/pymc3/parallel_sampling.py", line 116, in _unpickle_step_method
raise ValueError(unpickle_error)
ValueError: The model could not be unpickled. This is required for sampling with more than one core and multiprocessing context spawn or forkserver.
"""
The above exception was the direct cause of the following exception:
ValueError: The model could not be unpickled. This is required for sampling with more than one core and multiprocessing context spawn or forkserver.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test.py", line 64, in <module>
random_seed=seed)
File "/Users/gmingas/projects/pymc3/pymc3/sampling.py", line 540, in sample
trace = _mp_sample(**sample_args, **parallel_args)
File "/Users/gmingas/projects/pymc3/pymc3/sampling.py", line 1461, in _mp_sample
for draw in sampler:
File "/Users/gmingas/projects/pymc3/pymc3/parallel_sampling.py", line 492, in __iter__
draw = ProcessAdapter.recv_draw(self._active)
File "/Users/gmingas/projects/pymc3/pymc3/parallel_sampling.py", line 365, in recv_draw
raise error from old_error
RuntimeError: Chain 0 failed.