Amazon SageMaker Examples

repository·default·Indexed 27 days ago

https://github.com/aws/amazon-sagemaker-examples

Official Jupyter notebooks demonstrating how to build, train, and deploy machine learning models using Amazon SageMaker. Examples cover distributed training (SMDDP, Model Parallelism, Horovod, PyTorch FSDP), hyperparameter optimization (Hyperband, Warm Starting), and specific algorithms including XGBoost, DeepAR, BlazingText, and TabTransformer. Includes guides for custom algorithm packaging via Docker, Hugging Face and NeMo Megatron dataset preparation, and deploying models to AWS DeepLens.

Tokens
79.5K
Snippets
191
Records
315
Agent score
95%

What's inside amazon-sagemaker-examples

  1. Introduction to SageMaker-Core Python SDK

    default

    SageMaker-Core is a new Python SDK providing an object-oriented interface for interacting with Amazon SageMaker resources like TrainingJob, Model, and Endpoint.

    Key features include:

    • Resource Chaining: Pass resource objects directly as parameters to simplify code and eliminate manual parameter specification.
    • Abstraction: Handles low-level details such as resource state transitions and polling logic.
    • Developer Experience: Provides auto code completion, type hints, and comprehensive documentation.

    It is designed as an improvement over Boto3 for ML practitioners requiring full customization of AWS primitives.

  2. Overview of SageMaker Core Python SDK

    default
    SageMaker Core is an object-oriented Python SDK designed for interacting with Amazon SageMaker resources. It provides full parity with SageMaker APIs while offering an improved developer experience through dedicated resource classes, resource chaining, and automatic handling of resource state transitions and polling logic. It also incorporates intelligent defaults for parameters like IAM roles and VPC configurations.
  3. Deploy a T5 NLP model using NVIDIA Triton Inference Server on SageMaker

    default
    This example demonstrates how to deploy a HuggingFace T5-small PyTorch translation model using the NVIDIA Triton Inference Server on Amazon SageMaker. The model is served specifically using Triton's Python Backend. The workflow covers hosting multiple NLP models on a g5.2xlarge GPU behind a Multi-Model Endpoint (MME).
  4. Understand the GRPO Training Pipeline

    default

    The GRPO (Group Relative Policy Optimization) training process uses reinforcement learning with custom reward functions to improve model reasoning. The workflow consists of three main stages:

    1. Containerization: Building and pushing a Docker image containing the training logic, EasyR1/veRL framework, and custom reward functions to Amazon ECR using build_and_push.sh.
    2. Job Submission: Using grpo_training_sagemaker.py to submit a SageMaker Training Job that references the ECR image and provides S3 paths for models and datasets.
    3. Execution: The SageMaker job runs the container, which executes train_script.py. This script downloads data from S3, applies the train_config.yaml settings, and uses the reward_function/math.py to score model outputs during training.
  5. Quick Start for LLM Fine-tuning & Deployment Pipeline

    default

    This project provides a complete four-step pipeline for fine-tuning and deploying Large Language Models (LLM) on AWS SageMaker. The workflow consists of:

    1. SFT (Supervised Fine-Tuning): Uses LlamaFactory to teach the model to follow instructions using a base model and training data.
    2. GRPO (Group Relative Policy Optimization) Training: Uses EasyR1 to improve reasoning capabilities using the SFT model and a reward function.
    3. Model Deployment: Uses vLLM to host the GRPO model as a SageMaker API endpoint.
    4. Model Evaluation: Tests model quality using inference and test data.
  6. Explore Build and Train Model Examples

    default

    The build_and_train_models/ directory contains a collection of example notebooks demonstrating various Amazon SageMaker capabilities for building and training machine learning models. These examples cover a wide range of techniques including:

    • Distributed Training: Using SageMaker Distributed Data Parallelism (SMDDP), Model Parallelism, Horovod, and PyTorch FSDP (Fully Sharded Data Parallel).
    • Hyperparameter Optimization (HPO): Using SageMaker PyTorch containers, Hyperband, Warm Starting, and early stopping for automatic model tuning.
    • Algorithm Specifics: Examples for XGBoost, DeepAR, BlazingText, IP Insights, LDA, NTM, Object2Vec, Random Cut Forests, k-Nearest-Neighbors (kNN), LightGBM, CatBoost, Linear Learner, and TabTransformer.
    • Advanced Frameworks: Training and deploying models using JAX, and using SageMaker Training Compiler for Hugging Face Transformers.
    • Infrastructure & Deployment: Using Heterogeneous Clusters, Managed Spot Training, AWS Batch for SageMaker training jobs, and packaging models for the AWS Marketplace.
  7. Explore Amazon SageMaker Data Preparation Examples

    default

    The prepare_data directory contains example notebooks demonstrating various Amazon SageMaker capabilities for collecting, preprocessing, and organizing raw data for machine learning.

    Key capabilities covered include:

    • SageMaker Data Wrangler: Using the Data Prep Widget for visual data preparation.
    • SageMaker Feature Store: Introduction to Feature Store and using Feature Processors.
    • Amazon SageMaker Ground Truth:
      • Labeling workflows (Object Detection, Image Classification, 3D Point Cloud, Video).
      • Advanced workflows (Active Learning, Streaming Labeling Jobs, Annotation Consolidation).
      • Optimization (Using pre-trained models for labeling, RLHF for LLMs, and analyzing worker efficiency).
    • SageMaker Processing:
      • Distributed data processing using Apache Spark and Dask.
      • Feature transformation and general introduction to SageMaker Processing.
    • Amazon Augmented AI (A2I): Integration with AWS Marketplace ML models.
    • PySpark Integration: Performing PCA and K-Means clustering using PySpark within SageMaker.
  8. Choose an Amazon SageMaker interface

    default

    Amazon SageMaker provides several ways to interact with its services depending on your workflow and expertise level:

    • SageMaker Studio: A feature-rich IDE using the JupyterLab interface. Recommended for new users, teams, and companies as it provides seamless integration with deep learning/data science environments and scalable compute resources while allowing administrators to control data access and resource provisioning.
    • SageMaker Notebook Instances: Uses familiar Jupyter and JupyterLab interfaces. Best suited for single users or small teams where users also act as administrators.
    • Command Line & SDK: For advanced users, SageMaker can be used via the AWS CLI, the boto3 SDK, or the SageMaker Python SDK using Python scripts.
    • 3rd Party Integrations: Supports Kubeflow workflows and Kubernetes operators for training and inference.
  9. Explore Amazon SageMaker Deploy and Monitor examples

    default

    The deploy_and_monitor directory contains a collection of example notebooks demonstrating various Amazon SageMaker capabilities for deploying and monitoring machine learning models. Key topics covered include:

    • Model Deployment Strategies: ModelBuilder (IN_PROCESS mode), A/B testing, Asynchronous Inference, Serverless Inference, Multi-Model Endpoints (MME), and Multi-container endpoints.
    • Autoscaling: Application Autoscaling for real-time endpoints (including inference components) and Step Scaling.
    • Batch Processing: SageMaker Batch Transform for PyTorch, Torchserve, and general use cases.
    • Deployment Guardrails: Updating endpoints using linear traffic shifting, rolling deployment, and canary traffic shifting.
    • Monitoring and Explainability: SageMaker Model Monitor (Data Quality and Model Quality), Amazon SageMaker Clarify (Model Bias and Explainability), and Bring Your Own Container (BYOC) LLM monitoring.
    • Specialized Inference Servers: Using NVIDIA Triton on SageMaker with various backends (TensorRT, Torchserve, Python backend) and instance types (Inferentia).
    • Advanced Features: Inference Pipelines, Shadow Variant Experiments via API, and SageMaker Inference Recommender.