Brms: sampling fails on macbook M2

Using R version 4.3.0 for mac [R-4.3.0-arm64.pkg] and brms 2.17.0, I get various problems when I try to build a brms model on a macbook air M2 (MacOS Monterey). The same thing happens with brms 2.19.0
First, the following is what happens when I specify to use more than one core (n.b. simple example just to illustrate the problem).

model <- brm(dist ~ speed, data=cars, cores=2)
Compiling Stan program...
Start sampling

SAMPLING
 FOR MODEL '70132a8135bce8c931d49bdfcfc12cae' NOW (CHAIN 1SAMPLING).
 FOR MODEL '70132a8135bce8c931d49bdfcfc12cae' NOW (CHAIN 2).

SAMPLING FOR MODEL '70132a8135bce8c931d49bdfcfc12cae' NOW (CHAIN 4).

SAMPLING FOR MODEL '70132a8135bce8c931d49bdfcfc12cae' NOW (CHAIN 3).
Error in x@mode : 
  no applicable method for `@` applied to an object of class "NULL"
In addition: Warning messages:
1: In parallel::mclapply(1:chains, FUN = callFun, mc.preschedule = FALSE,  :
  2 parallel function calls did not deliver results
2: In parallel::mclapply(1:chains, FUN = callFun, mc.preschedule = FALSE,  :
  2 function calls resulted in an error

Second, if I specify to use only one core, then something like a user prompt appears saying "Selection: " and it doesn’t progress from there:

 model <- brm(dist ~ speed, data=cars, cores=1)
Compiling Stan program...
Start sampling

SAMPLING FOR MODEL '70132a8135bce8c931d49bdfcfc12cae' NOW (CHAIN 1).
Selection: 

If I try to type something (for example “1”) at the user prompt and hit return, then R crashes with a segmentation fault.

Did anybody else have this problem? If so, how did you solve it?

Patrick

can you show the output of sessionInfo()?

> sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.5

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

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

time zone: Europe/London
tzcode source: internal

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

other attached packages:
[1] brms_2.17.0 Rcpp_1.0.10

loaded via a namespace (and not attached):
 [1] gtable_0.3.3         tensorA_0.36.2       ggplot2_3.4.2       
 [4] shinyjs_2.1.0        htmlwidgets_1.6.2    processx_3.8.1      
 [7] inline_0.3.19        lattice_0.21-8       callr_3.7.3         
[10] ps_1.7.5             vctrs_0.6.2          tools_4.3.0         
[13] crosstalk_1.2.0      generics_0.1.3       stats4_4.3.0        
[16] parallel_4.3.0       tibble_3.2.1         fansi_1.0.4         
[19] xts_0.13.1           pkgconfig_2.0.3      Matrix_1.5-4        
[22] checkmate_2.2.0      distributional_0.3.2 RcppParallel_5.1.7  
[25] lifecycle_1.0.3      compiler_4.3.0       farver_2.1.1        
[28] stringr_1.5.0        Brobdingnag_1.2-9    munsell_0.5.0       
[31] codetools_0.2-19     threejs_0.3.3        httpuv_1.6.11       
[34] htmltools_0.5.5      bayesplot_1.10.0     crayon_1.5.2        
[37] pillar_1.9.0         later_1.3.1          ellipsis_0.3.2      
[40] DT_0.28              StanHeaders_2.26.26  bridgesampling_1.1-2
[43] abind_1.4-5          nlme_3.1-162         mime_0.12           
[46] rstan_2.21.8         posterior_1.4.1      gtools_3.9.4        
[49] tidyselect_1.2.0     digest_0.6.31        mvtnorm_1.2-0       
[52] stringi_1.7.12       colourpicker_1.2.0   dplyr_1.1.2         
[55] reshape2_1.4.4       fastmap_1.1.1        grid_4.3.0          
[58] colorspace_2.1-0     cli_3.6.1            magrittr_2.0.3      
[61] shinythemes_1.2.0    loo_2.6.0            base64enc_0.1-3     
[64] pkgbuild_1.4.0       utf8_1.2.3           prettyunits_1.1.1   
[67] scales_1.2.1         promises_1.2.0.1     backports_1.4.1     
[70] matrixStats_1.0.0    igraph_1.4.3         gridExtra_2.3       
[73] zoo_1.8-12           coda_0.19-4          shiny_1.7.4         
[76] miniUI_0.1.1.1       markdown_1.7         rstantools_2.3.1    
[79] rlang_1.1.1          xtable_1.8-4         glue_1.6.2          
[82] shinystan_2.6.0      dygraphs_1.1.1.6     R6_2.5.1            
[85] plyr_1.8.8          
> 

You need to either reinstall rstan 2.21 from source, or update to 2.26 via install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

See R crashes during sampling · Issue #1070 · stan-dev/rstan · GitHub

Many thanks. Compiling rstan from source has worked.

1 Like

@pbcl Can you report back on general performance of your Macbook Air? I need to buy myself a new laptop and the MB Air is very interesting but I am concerned that it may become too hot when I run more complex models and thus that it then becomes too slow. Any recommendations? I am coming from MBP 2016. The 14 inch and 16 seem either too small or big, which is why I am eyeing the Air.

Hi, I’m using the 13 inch Macbook air M2, and I haven’t noticed any big problems. I can run models about 4 times faster than on my previous machine (e.g. 4 hours on the M2 vs. 16 hours on the old machine; however, that was a 2013 macbook air, with only 2 cores).

Compared to my 2019 MBP, the models from my new 2023 M2 MBP are anywhere from 1.5 to 6 times faster.

2 Likes