stan-dev / rstan

RStan, the R interface to Stan
https://mc-stan.org
1.03k stars 264 forks source link

"Error in sampler$call_sampler(args_list[[i]]) : " " c++ exception (unknown reason)" #716

Closed bertozzivill closed 4 years ago

bertozzivill commented 4 years ago

Summary:

When running a set of Stan models, I get the error "Error in sampler$call_sampler(args_list[[i]]) : " " c++ exception (unknown reason)". Which model it breaks on depends on the random seed, but it breaks consistently for any given random seed.

Description:

Solutions I have tried:

Reproducible Steps:

My data is sensitive, so I can't share it, but the following is my model code:

chains <- 1
warm<-5000
iterations<-10000
hh_size_max <- 10

mean_nets_model <- "data {
            int<lower=1> N;
            vector[N] nets_percapita; // formerly y1-10
            vector[N] adj_mean_nets; //formerly z1-10
            }
        parameters {
            real alpha_mean_nets; //formerly i1-10
            real beta_mean_nets; //fomerly b1-10
            real<lower=0> tau_mean_nets; //formerly tau1-10
         } 
         model {
                alpha_mean_nets ~ uniform(-20,20);
                beta_mean_nets ~ uniform(-20,20);
                  tau_mean_nets ~ gamma(0.1,0.1);
                  adj_mean_nets ~ normal(alpha_mean_nets + beta_mean_nets*nets_percapita, tau_mean_nets);
        }"

mean_net_fit <- lapply(1:hh_size_max, function(this_hhsize){
  mean_nets <- as.list(svy_indicators[hhsize==this_hhsize, list(adj_mean_nets, hhsize, nets_percapita)])
  mean_nets$N <- nrow(svy_indicators[hhsize==this_hhsize])

  this_mean_net_fit <-  stan(model_code=mean_nets_model,
                           warmup=warm,
                           data = mean_nets, 
                           chains=chains, 
                           iter=iterations,verbose = FALSE, refresh = -1)
  return(this_mean_net_fit)
})

Current Output:

Current Makevars:

# if you downloaded llvm manually above, replace with your chosen NEW_PATH/clang
LLVM_LOC = /usr/local/opt/llvm
CC=$(LLVM_LOC)/bin/clang -fopenmp
CXX=$(LLVM_LOC)/bin/clang++ -fopenmp
# -O3 should be faster than -O2 (default) level optimisation ..
CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe
LDFLAGS=-L/usr/local/opt/gettext/lib -L$(LLVM_LOC)/lib -Wl,-rpath,$(LLVM_LOC)/lib
CPPFLAGS=-I/usr/local/opt/gettext/include -I$(LLVM_LOC)/include

CXX14FLAGS=-O3 -march=native -mtune=native
CXX14FLAGS += -arch x86_64 -ftemplate-depth-256

RStan Version:

rstan version 2.19.2

R Version:

RStudio version 1.2.1335 R version 3.6.0

Operating System:

OSX 10.15.1

ssp3nc3r commented 4 years ago

Hi @bertozzivill , I see you are running this on Mac OS Catalina. There's an issue, but we think we have a solution at: https://discourse.mc-stan.org/t/dealing-with-catalina-ii/11802

bertozzivill commented 4 years ago

Thanks @ssp3nc3r, it's working now. I was previously unaware of @coatless' installer for clang++, so I installed that and then ran the line suggested in @bgoodri's first post on the page you sent. Now all my models are running smoothly, I appreciate the help and the rapid response.

Is it possible to get the clang++ installer and this fix for Catalina featured more prominently on the "Getting Started" page?

bgoodri commented 4 years ago

Yes, but we are still working out the details of the best solution.

On Fri, Nov 8, 2019 at 1:55 PM Amelia Bertozzi-Villa < notifications@github.com> wrote:

Thanks @ssp3nc3r https://github.com/ssp3nc3r, it's working now. I was previously unaware of @coatless https://github.com/coatless' installer for clang++, so I installed that and then ran the line suggested in @bgoodri https://github.com/bgoodri's first post on the page you sent. Now all my models are running smoothly, I appreciate the help and the rapid response.

Is it possible to get the clang++ installer and this fix for Catalina featured more prominently on the "Getting Started" page?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstan/issues/716?email_source=notifications&email_token=AAZ2XKUTUCLBLGTWE3LRBLTQSWYZFA5CNFSM4JKTKBX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEDTA7MI#issuecomment-551948209, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZ2XKWMSHRZEQ5BJZV5X2TQSWYZFANCNFSM4JKTKBXQ .

elbamos commented 4 years ago

FYI - I'm seeing the same issue on Catalina, using llvm 9.4 from homebrew, with rstan built from source.

(The Coatless installer did not resolve the problem for me.)

I've been through the discourse page, and I'm pretty bewildered by what the underlying problem is, and how changing llvm versions is a solution?

