Model runs in rstan but not in cmdstan; also additional problems with the R GUI (but not Rstudio or running R from the terminal)

I’ve been having difficulty getting cmdstanr to run on my computer. It was working before, but now it’s not. I’ll demonstrate with a simple Stan program, which I’ll try running in Rstudio, the regular R GUI, and plain old R in the terminal window, each time using cmdstanr and rstan. I’m using a Mac (Catalina 10.15.6), Rstudio 1.3.959, R 4.0.2, cmdstanr 0.0.0.9005, rstan 2.21.2.

Here’s my Stan program, which is in a file, test.stan:

parameters {
  real a;
} 
model {
  a ~ normal(0, 1);
}

Here’s my R script:

library("cmdstanr")
library("rstan")
stanfit <- function(fit) rstan::read_stan_csv(fit$output_files())

test_model <- cmdstan_model("test.stan")
fit <- test_model$sample()
print(stanfit(fit))

fit2 <- stan("test.stan")
print(fit2)
  1. Here’s what happens in the regular R GUI:

1a. cmdstanr crashes in the compilation stage. (When I do the cmdstan_model() function the R console just hangs, giving me that spinning multicolored ball on my Mac.)

1b. rstan causes R to hang too. No spinning ball, but it still hangs, and I have to Force Quit the R session.

  1. Here’s what happens in Rstudio:

2a. cmdstanr fails (but does not crash) in the compilation stage, returning the following error:

Compiling Stan program...
Error in processx::run(command = make_cmd(), args = c(tmp_exe, cpp_options_to_compile_flags(cpp_options),  : 
  System command 'make' failed, exit status: 2, stderr:
E> fatal error: file '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/_symbol_aliasing.h' has been modified since the precompiled header 'stan/src/stan/model/model_header.hpp.gch' was built: size changed
E> note: please rebuild precompiled header 'stan/src/stan/model/model_header.hpp.gch'
E> 1 error generated.
E> make: *** [/var/folders/j8/kg_4ryhs4d78nts87zqw73wh0000gn/T/Rtmp2TlwEZ/model-12b6b4689b7e2] Error 1
Type .Last.error.trace to see where the error occured

“Occured” is a misspelling, but, anyway, I typed .Last.error.trace and got this:

 Stack trace:

 1. cmdstanr:::cmdstan_model("test.stan")
 2. CmdStanModel$new(stan_file = stan_file, compile = compile, ...)
 3. .subset2(public_bind_env, "initialize")(...)
 4. self$compile(...)
 5. processx::run(command = make_cmd(), args = c(tmp_exe, cpp_options_to_compile_flags(cpp_options),  ...
 6. throw(new_process_error(res, call = sys.call(), echo = echo,  ...

 x System command 'make' failed, exit status: 2, stderr:
E> fatal error: file '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/_symbol_aliasing.h' has been modified since the precompiled header 'stan/src/stan/model/model_header.hpp.gch' was built: size changed
E> note: please rebuild precompiled header 'stan/src/stan/model/model_header.hpp.gch'
E> 1 error generated.
E> make: *** [/var/folders/j8/kg_4ryhs4d78nts87zqw73wh0000gn/T/Rtmp2TlwEZ/model-12b6b4689b7e2] Error 1 

2b. rstan works just fine.

  1. Here’s what happens running R from the terminal window (no GUI):

3a. cmdstanr fails but does not crash, just as it fails but does not crash in Rstudio.

3b. rstan works just fine, just as it works in Rstudio.

So, right now I’m surviving by working in Rstudio or the terminal (not the regular R GUI) and just using rstan, not cmdstanr.

But this is not an optimal solution, first because sometimes I’ve used the regular R GUI as a safe or stable alternative to Rstudio, but now it’s failing, and second because I’ve been migrating to cmstanr (it has fast compilation, other language features, and in some Discourse discussions it’s been suggested as a stable alternative to rstan), but now cmdstanr isn’t working for me.

Everything worked fine with me a few weeks ago. I guess it’s possible that some of my software has been updated in the meantime? I tried opening Xcode on my mac (it’s version 11.6 (11E708)) but that doesn’t fix the problem.

Hi,

it seems that clang has updated and that messes with the precompiled header. The precompiled header is used in cmdstan to enable faster compilation (up to 50% faster).

The latest versions of cmdstanr handle this automatically. For the one installed on your system you have to run

rebuild_cmdstan(cores = 2)

and it will all work again.

After that I would suggest upgrading to the beta cmdstanr release by running

install.packages("cmdstanr", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
1 Like

Rok:

I did this and it fixed the cmdstanr compilation problem in Rstudio and R from the command line. Thanks!

It still crashes the regular R GUI. I guess that not many people use the R GUI anymore, now that there’s Rstudio, so I won’t worry about that?

Will check why that is happening there. I honestly never used it, but it should not be crashing.

Thanks for the report!

Thanks @rok_cesnovar for the speedy reply. Things are working again. And I must admit, the crash was caused by a bug in a user-defined function. Mea culpa.