- Notifications
You must be signed in to change notification settings - Fork1.2k
A library for training and deploying machine learning models on Amazon SageMaker
License
aws/sagemaker-python-sdk
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
SageMaker Python SDK is an open source library for training and deploying machine learning models on Amazon SageMaker.
With the SDK, you can train and deploy models using popular deep learning frameworksApache MXNet andPyTorch.You can also train and deploy models withAmazon algorithms,which are scalable implementations of core machine learning algorithms that are optimized for SageMaker and GPU training.If you haveyour own algorithms built into SageMaker compatible Docker containers, you can train and host models using these as well.
To install SageMaker Python SDK, seeInstalling SageMaker Python SDK.
Version 3.0.0 represents a significant milestone in our product's evolution. This major release introduces a modernized architecture, enhanced performance, and powerful new features while maintaining our commitment to user experience and reliability.
Important: Please review these breaking changes before upgrading.
- Older interfaces such as Estimator, Model, Predictor and all their subclasses will not be supported in V3.
- Please see ourV3 examples folder for example notebooks and usage patterns.
Upgrading to 3.x
To upgrade to the latest version of SageMaker Python SDK 3.x:
pip install --upgrade sagemaker
If you prefer to downgrade to the 2.x version:
pip install sagemaker==2.*
SeeSageMaker V2 Examples for V2 documentation and examples.
Key Benefits of 3.x
- Modular Architecture: Separate PyPI packages for core, training, and serving capabilities
- Unified Training & Inference: Single classes (ModelTrainer, ModelBuilder) replace multiple framework-specific classes
- Object-Oriented API: Structured interface with auto-generated configs aligned with AWS APIs
- Simplified Workflows: Reduced boilerplate and more intuitive interfaces
Training Experience
V3 introduces the unified ModelTrainer class to reduce complexity of initial setup and deployment for model training. This replaces the V2 Estimator class and framework-specific classes (PyTorchEstimator, SKLearnEstimator, etc.).
This example shows how to train a model using a custom training container with training data from S3.
SageMaker Python SDK 2.x:
fromsagemaker.estimatorimportEstimatorestimator=Estimator(image_uri="my-training-image",role="arn:aws:iam::123456789012:role/SageMakerRole",instance_count=1,instance_type="ml.m5.xlarge",output_path="s3://my-bucket/output")estimator.fit({"training":"s3://my-bucket/train"})
SageMaker Python SDK 3.x:
fromsagemaker.trainimportModelTrainerfromsagemaker.train.configsimportInputDatatrainer=ModelTrainer(training_image="my-training-image",role="arn:aws:iam::123456789012:role/SageMakerRole")train_data=InputData(channel_name="training",data_source="s3://my-bucket/train")trainer.train(input_data_config=[train_data])
See more examples:SageMaker V3 Examples
Inference Experience
V3 introduces the unified ModelBuilder class for model deployment and inference. This replaces the V2 Model class and framework-specific classes (PyTorchModel, TensorFlowModel, SKLearnModel, XGBoostModel, etc.).
This example shows how to deploy a trained model for real-time inference.
SageMaker Python SDK 2.x:
fromsagemaker.modelimportModelfromsagemaker.predictorimportPredictormodel=Model(image_uri="my-inference-image",model_data="s3://my-bucket/model.tar.gz",role="arn:aws:iam::123456789012:role/SageMakerRole")predictor=model.deploy(initial_instance_count=1,instance_type="ml.m5.xlarge")result=predictor.predict(data)
SageMaker Python SDK 3.x:
fromsagemaker.serveimportModelBuildermodel_builder=ModelBuilder(model="my-model",model_path="s3://my-bucket/model.tar.gz")endpoint=model_builder.build()result=endpoint.invoke(...)
See more examples:SageMaker V3 Examples
Training Examples
- Custom Distributed Training Example
- Distributed Local Training Example
- Hyperparameter Training Example
- JumpStart Training Example
- Local Training Example
Inference Examples
- HuggingFace Example
- In-Process Mode Example
- Inference Spec Example
- JumpStart E2E Training Example
- JumpStart Example
- Local Mode Example
- Optimize Example
- Train Inference E2E Example
ML Ops Examples
- V3 Hyperparameter Tuning Example
- V3 Hyperparameter Tuning Pipeline
- V3 Model Registry Example
- V3 PyTorch Processing Example
- V3 Pipeline Train Create Registry
- V3 Processing Job Sklearn
- V3 SageMaker Clarify
- V3 Transform Job Example
Looking for V2 Examples? SeeSageMaker V2 Examples below.
The SageMaker Python SDK is built to PyPI and the latest version of the SageMaker Python SDK can be installed with pip as follows
pip install sagemaker==<Latest version from pyPI from https://pypi.org/project/sagemaker/>
You can install from source by cloning this repository and running a pip install command in the root directory of the repository:
git clone https://github.com/aws/sagemaker-python-sdk.gitcd sagemaker-python-sdkpip install .
SageMaker Python SDK supports Unix/Linux and Mac.
SageMaker Python SDK is tested on:
- Python 3.9
- Python 3.10
- Python 3.11
- Python 3.12
Thesagemaker library has telemetry enabled to help us better understand user needs, diagnose issues, and deliver new features. This telemetry tracks the usage of various SageMaker functions.
If you prefer to opt out of telemetry, you can easily do so by setting theTelemetryOptOut parameter totrue in the SDK defaults configuration. For detailed instructions, please visitConfiguring and using defaults with the SageMaker Python SDK.
As a managed service, Amazon SageMaker performs operations on your behalf on the AWS hardware that is managed by Amazon SageMaker.Amazon SageMaker can perform only operations that the user permits.You can read more about which permissions are necessary in theAWS Documentation.
The SageMaker Python SDK should not require any additional permissions aside from what is required for using SageMaker.However, if you are using an IAM role with a path in it, you should grant permission foriam:GetRole.
SageMaker Python SDK is licensed under the Apache 2.0 License. It is copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. The license is available at:http://aws.amazon.com/apache2.0/
SageMaker Python SDK has unit tests and integration tests.
You can install the libraries needed to run the tests by runningpip install --upgrade .[test] or, for Zsh users:pip install --upgrade .\[test\]
Unit tests
We run unit tests with tox, which is a program that lets you run unit tests for multiple Python versions, and also make sure thecode fits our style guidelines. We run tox withall of our supported Python versions, so to run unit testswith the same configuration we do, you need to have interpreters for those Python versions installed.
To run the unit tests with tox, run:
tox tests/unit
Integration tests
To run the integration tests, the following prerequisites must be met
- AWS account credentials are available in the environment for the boto3 client to use.
- The AWS account has an IAM role named
SageMakerRole.It should have the AmazonSageMakerFullAccess policy attached as well as a policy withthe necessary permissions to use Elastic Inference. - To run remote_function tests, dummy ecr repo should be created. It can be created by running -
aws ecr create-repository --repository-name remote-function-dummy-container
We recommend selectively running just those integration tests you'd like to run. You can filter by individual test function names with:
tox -- -k 'test_i_care_about'
You can also run all of the integration tests by running the following command, which runs them in sequence, which may take a while:
tox -- tests/integ
You can also run them in parallel:
tox -- -n auto tests/integ
to enable all git hooks in the .githooks directory, run these commands in the repository directory:
find .git/hooks -type l -exec rm {} \;find .githooks -type f -exec ln -sf ../../{} .git/hooks/ \;To enable an individual git hook, simply move it from the .githooks/ directory to the .git/hooks/ directory.
Setup a Python environment, and install the dependencies listed indoc/requirements.txt:
# condaconda create -n sagemaker python=3.12conda activate sagemakerconda install sphinx=5.1.1 sphinx_rtd_theme=0.5.0# pippip install -r doc/requirements.txt
Clone/fork the repo, and install your local version:
pip install --upgrade .
Thencd into thesagemaker-python-sdk/doc directory and run:
make html
You can edit the templates for any of the pages in the docs by editing the .rst files in thedoc directory and then runningmake html again.
Preview the site with a Python web server:
cd _build/htmlpython -m http.server 8000
View the website by visitinghttp://localhost:8000
With SageMaker SparkML Serving, you can now perform predictions against a SparkML Model in SageMaker.In order to host a SparkML model in SageMaker, it should be serialized withMLeap library.
For more information on MLeap, seehttps://github.com/combust/mleap .
Supported major version of Spark: 3.3 (MLeap version - 0.20.0)
Here is an example on how to create an instance ofSparkMLModel class and usedeploy() method to create anendpoint which can be used to perform prediction against your trained SparkML Model.
sparkml_model=SparkMLModel(model_data='s3://path/to/model.tar.gz',env={'SAGEMAKER_SPARKML_SCHEMA':schema})model_name='sparkml-model'endpoint_name='sparkml-endpoint'predictor=sparkml_model.deploy(initial_instance_count=1,instance_type='ml.c4.xlarge',endpoint_name=endpoint_name)
Once the model is deployed, we can invoke the endpoint with aCSV payload like this:
payload='field_1,field_2,field_3,field_4,field_5'predictor.predict(payload)
For more information about the differentcontent-type andAccept formats as well as the structure of theschema that SageMaker SparkML Serving recognizes, please seeSageMaker SparkML Serving Container.
- Using the SageMaker Python SDK
- Using MXNet
- Using TensorFlow
- Using Chainer
- Using PyTorch
- Using Scikit-learn
- Using XGBoost
- SageMaker Reinforcement Learning Estimators
- SageMaker SparkML Serving
- Amazon SageMaker Built-in Algorithm Estimators
- Using SageMaker AlgorithmEstimators
- Consuming SageMaker Model Packages
- BYO Docker Containers with SageMaker Estimators
- SageMaker Automatic Model Tuning
- SageMaker Batch Transform
- Secure Training and Inference with VPC
- BYO Model
- Inference Pipelines
- Amazon SageMaker Operators in Apache Airflow
- SageMaker Autopilot
- Model Monitoring
- SageMaker Debugger
- SageMaker Processing
We're excited to announce model fine-tuning capabilities in SageMaker Python SDK V3!
What's New
Four new trainer classes for fine-tuning foundation models:
- SFTTrainer - Supervised fine-tuning
- DPOTrainer - Direct preference optimization
- RLAIFTrainer - RL from AI feedback
- RLVRTrainer - RL from verifiable rewards
Quick Example
fromsagemaker.trainimportSFTTrainerfromsagemaker.train.commonimportTrainingTypetrainer=SFTTrainer(model="meta-llama/Llama-2-7b-hf",training_type=TrainingType.LORA,model_package_group_name="my-models",training_dataset="s3://bucket/train.jsonl")training_job=trainer.train()
Key Features
- ✨ LoRA & full fine-tuning
- 📊 MLflow integration with real-time metrics
- 🚀 Deploy to SageMaker or Bedrock
- 📈 Built-in evaluation (11 benchmarks)
- ☁️ Serverless training
Get Started
pipinstallsagemaker>=3.1.0
About
A library for training and deploying machine learning models on Amazon SageMaker
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.