aws-samples / amazon-sagemaker-local-mode

Amazon SageMaker Local Mode Examples
MIT No Attribution
246 stars 61 forks source link
amazon-sagemaker catboost dask delta-lake gensim-word2vec hdbscan-clustering-algorithm huggingface huggingface-transformers lightgbm machine-learning prophet prophet-model pycharm pytorch pytorch-training sagemaker sagemaker-processing scikit-learn tensorflow tensorflow-training

Amazon SageMaker Local Mode Examples

AWS ML Docker Local Machine

This repository contains examples and related resources showing you how to preprocess, train, debug your training script with breakpoints, and serve on your local machine using Amazon SageMaker Local mode for processing jobs, training and serving.

SageMaker Studio now supports local mode and Docker: https://aws.amazon.com/about-aws/whats-new/2023/12/sagemaker-studio-local-mode-docker/

Read the Blog Post: Accelerate ML workflows with Amazon SageMaker Studio Local Mode and Docker support

Overview

The local mode in the Amazon SageMaker Python SDK can emulate CPU (single and multi-instance) and GPU (single instance) SageMaker training jobs by changing a single argument in the TensorFlow, PyTorch or MXNet estimators. To do this, it uses Docker compose and NVIDIA Docker. It will also pull the Amazon SageMaker TensorFlow, PyTorch or MXNet containers from Amazon ECS, so you’ll need to be able to access a public Amazon ECR repository from your local environment.

For full details on how this works:

SageMaker local mode training and serving in PyCharm/Visual Studio Code

This repository examples will work in any IDE on your local machine.

Here you can see a TensorFlow example running on PyCharm. The data for processing, training and serving is also located on your local machine file system.

SageMaker local mode training in PyCharm

SageMaker local mode training in PyCharm

SageMaker local mode serving in PyCharm

SageMaker local mode serving in PyCharm

SageMaker local mode processing jobs in PyCharm

SageMaker local mode processing jobs in PyCharm

Debugging your training script running SageMaker local mode training in PyCharm

Debug your application

Repository Structure

The repository contains the following resources:

Note: Those examples were tested on macOS, Linux and Windows.

Prerequisites

  1. Create an AWS account if you do not already have one and login.

  2. Mac users: Install Docker Desktop for Mac

  3. Windows users: Install Docker Desktop for Windows, WSL 2 and then, a Linux distro from the Microsoft Store. You can follow the instructions here.

  4. Install the AWS CLI and Configure AWS credentials.

  5. Make sure you have Python3 installed python --version

  6. Make sure you have pip installed pip --version

Installation Instructions for PyCharm

We assume the root folder of the project is ~/Documents/dev/

  1. Open Terminal and navigate to the project root folder: cd ~/Documents/dev/

  2. Create a directory for the GitHub projects: mkdir GitHub

  3. Navigate to ~/Documents/dev/GitHub/: cd GitHub

  4. Clone the repo onto your local development machine using git clone https://github.com/aws-samples/amazon-sagemaker-local-mode

  5. Open PyCharm

  6. Open amazon-sagemaker-local-mode project from ~/Documents/dev/GitHub/amazon-sagemaker-local-mode/

  7. Now you will add a new virtual environment and install the required Python dependencies to run the demos.

  8. Navigate to PyCharm -> Preferences -> Python Interpreter, and click "Add" Add new Interpreter

  9. Add a new Virtual environment by specifying the location of the virtual environment to be created: /Users/<MY USER>/Documents/dev/sagemaker-python-sdk Add new venv

  10. Click OK

  11. On the Python Interpreter Screen, click OK. Final add venv

  12. Open terminal (inside PyCharm) and install the requirements: python -m pip install -r requirements.txt install requirements

  13. Once completed, navigate to tensorflow_script_mode_california_housing_local_training_and_serving folder, and double click on tensorflow_script_mode_california_housing_local_training_and_serving.py file open tf example

  14. Right click -> Run tensorflow_script_mode_california_housing_local_training_and_serving.py run tf example

  15. The container image download might take a few minutes to complete, but eventually you will View the output view tf output

Installation Instructions for SageMaker Notebook instances

  1. Create a new SageMaker Notebook instance. Choose ml.c5.xlarge and set volume size to 64 GB. This is required since SageMaker Dokcer images that are fetched are few GB in size
    Create SageMaker Local Notebook

  2. Open JupyterLab, and from there open terminal: File -> New -> Terminal Open Terminal

  3. List Conda environments by running conda env list List conda envs We will use python3 kernel.

  4. Activate specific Conda environment by running the following commands: source ~/anaconda3/etc/profile.d/conda.sh and then conda activate python3 Activate specific Conda environment

  5. Clone the GitHub repo by running

    • cd SageMaker
    • git clone https://github.com/aws-samples/amazon-sagemaker-local-mode
  6. Create virtual environment and then install dependencies:

    • cd amazon-sagemaker-local-mode
    • python -m venv venv
    • source venv/bin/activate
    • pip install -r requirements.txt
  7. Test the environment

    • cd scikit_learn_script_mode_local_training_and_serving
    • python scikit_learn_script_mode_local_training_and_serving.py you should see Login Succeeded message. It will take few minutes to download the Docker image so be patient. Start run python
  8. Once script is completed, you should see Predictions, Actual and RMSE Inference Success

  9. Press Control-C to exit the program.

Questions?

Please contact Eitan Sela on LinkedIn or raise an issue on this repo.

License

This library is licensed under the MIT-0 License. See the LICENSE file.