Rstan on Apple M2 chip

Has anyone had any success getting Rstan working on the new M2 chip. Running Ventura on a new M2 MacBook Air.

example(stan_model, package = "rstan", run.dontrun = TRUE)

Gets me this:

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) : 
          typedef double local_scalar_t__;                       ^36 warnings generated.error: unknown target CPU 'apple-a12'note: valid target CPU values are: i386, i486, winchip-c6, winchip2, c3, i586, pentium, pentium-mmx, pentiumpro, i686, pentium2, pentium3, pentium3m, pentium-m, c3-2, yonah, pentium4, pentium4m, prescott, nocona, core2, penryn, bonnell, atom, silvermont, slm, goldmont, goldmont-plus, tremont, nehalem, corei7, westmere, sandybridge, corei7-avx, ivybridge, core-avx-i, haswell, core-avx2, broadwell, skylake, skylake-avx512, skx, cascadelake, cooperlake, cannonlake, icelake-client, rocketlake, icelake-server, tigerlake, sapphirerapids, alderlake, knl, knm, lakemont, k6, k6-2, k6-3, athlon, athlon-tbird, athlon-xp, athlon-mp, athlon-4, k8, athlon64, athlon-fx, opteron, k8-sse3, athlon64-sse3, opteron-sse3, amdfam10, barcelona, btver1, btver2, bdver1, bdver2, bdver3, bdver4, znver1, znver2, znver3, x86-64, geodemake: * [file414619c043e.o] Error 1

System details:

Operating System: Ventura 13.0
Interface Version: Rstan 2.21.7
Compiler/Toolkit: gfortran 12.2 for Ventura (macOS 13)

xcode-select --version
xcode-select version 2396.
clang --version
Apple clang version 14.0.0 (clang-1400.0.29.202)

Is it just too new to be working properly?

thank you!

I believe you’ll need to use the preview of the next rstan version, I use that on an M1 with no issues:

remove.packages(c("StanHeaders", "rstan"))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

Thanks! Still same error, unfortunately …

I think the error is a clang error with the unknown target CPU. Maybe this thread Problem compling rstan models on Mac M1 with clang 13.0.0

Also in this thread som e of the things folks had to do on the M1 chips to get it up and running

this is exactly my problem I think – but I can’t work out how to edit the Makevars. I’m new to all this … :(

1 Like

Can you post the output from:

readLines("~/.R/Makevars")

And can you check if you’re able to compile c++ at all:

system("touch foo.cpp")
system("R CMD SHLIB foo.cpp")

Sure …

> readLines("~/.R/Makevars")
[1] ""                                                                 
[2] "CXX14FLAGS += -O3 -mtune=native -arch x86_64 -ftemplate-depth-256"

Looks like no to c++ …

> system("touch foo.cpp")
> system("R CMD SHLIB foo.cpp")
clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/opt/R/arm64/include   -fPIC  -falign-functions=64 -Wall -g -O2  -c foo.cpp -o foo.o
clang++ -arch arm64 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/opt/R/arm64/lib -o foo.so foo.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: warning: -undefined dynamic_lookup may not work with chained fixups
ld: warning: dylib (/Library/Frameworks/R.framework/R) was built for newer macOS version (20.0) than being linked (13.0)

What output do you get from:

R.version$arch
> R.version$arch
[1] "aarch64"

It looks like your Makevars file was configured under an Intel Mac installation, which is why it has the -arch x86_64, so given that your current R installation is using Apple Silicon (aarch64) we’ll try resetting your Makevars.

Can you run:

cat("CXX14FLAGS += -O3 -arch arm64 -ftemplate-depth-256",
    file = "~/.R/Makevars", sep = "\n", append = FALSE)

And then try the rstan example model again?

1 Like

Amazing, that worked thanks!