Ptterz commented 4 years ago

So, the installer by Coatless didn't do it for me either. Now even the older codes that uses Stan are broken and throws that same error message.


EDIT:

For anyone with this issue, go to the post linked below: https://discourse.mc-stan.org/t/dealing-with-catalina-ii/11802/76

bgoodri commented 4 years ago

We need a lot more detail in the failure case and probably to tag @coatless . When you compile a Stan program with stan_model and then do

dlls <- getLoadedDLLs() paths <- vapply(dlls, [[, "path", FUN.VALUE = character(1)) invisible(lapply(paths, function(path) {

if (!file.exists(path)) return(FALSE)

output <- system(paste("otool -L", shQuote(path), "| grep libc++ || true"), intern = TRUE) if (length(output) == 0) return(FALSE)

writeLines(paste0(path, ":")) writeLines(output)

}))

what does it say for the last DLL (it have a random name in the temporary directory).

On Thu, Nov 28, 2019 at 1:11 PM Pete notifications@github.com wrote:

So, the installer by Coatless didn't do it for me either. Now even the older codes that uses Stan are broken and throws that same error message.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstan/issues/716?email_source=notifications&email_token=AAZ2XKSLY4M5DCG4FF5F45DQWACUJA5CNFSM4JKTKBX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFNHMJQ#issuecomment-559576614, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZ2XKWHG7D2IQB7FXWZ6G3QWACUJANCNFSM4JKTKBXQ .

Ptterz commented 4 years ago

Sorry about that! I was not sure what you wanted to know. But, by doing that, the last DLL it prints is: "/var/folders/hl/nhq1bfz13z3_6vh30rl7fgqw0000gn/T//RtmpIUzsbQ/file13eea5d8065ba.so: /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 800.7.0)"

bgoodri commented 4 years ago

OK, under the @coatless installer that is not supposed to be /usr/lib/libc++.1.dylib . We need to figure out why that is happening, but it will probably work in the interim if you put

CXX14 = /usr/local/clang7/bin/clang++ -stdlib=libc++ -nostdinc++ -I/usr/local/clang7/include/c++/v1 SHLIB_CXX14LD = /usr/local/clang7/bin/clang++ -L/usr/local/clang7/lib/

at the end of ~/.R/Makevars .

On Thu, Nov 28, 2019 at 2:07 PM Pete notifications@github.com wrote:

Sorry about that! I was not sure what you wanted to know. But, by doing that, the last DLL it prints is:

"/var/folders/hl/nhq1bfz13z3_6vh30rl7fgqw0000gn/T//RtmpIUzsbQ/file13eea5d8065ba.so: /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 800.7.0)"

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstan/issues/716?email_source=notifications&email_token=AAZ2XKWBZGNDFG37C2XTUYTQWAJHJA5CNFSM4JKTKBX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFNJUZI#issuecomment-559585893, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZ2XKXFSMAR76VCKJBSY7DQWAJHJANCNFSM4JKTKBXQ .

Ptterz commented 4 years ago

Hi, sorry for a late answer. How can I modify the file? I already tried to modify it according to "Dealing with Catalina II" conversation, but system denies all changes.

bgoodri commented 4 years ago

Ah, possibly the problem is that @coatless installer tried to modify it but couldn't due to some permissions issue. What are its contents, which can be found by executing

readLines("~/.R/Makevars")

On Fri, Nov 29, 2019 at 12:50 AM Pete notifications@github.com wrote:

Hi, sorry for a late answer. How can I modify the file? I already tried to modify it according to "Dealing with Catalina II" conversation, but system denies all changes.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstan/issues/716?email_source=notifications&email_token=AAZ2XKXTVAZ6ZWXT6U3KSFLQWCUQ5A5CNFSM4JKTKBX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFN5S7A#issuecomment-559667580, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZ2XKW6UDNUL2MCONESYYTQWCUQ5ANCNFSM4JKTKBXQ .

Ptterz commented 4 years ago

Yeah, that could be the reason! Funny that it still said installation was successful. I would have thought it informed you if there was a problem.

Anyway, I sudo'ed the rights for the group modified the file according to this: https://discourse.mc-stan.org/t/dealing-with-catalina-ii/11802/76 It solved the problem for me!

coatless commented 4 years ago

Greetings and Salutations All,

Sorry for the delay in seeing this. I was out due to thanksgiving and a broken phone :/

To recap, the main problem seems to be the ~/.R/Makevars file wasn't set up probably due to the existing file being restricted to root?

Also, @elbamos, regarding the homebrew compilation... This installer is only meant to setup the environment for the official CRAN macOS binaries. So, you may need to change the ~/.R/Makevars and ~/.Renviron files.

coatless commented 4 years ago

Released a new version of the installer that addresses the file permission issue.

https://github.com/rmacoslib/r-macos-rtools/releases/latest

swood-ecology commented 4 years ago

@bgoodri @coatless Have you been getting comments about this error still?

I am running Catalina 10.15.3 with R 3.6.3. I installed the most recent tools installer here: https://github.com/rmacoslib/r-macos-rtools/releases/latest

When I try to compile my model I'm still getting the same error of:


[2] "  c++ exception (unknown reason)"                
error occurred during calling the sampler; sampling not done```
coatless commented 4 years ago

@swood-ecology please rebuild the stan package from source:

install.packages(“rstan”, type="source", dependencies = TRUE)
swood-ecology commented 4 years ago

for some reason I'm having a hard time doing that. i tried this both by doing install.packages(“rstan”, type="source", dependencies = TRUE) and remotes::install_github("stan-dev/rstan", ref = "develop", subdir = "rstan/rstan", build_opts = "")

i get the following error:

Error: Failed to install 'rstan' from GitHub:
  System command 'R' failed, exit status: 1, stdout + stderr (last 10 lines):
E> In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/RcppEigen/include/unsupported/Eigen/SparseExtra:51:
E> /Library/Frameworks/R.framework/Versions/3.6/Resources/library/RcppEigen/include/unsupported/Eigen/../../Eigen/src/Core/util/ReenableStupidWarnings.h:10:30: warning: pragma diagnostic pop could not pop, no matching push [-Wunknown-pragmas]
E>     #pragma clang diagnostic pop
E>                              ^
E> 17 warnings generated.
E> 13 warnings generated.
E> ERROR: compilation failed for package ‘rstan’
E> * removing ‘/private/var/folders/09/5xl87zc16kz797xl2tbhc60r0000gn/T/RtmpkPS9b4/Rinst111c7295a8d2/rstan’
E>       -----------------------------------
E> ERROR: package installation failed
nolanoreilly commented 3 years ago

I've been having this issue for a while, and I tried reinstalling RStan from source as per this post but when I try to install the installer I get the attached error. My computer has since been updated from Catalina to Big Sur, would this be causing the issue? So far none of the above have worked for me. Screen Shot 2020-11-25 at 12 29 35 PM

bgoodri commented 3 years ago

If you have Big Sur or otherwise have R 4.x, then you don't need the macOS R toolchain. You just need to install XCode.

On Wed, Nov 25, 2020 at 2:32 PM nolanoreilly notifications@github.com wrote:

I've been having this issue for a while, and I tried reinstalling RStan from source as per this post https://github.com/stan-dev/rstan/wiki/Installing-RStan-from-source-on-a-Mac but when I try to install the installer I get the attached error. My computer has since been updated from Catalina to Big Sur, would this be causing the issue? So far none of the above have worked for me. [image: Screen Shot 2020-11-25 at 12 29 35 PM] https://user-images.githubusercontent.com/44284315/100273571-36348980-2f1a-11eb-9ff6-81e2c059037c.png

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstan/issues/716#issuecomment-733908805, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZ2XKSPGK6HWADDPCF6TE3SRVLV3ANCNFSM4JKTKBXQ .

jgabry commented 3 years ago

@bgoodri I think that contradicts what it says in the RStan instructions:

We recommend using the macOS R toolchain installer for pre-Catalina versions of the operating system, in which case you can then install the binary version of the RStan package. If you have Catalina, then you need to open it by pressing CTRL+right-click and then clicking Open in order to bypass the security settings that took effect on Macs with Catalina on January 1, 2020.

I know it says "pre-Catalina" but then the next sentence talks about Catalina and opening "it", and the only thing "it" could be referring to is the macOS R toolchain installer. If people with R 4.x and Catalina or later don't need the macOS R toolchain installer can you update the instructions to the latest info? I just fixed the page title which said it was for installing from Mac from source even though it wasn't just from source. But you know more about the installation stuff so can you update the instructions?

Similar confusion also came up recently on the forums: https://discourse.mc-stan.org/t/update-macos-to-catalina-or-mojave-for-least-problems-with-stan-based-software/19433/7?u=jonah

bgoodri commented 3 years ago

OK. I think now on Macs the main thing to worry about is R version 3.x vs. 4.x. If everyone uses 4.x, we will be OK. There might be new problems with the C++ toolchain installer on Big Sur but if so, @nolanoreilly should file an issue at https://github.com/rmacoslib/r-macos-rtools/issues but there have been some reports of success. If the C++ toolchain installer is not working, rstan should still work as long as XCode can be installed from the Appstore.

jgabry commented 3 years ago

If the C++ toolchain installer is not working, rstan should still work as long as XCode can be installed from the Appstore.

Ah ok I see. Thank you for updating the instructions. Sorry they need to be changed so frequently, that's super annoying. I remember the good old days when only Windows was a problem.