Debugging pystan3

I got the “Initialization failed” error while developing a model in PyStan3. It seemed to be impossible to debug this model since every time I would get the error, the prints in the Stan code would not show.

I then switched to Pystan 2.19 to see if it was about the version. I got the same problems in the stan code (init error), but I was able to debug them as pystan2 allowed me to debug by printing.

Once I fixed the bugs in the stan code, I tried to switch back to pystan3. However, same initialization failed error appears, and I am not able to print anything from the stan file in order to debug the problem. So now I have a working stan code in Pystan2 but same stan code will not work in Pystan3 due to Initialization failed error.

So the question is, how to print/debug from stan files using pystan3 when facing errors?

A simple example:
Pystan2:

import pystan

stan_code = """
parameters { 
  real<upper=0.0> y; 
}
model{
  print(y);
  target += log(y);
}
"""

sm = pystan.StanModel(model_code=stan_code)
fit = sm.sampling(data={}, iter=1, chains=1)

Output:

INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_4d2aeabbf3bdd6f112cd0695ffd230cb NOW.
-0.238063

Rejecting initial value:
  Log probability evaluates to log(0), i.e. negative infinity.
  Stan can't start sampling from this initial value.
-1.7141

Rejecting initial value:

...

Initialization between (-2, 2) failed after 100 attempts.
 Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
Traceback (most recent call last):
  ...
RuntimeError: Initialization failed.

Pystan3:

import stan

stan_code = """
parameters { 
  real<upper=0.0> y; 
}
model{
  print(y);
  target += log(y);
}
"""
posterior = stan.build(stan_code, data={})
fit = posterior.sample(num_chains=1, num_samples=1)

Output:

Building: 9.9s, done.
Sampling: Initialization failed.
Traceback (most recent call last):
  ...
RuntimeError: Initialization failed.

System details:

  • Mac Monterey (M1)
  • Python 3.8.12
  • Pystan(s) 2.19.0.0 / 3.4.0
  • Compiler Clang 10.0.0

ps. I launched the scripts from terminal, so notebook problems that seem to be frequent should not be the problem.

Thanks for the self-contained example.

It would be nice to fix this.

Right now, pystan (httpstan, actually), discards everything if the sampling process does not finish.

It need not discard everything. It could attach the messages it receives (including the output of the prints) to the operation metadata. pystan could then show this to the user before raising the exception.

I created an issue for this https://github.com/stan-dev/httpstan/issues/595.

1 Like

There’s now a patch with a fix, see feat: Add context to initialization failed error by riddell-stan · Pull Request #597 · stan-dev/httpstan · GitHub It’s a bit limited but it does cover this particular case.

1 Like