Running rstan on AWS Fargate

I’m running a variational bayes model in rstan in a docker container on AWS Fargate. A similar container runs fine on AWS Lambda, but when I switch to the Fargate EC2 instance, the VB crashes and I get the following error:

Chain 1: 1200 -31633.462 0.120 0.016
Chain 1: 1300 -31591.262 0.111 0.010
Chain 1: 1400 -31566.196 0.103 0.010
Chain 1: 1500 -31543.113 0.096 0.007 MEDIAN ELBO CONVERGED
Chain 1:
Chain 1: Drawing a sample of size 5000 from the approximate posterior...

double free or corruption (out)
Aborted (core dumped)"

where my stan model is invoked using:
posterior_irt <- rstan::vb(object = stan_model, data = full_data, iter = 30000, output_samples = 5000, init = 0)

The instance runs a docker container that I base off of rocker/r-ubuntu:20.04

during the docker build, I run an R script which I borrowed off the web:

Sys.setenv(MAKEFLAGS = paste0(“-j”,parallel::detectCores()))

Creating /home/rstudio/.R/Makevars

dotR ← file.path(Sys.getenv(“HOME”), “.R”)
if (!file.exists(dotR)) dir.create(dotR)
M ← file.path(dotR, “Makevars”)
if (!file.exists(M)) file.create(M)
cat(“\nCXX14FLAGS=-O3 -march=native -mtune=native -fPIC”,
file = M, sep = “\n”, append = TRUE)

I should also add that I’m actually running the r script using rpy2 from python as I need to use the AWS boto3 API for other features of the program. The instance is ephemeral, and as such, I’m not sure how to retrieve the makevars file.

Any help would be massively appreciated.


Can you try running your model with the preview version of rstan? You just need to change the rstan/StanHeaders installation step to:

install.packages("StanHeaders", repos = c("", getOption("repos")))
install.packages("rstan", repos = c("", getOption("repos")))

Thanks for the reply! When I switch to

Sys.setenv(MAKEFLAGS = paste0("-j",parallel::detectCores()))
install.packages("StanHeaders", repos = c("", getOption("repos")))
install.packages("rstan", repos = c("", getOption("repos")))

the container appears to build successfully, but rstan is no longer found when I try to load/require the library. ie:
“there is no package called ‘rstan’”

You’ll have to look at the output from the setup script, since the R package installation can fail and still allow the docker container to build successfully

Got it working!

You’re right, I think the stan installation was failing, but the container still looked like it had built.

I ran a container from my machine based on a rocker/r-ubuntu image and then manually loaded everything until it seemed kosher. Now I’m able to run stan code using fargate which is pretty convenient.