unconstrain_pars() crashes R
Summary:
unconstrain_pars causes segfault in R
Description:
When running unconstrain_pars on a newly fitted model, it causes a segfault and crashes R.
Reproducible Steps:
Standard eight schools model
data {
int<lower=0> J;
real y[J];
real<lower=0> sigma[J];
}
parameters {
vector[J] theta_trans;
real mu;
real<lower=0> tau;
}
transformed parameters{
vector[J] theta;
theta = theta_trans * tau + mu;
}
model {
// priors
target += normal_lpdf(theta_trans | 0, 1);
target += normal_lpdf(mu | 0, 5);
target += cauchy_lpdf(tau | 0, 5);
//likelihood
target += normal_lpdf(y | theta, sigma);
}
library(rstan)
model <- stan_model("eight_schools.stan")
d <- list(
J = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18)
)
fit <- sampling(model, data = d)
unconstrain_pars(fit, pars = list(mu = 0, theta = rep(1.5, 8), tau = 2))
Current Output:
*** caught segfault ***
*** caught segfault ***
address (nil), cause 'memory not mapped'
Traceback:
1: .External(list(name = "CppMethod__invoke_notvoid", address = <pointer:0x55a5f4813370>, dll = list(name = "Rcpp", path =
"/lib/R/library/Rcpp/libs/Rcpp.so", dynamicLookup = TRUE, handle = <pointer: 0x55a5f2b78520>, info = <pointer: 0x55a5f202dad0>),
numParameters = -1L), <pointer: 0x55a5fb4cd110>, <pointer: 0x55a5f76d9760>, .pointer, ...)
2: [email protected]$stan_fit_instance$unconstrain_pars(pars)
3: .local(object, ...)
4: unconstrain_pars(fit, pars = list(mu = 0, theta = rep(1.5, 8), tau = 2))
5: unconstrain_pars(fit, pars = list(mu = 0, theta = rep(1.5, 8), tau = 2))
Expected Output:
Interestingly, the following works:
fit1 <- stan_demo("eight_schools")
unconstrain_pars(fit1, pars = list(mu = 0, theta = rep(1.5, 8), tau = 2))
[1] 0.0000000 1.5000000 1.5000000 1.5000000 1.5000000 1.5000000 1.5000000
[8] 1.5000000 1.5000000 0.6931472
RStan Version:
2.26.1
R Version:
"R version 4.0.5 (2021-03-31)"
Operating System:
Ubuntu 18.04
@n-kall I can't reproduce this issue. Can you install the experimental StanHeaders and rstan from the experimental branch (https://github.com/stan-dev/rstan/tree/experimental) with the development version of Rcpp and RcppParallel?
remotes::install_git("https://github.com/stan-dev/rstan", subdir = "StanHeaders", ref= "experimental")
# OR
remotes::install_github("stan-dev/rstan/StanHeaders@experimental")
remotes::install_git("https://github.com/stan-dev/rstan", subdir = "rstan/rstan", ref = "experimental")
# OR
remotes::install_github("stan-dev/rstan/rstan/rstan@experimental")
This issue still happens on the experimental branch with development versions of Rcpp/parallel. Here's the current output (with sessionInfo):
rstan version 2.27.0.9000 (Stan version 2.27.0)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
For within-chain threading using `reduce_sum()` or `map_rect()` Stan functions,
change `threads_per_chain` option:
rstan_options(threads_per_chain = 1)
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.5 LTS
Matrix products: default
BLAS: /lib/R/lib/libRblas.so
LAPACK: /lib/R/lib/libRlapack.so
locale:
[1] LC_CTYPE=en_GB.utf8 LC_NUMERIC=C
[3] LC_TIME=en_DK.utf8 LC_COLLATE=en_GB.utf8
[5] LC_MONETARY=en_GB.utf8 LC_MESSAGES=en_GB.utf8
[7] LC_PAPER=fi_FI.utf8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rstan_2.27.0.9000 StanHeaders_2.27.0.9000
loaded via a namespace (and not attached):
[1] Rcpp_1.0.7 compiler_4.1.0 pillar_1.6.1
[4] prettyunits_1.1.1 pkgbuild_1.2.0 jsonlite_1.7.2
[7] lifecycle_1.0.0 tibble_3.1.2 gtable_0.3.0
[10] pkgconfig_2.0.3 rlang_0.4.11 DBI_1.1.1
[13] cli_3.0.0 parallel_4.1.0 curl_4.3.2
[16] loo_2.4.1 gridExtra_2.3 dplyr_1.0.7
[19] generics_0.1.0 vctrs_0.3.8 stats4_4.1.0
[22] grid_4.1.0 tidyselect_1.1.1 glue_1.4.2
[25] inline_0.3.19 R6_2.5.0 processx_3.5.2
[28] fansi_0.5.0 purrr_0.3.4 ggplot2_3.3.5
[31] callr_3.7.0 magrittr_2.0.1 codetools_0.2-18
[34] matrixStats_0.59.0 scales_1.1.1 ps_1.6.0
[37] ellipsis_0.3.2 assertthat_0.2.1 colorspace_2.0-2
[40] V8_3.4.2 utf8_1.2.1 RcppParallel_5.1.4-9000
[43] munsell_0.5.0 crayon_1.4.1
Warning messages:
1: There were 1 divergent transitions after warmup. See
https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
to find out why this is a problem and how to eliminate them.
2: Examine the pairs() plot to diagnose sampling problems
*** caught segfault ***
address (nil), cause 'memory not mapped'
Traceback:
1: .External(list(name = "CppMethod__invoke_notvoid", address = <pointer: 0x55c72f6f6090>, dll = list(name = "Rcpp", path = "/lib/R/library/Rcpp/libs/Rcpp.so", dynamicLookup = TRUE, handle = <pointer: 0x55c730c54a60>, info = <pointer: 0x55c72e312d10>), numParameters = -1L), <pointer: 0x55c736bf9730>, <pointer: 0x55c72efdbb20>, .pointer, ...)
2: [email protected]$stan_fit_instance$unconstrain_pars(pars)
3: .local(object, ...)
4: unconstrain_pars(fit, pars = list(mu = 0, theta = rep(1.5, 8), tau = 2))
5: unconstrain_pars(fit, pars = list(mu = 0, theta = rep(1.5, 8), tau = 2))
An irrecoverable exception occurred. R is aborting now ...
*** caught segfault ***
This looks like a dependency issue. Try to install the development version of Rcpp.
Thanks for the follow up. By 'development version', do you mean Rcpp directly from the git repo?
This still occurs in the latest Rcpp installed today via remotes::install_github("RcppCore/Rcpp").
If this is an issue in Rcpp, do you have any ideas for making a simpler example for an Rcpp bug report (I'm not too familiar with how unconstrain_pars works under the hood)?
I think this is due to an outdated dependency, especially if you've upgraded R or a system library. The easiest solution is to reinstall the packages or try:
update.packages(checkBuilt = TRUE, ask = FALSE)
Unfortunately, updating all packages didn't seem to fix it. The same error also occurs in a Github actions system for running tests via rcmdcheck (ubuntu 20.04, with rstan from CRAN), so it doesn't seem specific to the packages on my system.
I get the same segfault with rstan_2.26.13 and StanHeaders_2.26.13.