Specifying adapt_delta crashes RStudio every time

Hi,

I just got a new macbook w/ the M1 chip. I reinstalled Stan, ran some of the existing sample codes and everything worked fine! Then I tried to reproduce an analysis for a previous project and every time I hit the sampling() function RStudio would crash.

After investigating, I eventually narrowed it down to adapt_delta. Whenever I would run sampling() without specifying adapt_delta, it runs fine. However, specifying it leads to RStudio crashing.

I’ve attached a minimal example that reproduces this error on my end - it’s the template code that you get when you start a new Stan file in RStudio: test.stan (750 Bytes)
Here is the corresponding R code: test.R (274 Bytes)

The first sampling step runs fine :

> set.seed(1)
> 
> library(rstan)
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.21.2, GitRev: 2e1f913d3ca3)
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)
> 
> N = 1000
> y = rnorm(N, 100, 10)
> 
> mod = stan_model("test.stan")
> 
> fit = sampling(mod, seed=1, chains=1, data=list(N=N, y=y) )

SAMPLING FOR MODEL 'test' NOW (CHAIN 1).
Chain 1: 
Chain 1: Gradient evaluation took 1.9e-05 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.19 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1: 
Chain 1: 
Chain 1: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 1: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 1: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 1: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 1: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 1: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 1: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 1: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 1: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 1: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 1: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 1: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 1: 
Chain 1:  Elapsed Time: 0.040169 seconds (Warm-up)
Chain 1:                0.033589 seconds (Sampling)
Chain 1:                0.073758 seconds (Total)
Chain 1: 
> summary(fit)
$summary
             mean     se_mean        sd         2.5%         25%         50%         75%       97.5%     n_eff      Rhat
mu       99.88055 0.010830127 0.3380073    99.176074    99.66878    99.88774   100.09241   100.55528  974.0584 1.0007501
sigma    10.37445 0.006978881 0.2242670     9.926377    10.22052    10.36980    10.52666    10.79644 1032.6641 0.9991188
lp__  -2835.06629 0.055807509 1.1096206 -2837.890928 -2835.31867 -2834.71446 -2834.34603 -2834.09044  395.3336 0.9991661

$c_summary
, , chains = chain:1

         stats
parameter        mean        sd         2.5%         25%         50%         75%       97.5%
    mu       99.88055 0.3380073    99.176074    99.66878    99.88774   100.09241   100.55528
    sigma    10.37445 0.2242670     9.926377    10.22052    10.36980    10.52666    10.79644
    lp__  -2835.06629 1.1096206 -2837.890928 -2835.31867 -2834.71446 -2834.34603 -2834.09044

The second sampling step crashes RStudio. Note I’m only setting adapt_delta to its default value.

Any help is much appreciated. Thanks!

R Session Info:

> sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.4

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rstan_2.21.2         ggplot2_3.3.5        StanHeaders_2.21.0-7

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7         compiler_4.1.0     pillar_1.6.1       prettyunits_1.1.1  tools_4.1.0        pkgbuild_1.2.0     jsonlite_1.7.2     lifecycle_1.0.0   
 [9] tibble_3.1.2       gtable_0.3.0       pkgconfig_2.0.3    rlang_0.4.11       cli_3.0.1          parallel_4.1.0     curl_4.3.2         loo_2.4.1         
[17] gridExtra_2.3      dplyr_1.0.7        withr_2.4.2        generics_0.1.0     vctrs_0.3.8        tidyselect_1.1.1   stats4_4.1.0       grid_4.1.0        
[25] inline_0.3.19      glue_1.4.2         R6_2.5.0           processx_3.5.2     fansi_0.5.0        purrr_0.3.4        callr_3.7.0        magrittr_2.0.1    
[33] codetools_0.2-18   matrixStats_0.59.0 scales_1.1.1       ps_1.6.0           ellipsis_0.3.2     colorspace_2.0-2   V8_3.4.2           utf8_1.2.1        
[41] RcppParallel_5.1.4 munsell_0.5.0      crayon_1.4.1

Computer Info: macOS Big Sur 11.4. MacBook Pro 13-inch M1, 2020, 16 GB.

1 Like

Hi,

there seems to be some issue with using the control statement. Perhaps using cmdstanr is an option?

1 Like

Thanks @torkar . Comforting to see similar issue has come up earlier. Running the model with cmdstanr does work for me even when I specify adapt_delta. See code
cmd_stan_test.R (239 Bytes) and log below.

> library(cmdstanr) 
> set.seed(1)
>
> N = 1000
> y = rnorm(N, 100, 10)
> 
> mod = cmdstan_model("test.stan")
Model executable is up to date!
> 
> fit = mod$sample(seed=1, 
+                  chains=1, 
+                  data=list(N=N, y=y), 
+                  adapt_delta=.8 )
Running MCMC with 1 chain...

Chain 1 Iteration:    1 / 2000 [  0%]  (Warmup) 
Chain 1 Iteration:  100 / 2000 [  5%]  (Warmup) 
Chain 1 Iteration:  200 / 2000 [ 10%]  (Warmup) 
Chain 1 Iteration:  300 / 2000 [ 15%]  (Warmup) 
Chain 1 Iteration:  400 / 2000 [ 20%]  (Warmup) 
Chain 1 Iteration:  500 / 2000 [ 25%]  (Warmup) 
Chain 1 Iteration:  600 / 2000 [ 30%]  (Warmup) 
Chain 1 Iteration:  700 / 2000 [ 35%]  (Warmup) 
Chain 1 Iteration:  800 / 2000 [ 40%]  (Warmup) 
Chain 1 Iteration:  900 / 2000 [ 45%]  (Warmup) 
Chain 1 Iteration: 1000 / 2000 [ 50%]  (Warmup) 
Chain 1 Iteration: 1001 / 2000 [ 50%]  (Sampling) 
Chain 1 Iteration: 1100 / 2000 [ 55%]  (Sampling) 
Chain 1 Iteration: 1200 / 2000 [ 60%]  (Sampling) 
Chain 1 Iteration: 1300 / 2000 [ 65%]  (Sampling) 
Chain 1 Iteration: 1400 / 2000 [ 70%]  (Sampling) 
Chain 1 Iteration: 1500 / 2000 [ 75%]  (Sampling) 
Chain 1 Iteration: 1600 / 2000 [ 80%]  (Sampling) 
Chain 1 Iteration: 1700 / 2000 [ 85%]  (Sampling) 
Chain 1 Iteration: 1800 / 2000 [ 90%]  (Sampling) 
Chain 1 Iteration: 1900 / 2000 [ 95%]  (Sampling) 
Chain 1 Iteration: 2000 / 2000 [100%]  (Sampling) 
Chain 1 finished in 0.0 seconds.
> fit$summary()
# A tibble: 3 x 10
  variable    mean  median    sd   mad      q5     q95  rhat ess_bulk ess_tail
  <chr>      <dbl>   <dbl> <dbl> <dbl>   <dbl>   <dbl> <dbl>    <dbl>    <dbl>
1 lp__     -2835.  -2835.  1.00  0.689 -2837.  -2834.  1.00      449.     393.
2 mu          99.9    99.9 0.328 0.312    99.4   100.  0.999     946.     633.
3 sigma       10.4    10.4 0.222 0.228    10.0    10.7 1.00      937.     723.

Could you try out the experimental branch of Rutan? That has a lot of fixes in it

remotes::install_git("https://github.com/stan-dev/rstan", subdir = "StanHeaders", ref = "experimental") 
remotes::install_git("https://github.com/stan-dev/rstan", subdir = "rstan/rstan", ref = "experimental") 

1 Like