Compiler getting stuck when chain number is larger than 1


I am having issues when compiling models with PyStan on Windows. I am using Spyder as IDE. When I run the command “stan_model.sampling” with chain number more than one, it gets stuck after printing “COMPILING THE C++ CODE FOR MODEL”. I can not even interrupt so I have to restart Spyder. The command is working only when chain is 1, which seems to be a multi tread problem I could not solve.

  • Operating System: Windows 10
  • Python Version: 3.7
  • PyStan Version: 2.19
  • Compiler/Toolkit: mingw-w64

That seems odd. If you’ve got a StanModel object, compiling should have been done in a separate sted before sampling even began. What does your Python code look like?

Even the eight school example provided in the docs does not work when the chain is 3.

schools_code = “”"
data {
int<lower=0> J;
real y[J];
real<lower=0> sigma[J];

parameters {
real mu;
real<lower=0> tau;
real theta_tilde[J];

transformed parameters {
real theta[J];
for (j in 1:J)
theta[j] = mu + tau * theta_tilde[j];

model {
mu ~ normal(0, 5);
tau ~ cauchy(0, 5);
theta_tilde ~ normal(0, 1);
y ~ normal(theta, sigma);

generated quantities {
vector[J] log_lik;
vector[J] y_hat;
for (j in 1:J) {
log_lik[j] = normal_lpdf(y[j] | theta[j], sigma[j]);
y_hat[j] = normal_rng(theta[j], sigma[j]);

eight_school_data = {
‘J’: 8,
‘y’: np.array([28., 8., -3., 7., -1., 1., 18., 12.]),
‘sigma’: np.array([15., 10., 16., 11., 9., 11., 10., 18.])

stan_model = pystan.StanModel(model_code=schools_code)
fit = stan_model.sampling(chains=3, data=eight_school_data, control={“adapt_delta” : 0.9})

Okay, this clarifies things somewhat. The compiler has nothing to do with this. It’s just a case of parallel chains not working. What gets printed if you replace the last two lines of your code with the following?:

stan_model = pystan.StanModel(model_code=schools_code)

import sys

fit = stan_model.sampling(chains=3, data=eight_school_data, control={"adapt_delta" : 0.9})

This is the print out:

3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]

Hi, can you see any messages in internal console?

Hi, I checked, there is no message in internal console when it is stuck.

Can you try run that code with python on commandline?

Add verbose=True for .sampling.

This problem might be due to Spyder.

See answer here and see if that helps

1 Like

You are right; the problem is because of Spyder itself. I can run the same code on Jupyter. However, applying the solution on Stack overflow changed nothing. Still hanging without no messages in both ipython and internal consoles when verbose is true.