sagemaker-training


Namesagemaker-training JSON
Version 4.8.0 PyPI version JSON
download
home_pagehttps://github.com/aws/sagemaker-training-toolkit/
SummaryOpen source library for creating containers to run on Amazon SageMaker.
upload_time2024-08-15 00:08:42
maintainerNone
docs_urlNone
authorAmazon Web Services
requires_pythonNone
licenseApache License 2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![SageMaker](https://github.com/aws/sagemaker-training-toolkit/raw/master/branding/icon/sagemaker-banner.png)

# SageMaker Training Toolkit

[![Latest Version](https://img.shields.io/pypi/v/sagemaker-training.svg)](https://pypi.python.org/pypi/sagemaker-training) [![Supported Python Versions](https://img.shields.io/pypi/pyversions/sagemaker-training.svg)](https://pypi.python.org/pypi/sagemaker-training) [![Code Style: Black](https://img.shields.io/badge/code_style-black-000000.svg)](https://github.com/python/black)

Train machine learning models within a Docker container using Amazon SageMaker.


## :books: Background

[Amazon SageMaker](https://aws.amazon.com/sagemaker/) is a fully managed service for data science and machine learning (ML) workflows.
You can use Amazon SageMaker to simplify the process of building, training, and deploying ML models.

To train a model, you can include your training script and dependencies in a [Docker container](https://www.docker.com/resources/what-container) that runs your training code.
A container provides an effectively isolated environment, ensuring a consistent runtime and reliable training process. 

The **SageMaker Training Toolkit** can be easily added to any Docker container, making it compatible with SageMaker for [training models](https://aws.amazon.com/sagemaker/train/).
If you use a [prebuilt SageMaker Docker image for training](https://docs.aws.amazon.com/sagemaker/latest/dg/pre-built-containers-frameworks-deep-learning.html), this library may already be included.

For more information, see the Amazon SageMaker Developer Guide sections on [using Docker containers for training](https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms.html).

## :hammer_and_wrench: Installation

To install this library in your Docker image, add the following line to your [Dockerfile](https://docs.docker.com/engine/reference/builder/):

``` dockerfile
RUN pip3 install sagemaker-training
```

## :computer: Usage

The following are brief how-to guides.
For complete, working examples of custom training containers built with the SageMaker Training Toolkit, please see [the example notebooks](https://github.com/awslabs/amazon-sagemaker-examples/tree/master/advanced_functionality/custom-training-containers).

### Create a Docker image and train a model

1. Write a training script (eg. `train.py`).

2. [Define a container with a Dockerfile](https://docs.docker.com/get-started/part2/#define-a-container-with-dockerfile) that includes the training script and any dependencies.

    The training script must be located in the `/opt/ml/code` directory.
    The environment variable `SAGEMAKER_PROGRAM` defines which file inside the `/opt/ml/code` directory to use as the training entry point.
    When training starts, the interpreter executes the entry point defined by `SAGEMAKER_PROGRAM`.
    Python and shell scripts are both supported.
    
    ``` docker
    FROM yourbaseimage:tag
  
    # install the SageMaker Training Toolkit 
    RUN pip3 install sagemaker-training

    # copy the training script inside the container
    COPY train.py /opt/ml/code/train.py

    # define train.py as the script entry point
    ENV SAGEMAKER_PROGRAM train.py
    ```

3. Build and tag the Docker image.

    ``` shell
    docker build -t custom-training-container .
    ```

4. Use the Docker image to start a training job using the [SageMaker Python SDK](https://github.com/aws/sagemaker-python-sdk).

    ``` python
    from sagemaker.estimator import Estimator

    estimator = Estimator(image_name="custom-training-container",
                          role="SageMakerRole",
                          train_instance_count=1,
                          train_instance_type="local")

    estimator.fit()
    ```
    
    To train a model using the image on SageMaker, [push the image to ECR](https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-ecr-image.html) and start a SageMaker training job with the image URI.
    

### Pass arguments to the entry point using hyperparameters

Any hyperparameters provided by the training job are passed to the entry point as script arguments.
The SageMaker Python SDK uses this feature to pass special hyperparameters to the training job, including `sagemaker_program` and `sagemaker_submit_directory`.
The complete list of SageMaker hyperparameters is available [here](https://github.com/aws/sagemaker-training-toolkit/blob/master/src/sagemaker_training/params.py).

1. Implement an argument parser in the entry point script. For example, in a Python script:

    ``` python
    import argparse

    if __name__ == "__main__":
      parser = argparse.ArgumentParser()

      parser.add_argument("--learning-rate", type=int, default=1)
      parser.add_argument("--batch-size", type=int, default=64)
      parser.add_argument("--communicator", type=str)
      parser.add_argument("--frequency", type=int, default=20)

      args = parser.parse_args()
      ...
    ```

2. Start a training job with hyperparameters.

    ``` python
    {"HyperParameters": {"batch-size": 256, "learning-rate": 0.0001, "communicator": "pure_nccl"}}
    ```

### Read additional information using environment variables

An entry point often needs additional information not available in `hyperparameters`.
The SageMaker Training Toolkit writes this information as environment variables that are available from within the script.
For example, this training job includes the channels `training` and `testing`:

``` python
from sagemaker.pytorch import PyTorch

estimator = PyTorch(entry_point="train.py", ...)

estimator.fit({"training": "s3://bucket/path/to/training/data", 
               "testing": "s3://bucket/path/to/testing/data"})
```

The environment variables `SM_CHANNEL_TRAINING` and `SM_CHANNEL_TESTING` provide the paths to the channels:

``` python
import argparse
import os

if __name__ == "__main__":
  parser = argparse.ArgumentParser()

  ...

  # reads input channels training and testing from the environment variables
  parser.add_argument("--training", type=str, default=os.environ["SM_CHANNEL_TRAINING"])
  parser.add_argument("--testing", type=str, default=os.environ["SM_CHANNEL_TESTING"])

  args = parser.parse_args()

  ...
```

When training starts, SageMaker Training Toolkit will print all available environment variables. Please see the [reference on environment variables](https://github.com/aws/sagemaker-training-toolkit/blob/master/ENVIRONMENT_VARIABLES.md) for a full list of provided environment variables.

### Get information about the container environment

To get information about the container environment, initialize an `Environment` object.
`Environment` provides access to aspects of the environment relevant to training jobs, including hyperparameters, system characteristics, filesystem locations, environment variables and configuration settings.
It is a read-only snapshot of the container environment during training, and it doesn't contain any form of state.

``` python
from sagemaker_training import environment

env = environment.Environment()

# get the path of the channel "training" from the `inputdataconfig.json` file
training_dir = env.channel_input_dirs["training"]

# get a the hyperparameter "training_data_file" from `hyperparameters.json` file
file_name = env.hyperparameters["training_data_file"]

# get the folder where the model should be saved
model_dir = env.model_dir

# train the model
data = np.load(os.path.join(training_dir, file_name))
x_train, y_train = data["features"], keras.utils.to_categorical(data["labels"])
model = ResNet50(weights="imagenet")
...
model.fit(x_train, y_train)

#save the model to the model_dir at the end of training
model.save(os.path.join(model_dir, "saved_model"))
```

### Execute the entry point

To execute the entry point, call `entry_point.run()`.

``` python
from sagemaker_training import entry_point, environment

env = environment.Environment()

# read hyperparameters as script arguments
args = env.to_cmd_args()

# get the environment variables
env_vars = env.to_env_vars()

# execute the entry point
entry_point.run(uri=env.module_dir,
                user_entry_point=env.user_entry_point,
                args=args,
                env_vars=env_vars)

```

If the entry point execution fails, `trainer.train()` will write the error message to `/opt/ml/output/failure`. Otherwise, it will write to the file `/opt/ml/success`.

## :scroll: License

This library is licensed under the [Apache 2.0 License](http://aws.amazon.com/apache2.0/).
For more details, please take a look at the [LICENSE](https://github.com/aws/sagemaker-training-toolkit/blob/master/LICENSE) file.

## :handshake: Contributing

Contributions are welcome!
Please read our [contributing guidelines](https://github.com/aws/sagemaker-training-toolkit/blob/master/CONTRIBUTING.md)
if you'd like to open an issue or submit a pull request.
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/aws/sagemaker-training-toolkit/",
    "name": "sagemaker-training",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "Amazon Web Services",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/ad/c1/6fca1c700c1cb76a945e5919c596a703fab9f7c1f4fab1cabe0a5788bcb5/sagemaker_training-4.8.0.tar.gz",
    "platform": null,
    "description": "![SageMaker](https://github.com/aws/sagemaker-training-toolkit/raw/master/branding/icon/sagemaker-banner.png)\n\n# SageMaker Training Toolkit\n\n[![Latest Version](https://img.shields.io/pypi/v/sagemaker-training.svg)](https://pypi.python.org/pypi/sagemaker-training) [![Supported Python Versions](https://img.shields.io/pypi/pyversions/sagemaker-training.svg)](https://pypi.python.org/pypi/sagemaker-training) [![Code Style: Black](https://img.shields.io/badge/code_style-black-000000.svg)](https://github.com/python/black)\n\nTrain machine learning models within a Docker container using Amazon SageMaker.\n\n\n## :books: Background\n\n[Amazon SageMaker](https://aws.amazon.com/sagemaker/) is a fully managed service for data science and machine learning (ML) workflows.\nYou can use Amazon SageMaker to simplify the process of building, training, and deploying ML models.\n\nTo train a model, you can include your training script and dependencies in a [Docker container](https://www.docker.com/resources/what-container) that runs your training code.\nA container provides an effectively isolated environment, ensuring a consistent runtime and reliable training process. \n\nThe **SageMaker Training Toolkit** can be easily added to any Docker container, making it compatible with SageMaker for [training models](https://aws.amazon.com/sagemaker/train/).\nIf you use a [prebuilt SageMaker Docker image for training](https://docs.aws.amazon.com/sagemaker/latest/dg/pre-built-containers-frameworks-deep-learning.html), this library may already be included.\n\nFor more information, see the Amazon SageMaker Developer Guide sections on [using Docker containers for training](https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms.html).\n\n## :hammer_and_wrench: Installation\n\nTo install this library in your Docker image, add the following line to your [Dockerfile](https://docs.docker.com/engine/reference/builder/):\n\n``` dockerfile\nRUN pip3 install sagemaker-training\n```\n\n## :computer: Usage\n\nThe following are brief how-to guides.\nFor complete, working examples of custom training containers built with the SageMaker Training Toolkit, please see [the example notebooks](https://github.com/awslabs/amazon-sagemaker-examples/tree/master/advanced_functionality/custom-training-containers).\n\n### Create a Docker image and train a model\n\n1. Write a training script (eg. `train.py`).\n\n2. [Define a container with a Dockerfile](https://docs.docker.com/get-started/part2/#define-a-container-with-dockerfile) that includes the training script and any dependencies.\n\n    The training script must be located in the `/opt/ml/code` directory.\n    The environment variable `SAGEMAKER_PROGRAM` defines which file inside the `/opt/ml/code` directory to use as the training entry point.\n    When training starts, the interpreter executes the entry point defined by `SAGEMAKER_PROGRAM`.\n    Python and shell scripts are both supported.\n    \n    ``` docker\n    FROM yourbaseimage:tag\n  \n    # install the SageMaker Training Toolkit \n    RUN pip3 install sagemaker-training\n\n    # copy the training script inside the container\n    COPY train.py /opt/ml/code/train.py\n\n    # define train.py as the script entry point\n    ENV SAGEMAKER_PROGRAM train.py\n    ```\n\n3. Build and tag the Docker image.\n\n    ``` shell\n    docker build -t custom-training-container .\n    ```\n\n4. Use the Docker image to start a training job using the [SageMaker Python SDK](https://github.com/aws/sagemaker-python-sdk).\n\n    ``` python\n    from sagemaker.estimator import Estimator\n\n    estimator = Estimator(image_name=\"custom-training-container\",\n                          role=\"SageMakerRole\",\n                          train_instance_count=1,\n                          train_instance_type=\"local\")\n\n    estimator.fit()\n    ```\n    \n    To train a model using the image on SageMaker, [push the image to ECR](https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-ecr-image.html) and start a SageMaker training job with the image URI.\n    \n\n### Pass arguments to the entry point using hyperparameters\n\nAny hyperparameters provided by the training job are passed to the entry point as script arguments.\nThe SageMaker Python SDK uses this feature to pass special hyperparameters to the training job, including `sagemaker_program` and `sagemaker_submit_directory`.\nThe complete list of SageMaker hyperparameters is available [here](https://github.com/aws/sagemaker-training-toolkit/blob/master/src/sagemaker_training/params.py).\n\n1. Implement an argument parser in the entry point script. For example, in a Python script:\n\n    ``` python\n    import argparse\n\n    if __name__ == \"__main__\":\n      parser = argparse.ArgumentParser()\n\n      parser.add_argument(\"--learning-rate\", type=int, default=1)\n      parser.add_argument(\"--batch-size\", type=int, default=64)\n      parser.add_argument(\"--communicator\", type=str)\n      parser.add_argument(\"--frequency\", type=int, default=20)\n\n      args = parser.parse_args()\n      ...\n    ```\n\n2. Start a training job with hyperparameters.\n\n    ``` python\n    {\"HyperParameters\": {\"batch-size\": 256, \"learning-rate\": 0.0001, \"communicator\": \"pure_nccl\"}}\n    ```\n\n### Read additional information using environment variables\n\nAn entry point often needs additional information not available in `hyperparameters`.\nThe SageMaker Training Toolkit writes this information as environment variables that are available from within the script.\nFor example, this training job includes the channels `training` and `testing`:\n\n``` python\nfrom sagemaker.pytorch import PyTorch\n\nestimator = PyTorch(entry_point=\"train.py\", ...)\n\nestimator.fit({\"training\": \"s3://bucket/path/to/training/data\", \n               \"testing\": \"s3://bucket/path/to/testing/data\"})\n```\n\nThe environment variables `SM_CHANNEL_TRAINING` and `SM_CHANNEL_TESTING` provide the paths to the channels:\n\n``` python\nimport argparse\nimport os\n\nif __name__ == \"__main__\":\n  parser = argparse.ArgumentParser()\n\n  ...\n\n  # reads input channels training and testing from the environment variables\n  parser.add_argument(\"--training\", type=str, default=os.environ[\"SM_CHANNEL_TRAINING\"])\n  parser.add_argument(\"--testing\", type=str, default=os.environ[\"SM_CHANNEL_TESTING\"])\n\n  args = parser.parse_args()\n\n  ...\n```\n\nWhen training starts, SageMaker Training Toolkit will print all available environment variables. Please see the [reference on environment variables](https://github.com/aws/sagemaker-training-toolkit/blob/master/ENVIRONMENT_VARIABLES.md) for a full list of provided environment variables.\n\n### Get information about the container environment\n\nTo get information about the container environment, initialize an `Environment` object.\n`Environment` provides access to aspects of the environment relevant to training jobs, including hyperparameters, system characteristics, filesystem locations, environment variables and configuration settings.\nIt is a read-only snapshot of the container environment during training, and it doesn't contain any form of state.\n\n``` python\nfrom sagemaker_training import environment\n\nenv = environment.Environment()\n\n# get the path of the channel \"training\" from the `inputdataconfig.json` file\ntraining_dir = env.channel_input_dirs[\"training\"]\n\n# get a the hyperparameter \"training_data_file\" from `hyperparameters.json` file\nfile_name = env.hyperparameters[\"training_data_file\"]\n\n# get the folder where the model should be saved\nmodel_dir = env.model_dir\n\n# train the model\ndata = np.load(os.path.join(training_dir, file_name))\nx_train, y_train = data[\"features\"], keras.utils.to_categorical(data[\"labels\"])\nmodel = ResNet50(weights=\"imagenet\")\n...\nmodel.fit(x_train, y_train)\n\n#save the model to the model_dir at the end of training\nmodel.save(os.path.join(model_dir, \"saved_model\"))\n```\n\n### Execute the entry point\n\nTo execute the entry point, call `entry_point.run()`.\n\n``` python\nfrom sagemaker_training import entry_point, environment\n\nenv = environment.Environment()\n\n# read hyperparameters as script arguments\nargs = env.to_cmd_args()\n\n# get the environment variables\nenv_vars = env.to_env_vars()\n\n# execute the entry point\nentry_point.run(uri=env.module_dir,\n                user_entry_point=env.user_entry_point,\n                args=args,\n                env_vars=env_vars)\n\n```\n\nIf the entry point execution fails, `trainer.train()` will write the error message to `/opt/ml/output/failure`. Otherwise, it will write to the file `/opt/ml/success`.\n\n## :scroll: License\n\nThis library is licensed under the [Apache 2.0 License](http://aws.amazon.com/apache2.0/).\nFor more details, please take a look at the [LICENSE](https://github.com/aws/sagemaker-training-toolkit/blob/master/LICENSE) file.\n\n## :handshake: Contributing\n\nContributions are welcome!\nPlease read our [contributing guidelines](https://github.com/aws/sagemaker-training-toolkit/blob/master/CONTRIBUTING.md)\nif you'd like to open an issue or submit a pull request.",
    "bugtrack_url": null,
    "license": "Apache License 2.0",
    "summary": "Open source library for creating containers to run on Amazon SageMaker.",
    "version": "4.8.0",
    "project_urls": {
        "Homepage": "https://github.com/aws/sagemaker-training-toolkit/"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "adc16fca1c700c1cb76a945e5919c596a703fab9f7c1f4fab1cabe0a5788bcb5",
                "md5": "0fc3a7b661ed3b60ba26824726cf7275",
                "sha256": "54d1f9eba8d667d01c451dc66d4818ca16d6d30f241dead64b4a8ea5589f60da"
            },
            "downloads": -1,
            "filename": "sagemaker_training-4.8.0.tar.gz",
            "has_sig": false,
            "md5_digest": "0fc3a7b661ed3b60ba26824726cf7275",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 60678,
            "upload_time": "2024-08-15T00:08:42",
            "upload_time_iso_8601": "2024-08-15T00:08:42.298780Z",
            "url": "https://files.pythonhosted.org/packages/ad/c1/6fca1c700c1cb76a945e5919c596a703fab9f7c1f4fab1cabe0a5788bcb5/sagemaker_training-4.8.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-15 00:08:42",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "aws",
    "github_project": "sagemaker-training-toolkit",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "sagemaker-training"
}
        
Elapsed time: 0.69165s