New User - NUTS Running forever on regression tutorial

I have just installed PyMC on MacOS, PyMC v5.20.1, as recommended in the section, i na newly minted conda environment.
Trying to run the example on the same page, “Motivating Example: Linear Regression”.
When sampling, I get to

Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [alpha, beta, sigma]

and then it gest stuck for minutes (>15, then I break).

Reading around this forum, I tried to set chains =1 in the last line of the snippet

with basic_model:
    # draw 1000 posterior samples
    idata = pm.sample()

and it solves in 2 seconds. Same happens for other tutorials, only difference that sometimes get stuck at the Initializing NUTS using jitter+adapt_diag... stage.

Same issue occurs when using the slice sampling algorithm

with basic_model:
    # instantiate sampler
    step = pm.Slice()

    # draw 5000 posterior samples
    slice_idata = pm.sample(5000, step=step)

runs forever with 4 chains, istantaneously with 1. Sure my machine has 4 computing cores, actually I have 8!
Sorry if I am missing anything obvious, thank you very much for your support.

Are you able to run a prior predictive check using pm.sample_prior_predictive()?

No it does not seem to be working either, I issued

pm.sample_prior_predictive(model = basic_model)

and it has been running for a minute!
If I try

pm.sample_prior_predictive(draws = 1, model = basic_model)

it computed though, although when I tried again it ran for more than 30 secs before I interrupted.
However absurd this might sound, it seems after interruption, if I re-run it computes immdiately!

Does this sample for you?

RANDOM_SEED = 101
rng = np.random.default_rng(RANDOM_SEED)

y = pm.draw(pm.Normal.dist(mu=2, sigma=1), draws=1000)

with pm.Model() as model:
    mu = pm.Normal("mu", mu=0, sigma=3)
    sigma = pm.HalfNormal("sigma", sigma=1)
    y_obs = pm.Normal("yobs", mu=mu, sigma=sigma, observed=y)
    idata = pm.sample(chains=4, draws=2000, random_seed=RANDOM_SEED)

No, same issues. It runs with chains=1, but goes on forever with chains=4.

I forgot to mention I have M1 Silicon chip, just in case it means anything (seems to be an issue for Tensorflow, for example).

I tried to re-install alas to no better outcome, this is very frustrating. As I mentioned, the installation got me PyMC v5.20.1 as well as PyTensor ‘2.27.1’.

Reading around the forum I gathered it was worth trying to run as a script, and not in a Jupyter Notebook.
If I run it as a script, with the proper boilerplate

if __name__ == "__main__":

it runs!

Without the "if name == “main” stuff, the traceback complains about

###########/pymc_env_5/lib/python3.12/multiprocessing/spawn.py", line 140, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

ok I clearly do not know much about multiprocessing…

The question is then, is there a way to get this to work on a Jupyter Notebook? Thank you

Two things to try in a notebook:

  1. Specify the multiprocessing context to use. This came up in this thread . Check the docs for pm.sample for all the mp_ctx options.
idata = pm.sample(chains=4, draws=2000, random_seed=RANDOM_SEED, mp_ctx='spawn')
  1. Compile to numba instead of C, to rule out annoying BLAS things. It shouldn’t be an issue in this model (no matrix multiplications), but I’m constantly surprised at how wrong my assumptions can be:
idata = pm.sample(chains=4, draws=2000, random_seed=RANDOM_SEED, compile_kwargs={'mode':'NUMBA'})

Thanks for this. Regretfully, no success at all. The first option does not change the observed behaviour, the second throws (when running as script) a bunch of Numba-related errors I am trying to decipher.
Will have to use one chain only for setting up things, and the. run as script, until I figure out what could be. Thanks a lot

I have a Mac with M2 Pro and OS Sequoia 15.0.1. have not had any issues in the past but your post got me curious. I tried creating a new environment and ran in to the same issues. Oddly enough, the issue is only in VS Code for me (I did not try other notebook editors) but not when running directly in the terminal. I noticed this error in my terminal:

UnsatisfiableError: The following specifications were found to be incompatible with each other:

Output in format: Requested package -> Available versionsThe following specifications were found to be incompatible with your system:

  - feature:/osx-64::__osx==10.16=0
  - feature:|@/osx-64::__osx==10.16=0
  - pymc==5.20.1 -> pytensor -> __osx[version='>=10.9']

Your installed version is: 10.16

I did some digging and it may be related to this issue. Not sure which OS version you are on but it’s worth a look. I don’t run any of my pymc models locally anymore so I did not try to repair the process per the suggestions. Hope this help!

That is excellent thank you for such great help. It is indeed VS code related, it works well e.g. in Jupyter Lab.

Thanks again much appreciated