pymc-devs / pymc

Bayesian Modeling and Probabilistic Programming in Python
https://docs.pymc.io/
Other
8.47k stars 1.97k forks source link

Runtime error windows. Anaconda #7303

Open OscarFlores-IFi opened 1 month ago

OscarFlores-IFi commented 1 month ago

Describe the issue:

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:

        if __name__ == '__main__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program
    is not going to be frozen to produce an executable.

When running the file, it appears as:

Auto-assigning NUTS sampler... Initializing NUTS using jitter+adapt_diag... Multiprocess sampling (8 chains in 4 jobs) NUTS: [beta, intercept_0] Auto-assigning NUTS sampler... Initializing NUTS using jitter+adapt_diag... Multiprocess sampling (8 chains in 4 jobs) NUTS: [beta, intercept_0]

Immediately after that, it shows the runtime error.

This failure appears when running the code directly from conda (base) or inside a conda environment. I tried running this on a python venv on windows and it also did not work.

When running the same code under a conda environment in Ubuntu24 it worked without any problems.

I have no idea what kind of information is needed to replicate the error. I will try to paste here what I think important.

conda 24.4.0

arviz 0.18.0 mkl 2023.2.0 numpy 1.26.4 pandas 2.2.2 pymc 5.14.0 pytensor 2.20.0

Windows 11 Version 10.0.22631 Build 22631 Processor i7-11800H Installed Physical Memory (RAM) 64.0 GB

Reproduceable code example:

import pymc as pm
import numpy as np

data = np.array([[ True, False, False, False, False, False, False, False, False,
        False, False, False, False, False],
       [False, False, False, False, False, False,  True, False, False,
        False, False, False, False, False],
       [False, False, False, False, False,  True, False, False, False,
        False, False, False,  True, False],
       [False, False, False, False, False, False, False, False, False,
        False, False, False, False, False],
       [False, False, False, False, False, False, False, False, False,
        False, False, False, False, False]])
# Define the logistic regression model
with pm.Model() as model:
    beta = pm.Normal('beta', mu=0, sigma=1, shape=13)  # Coefficients for the other 13 products

    # for i in range(14):
    i = 0
    # Intercept term (optional)
    intercept = pm.Normal(f'intercept_{i}', mu=0, sigma=1)

    # Define likelihood
    p = pm.math.invlogit(intercept + beta.dot(data[:, np.arange(14) != i].T))
    observed = pm.Bernoulli(f'observed_{i}', p=p, observed=data[:, i])

    # Sample from the posterior distribution using MCMC
    trace = pm.sample(draws=2000, tune=1000, chains=8)

# Plot posterior distributions of the coefficients
pm.plot_posterior(trace, var_names=['beta'])

# Compute and print the posterior means and credible intervals
print(pm.summary(trace, var_names=['beta']))

Error message:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\multiprocessing\spawn.py", line 125, in _main
    prepare(preparation_data)
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\multiprocessing\spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\runpy.py", line 289, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\52331\Documents\GitHub\marlitros\minimum_reproducible_error.py", line 29, in <module>
    trace = pm.sample(draws=2000, tune=1000, chains=8)
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\site-packages\pymc\sampling\mcmc.py", line 808, in sample
    _mp_sample(**sample_args, **parallel_args)
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\site-packages\pymc\sampling\mcmc.py", line 1186, in _mp_sample
    sampler = ps.ParallelSampler(
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\site-packages\pymc\sampling\parallel.py", line 405, in __init__
    self._samplers = [
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\site-packages\pymc\sampling\parallel.py", line 406, in <listcomp>
    ProcessAdapter(
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\site-packages\pymc\sampling\parallel.py", line 261, in __init__
    self._process.start()
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\multiprocessing\context.py", line 336, in _Popen
    return Popen(process_obj)
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "C:\Users\52331\anaconda3\envs\pymc_env\lib\multiprocessing\spawn.py", line 134, 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:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

PyMC version information:

conda create -c conda-forge -n pymc_env "pymc>=5" conda activate pymc_env

Context for the issue:

Nothing could run in windows :(

welcome[bot] commented 1 month ago

Welcome Banner] :tada: Welcome to PyMC! :tada: We're really excited to have your input into the project! :sparkling_heart:
If you haven't done so already, please make sure you check out our Contributing Guidelines and Code of Conduct.