Stan and aws sagemaker

Hi all,

I wonder if anyone has played with stan (rstan/pystan/cmdstan*) on aws sagemaker. We are thinking about moving our stan related workflow there and would love to hear your experience!

Many thanks,
Andy

4 Likes

I’m not sure, but hopefully this will get more eyes on your question:

3 Likes

I’m going through the same situation.
The company where I work recently moved to AWS Sagemaker, giving us access to Jupyter Notebooks.
I’ve been trying to install cmdstan through cmdstanpy but still haven’t been able to avoid the “Need to set TBB_CXX_TYPE for non-standard compiler other than gcc or clang.”

Please share your experience in case you were able to install it well.

My colleague was able to install cmdstanpy on sagemaker STUDIO notebook earlier this year. We have since migrated away from aws but the following had worked for us.

os.environ['CMDSTAN'] = './cmdstan-2.23.0'

# check path
from cmdstanpy import CmdStanModel, cmdstan_path
cmdstan_path()

!pip install cmdstanpy
!conda install cmdstanpy

tgz_file = 'colab-cmdstan-2.23.0.tar.gz

tgz_url = 'https://github.com/stan- dev/cmdstan/releases/download/v2.23.0/colab-cmdstan-2.23.0.tar.gz'

urllib.request.urlretrieve(tgz_url, tgz_file)
shutil.unpack_archive(tgz_file)

2 Likes

Hello,

3 years later and I’m running into similar issues with running stan on SageMaker. Attempting the method this code outlines allows me to download the files into my sagemaker project, but when I attempt to run code depending on stan, I get the error:

ValueError: Failed to compile Stan model '/root/helper_files/stan_model.stan'. Console:

Command: ['make', 'STANCFLAGS+=--filename-in-msg=stan_model.stan', '/root/helper_files/stan_model']
failed with error [Errno 2] No such file or directory: 'make'

I was wondering if you or anyone had additional insights into getting Stan working in a Sagemaker instance.

1 Like

I’ve never tried with sagemaker, but we put this question out on twitter/x and @ajnafa suggested using a docker image:

2 Likes

Thank you Jonah and Jordan!

To add to this thread, here’s the solution that ended up working on my end - not quite a docker image, but the output could probably be built into one!

  • Start by making sure everything is up to date in your SageMaker instance.
!apt-get update
!apt-get install -y build-essential
  • Install cmdstanpy directly from github.
!pip install -e git+https://github.com/stan-dev/cmdstanpy#egg=cmdstanpy
  • Restart your kernel manually at this point. You can do that by clicking “Kernel → Restart Kernel” in the upper bar.

  • Import cmdstanpy, then use it to install cmdstan.
import cmdstanpy
cmdstanpy.install_cmdstan()

IMPORTANT: At least in my experience, the instance needs to be large enough to support the installation or it will fail. Attempting the install on a ml.t3.medium (2 vCPU + 4 GiB) instance failed. However, attempting the install on a ml.m5.large (2vCPU + 8GiB) instance worked.

Finally, the installation will take 10-15 minutes to do its thing. When it completes, it will leave a cmdstanpy folder in your sagemaker workspace. Sometimes, when you relaunch the instance, the install method will recognize this folder and skip the installation process. Sometimes it won’t. I haven’t yet figured out why this only happens some of the time - but hey, at least it works!

1 Like

Thank you for following up!

Hey…I am also very new to Stan and Pystan. I have been able to install Pystan locally and run a basic model in a Jupyter notebook inside Anaconda environment. But now I am trying to use it on a SageMaker (JupyterLab) notebook instance, but it is not working. I have done !pip install pystan, and it shows requirements are all satisfied, but then when I import pystan, it throws a ModuleNotFoundError. Would appreciate any suggestions on how to get around this.

Hi, @ishani28 and welcome to the Stan Forums.

I would suggest moving to CmdStanPy. It’s easier to install than PyStan and doesn’t require a server instance running as well. It’s also kept up to date with improvements in Stan.