Skip to content

Parallel sampling hangs on macOS with Python 3.8 with pickling error #3844

Closed
@dfm

Description

@dfm

Description of your problem

Starting with Python 3.8 the default start method for multiprocessing on macOS is spawn instead of fork - more info here and here. This means that some features that used to work will not continue to work on macOS. I specifically ran into this when using a DensityDist where the function was defined in place as follows because the function is not picklable:

import pymc3 as pm

with pm.Model() as model:
    
    def val(x):
        return -0.5 * pm.math.sum(x ** 2)
    
    x = pm.Flat("x")
    pm.DensityDist("obs", val)
    
    pm.sample(chains=2, cores=2)

This fails with the following error that does not actually get caught. I was running in a Jupyter notebook and the notebook just hangs and this gets written to the command line:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/dforeman/research/projects/exoplanet-dev/case-studies/env/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "/Users/dforeman/research/projects/exoplanet-dev/case-studies/env/lib/python3.8/multiprocessing/spawn.py", line 126, in _main
    self = reduction.pickle.load(from_parent)
AttributeError: Can't get attribute 'val' on <module '__main__' (built-in)>

A temporary workaround that I found was to execute

import multiprocessing as mp
mp.set_start_method("fork")

before importing PyMC3, but my understanding is that this won't be a long term solution. It doesn't seem like there's anything obvious to do about this, but I wanted to bring it up here in case folks had clever ideas!

Versions and main components

  • PyMC3 Version: 3.8
  • Theano Version: 1.0.4
  • Python Version: 3.8.1
  • Operating system: macOS
  • How did you install PyMC3: pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions