|
| 1 | +# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +from sagemaker.estimator import Framework |
| 14 | +from sagemaker.fw_utils import create_image_uri, framework_name_from_image, framework_version_from_tag |
| 15 | +from sagemaker.pytorch.defaults import PYTORCH_VERSION, PYTHON_VERSION |
| 16 | +from sagemaker.pytorch.model import PyTorchModel |
| 17 | + |
| 18 | + |
| 19 | +class PyTorch(Framework): |
| 20 | + """Handle end-to-end training and deployment of custom PyTorch code.""" |
| 21 | + |
| 22 | + __framework_name__ = "pytorch" |
| 23 | + |
| 24 | + def __init__(self, entry_point, source_dir=None, hyperparameters=None, py_version=PYTHON_VERSION, |
| 25 | + framework_version=PYTORCH_VERSION, **kwargs): |
| 26 | + """ |
| 27 | + This ``Estimator`` executes an PyTorch script in a managed PyTorch execution environment, within a SageMaker |
| 28 | + Training Job. The managed PyTorch environment is an Amazon-built Docker container that executes functions |
| 29 | + defined in the supplied ``entry_point`` Python script. |
| 30 | +
|
| 31 | + Training is started by calling :meth:`~sagemaker.amazon.estimator.Framework.fit` on this Estimator. |
| 32 | + After training is complete, calling :meth:`~sagemaker.amazon.estimator.Framework.deploy` creates a |
| 33 | + hosted SageMaker endpoint and returns an :class:`~sagemaker.amazon.pytorch.model.PyTorchPredictor` instance |
| 34 | + that can be used to perform inference against the hosted model. |
| 35 | +
|
| 36 | + Technical documentation on preparing PyTorch scripts for SageMaker training and using the PyTorch Estimator is |
| 37 | + available on the project home-page: https://github.com/aws/sagemaker-python-sdk |
| 38 | +
|
| 39 | + Args: |
| 40 | + entry_point (str): Path (absolute or relative) to the Python source file which should be executed |
| 41 | + as the entry point to training. This should be compatible with either Python 2.7 or Python 3.5. |
| 42 | + source_dir (str): Path (absolute or relative) to a directory with any other training |
| 43 | + source code dependencies aside from tne entry point file (default: None). Structure within this |
| 44 | + directory are preserved when training on Amazon SageMaker. |
| 45 | + hyperparameters (dict): Hyperparameters that will be used for training (default: None). |
| 46 | + The hyperparameters are made accessible as a dict[str, str] to the training code on SageMaker. |
| 47 | + For convenience, this accepts other types for keys and values, but ``str()`` will be called |
| 48 | + to convert them before training. |
| 49 | + py_version (str): Python version you want to use for executing your model training code (default: 'py3'). |
| 50 | + One of 'py2' or 'py3'. |
| 51 | + framework_version (str): PyTorch version you want to use for executing your model training code. |
| 52 | + List of supported versions https://github.com/aws/sagemaker-python-sdk#pytorch-sagemaker-estimators |
| 53 | + **kwargs: Additional kwargs passed to the :class:`~sagemaker.estimator.Framework` constructor. |
| 54 | + """ |
| 55 | + super(PyTorch, self).__init__(entry_point, source_dir, hyperparameters, **kwargs) |
| 56 | + self.py_version = py_version |
| 57 | + self.framework_version = framework_version |
| 58 | + |
| 59 | + def train_image(self): |
| 60 | + """Return the Docker image to use for training. |
| 61 | +
|
| 62 | + The :meth:`~sagemaker.estimator.EstimatorBase.fit` method, which does the model training, calls this method to |
| 63 | + find the image to use for model training. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + str: The URI of the Docker image. |
| 67 | + """ |
| 68 | + return create_image_uri(self.sagemaker_session.boto_session.region_name, self.__framework_name__, |
| 69 | + self.train_instance_type, framework_version=self.framework_version, |
| 70 | + py_version=self.py_version) |
| 71 | + |
| 72 | + def create_model(self, model_server_workers=None): |
| 73 | + """Create a SageMaker ``PyTorchModel`` object that can be deployed to an ``Endpoint``. |
| 74 | +
|
| 75 | + Args: |
| 76 | + model_server_workers (int): Optional. The number of worker processes used by the inference server. |
| 77 | + If None, server will use one worker per vCPU. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + sagemaker.pytorch.model.PyTorchModel: A SageMaker ``PyTorchModel`` object. |
| 81 | + See :func:`~sagemaker.pytorch.model.PyTorchModel` for full details. |
| 82 | + """ |
| 83 | + return PyTorchModel(self.model_data, self.role, self.entry_point, source_dir=self.source_dir, |
| 84 | + enable_cloudwatch_metrics=self.enable_cloudwatch_metrics, name=self._current_job_name, |
| 85 | + container_log_level=self.container_log_level, code_location=self.code_location, |
| 86 | + py_version=self.py_version, framework_version=self.framework_version, |
| 87 | + model_server_workers=model_server_workers, sagemaker_session=self.sagemaker_session) |
| 88 | + |
| 89 | + @classmethod |
| 90 | + def _prepare_init_params_from_job_description(cls, job_details): |
| 91 | + """Convert the job description to init params that can be handled by the class constructor |
| 92 | +
|
| 93 | + Args: |
| 94 | + job_details: the returned job details from a describe_training_job API call. |
| 95 | +
|
| 96 | + Returns: |
| 97 | + dictionary: The transformed init_params |
| 98 | +
|
| 99 | + """ |
| 100 | + init_params = super(PyTorch, cls)._prepare_init_params_from_job_description(job_details) |
| 101 | + framework, py_version, tag = framework_name_from_image(init_params.pop('image')) |
| 102 | + |
| 103 | + init_params['py_version'] = py_version |
| 104 | + init_params['framework_version'] = framework_version_from_tag(tag) |
| 105 | + |
| 106 | + training_job_name = init_params['base_job_name'] |
| 107 | + |
| 108 | + if framework != cls.__framework_name__: |
| 109 | + raise ValueError("Training job: {} didn't use image for requested framework".format(training_job_name)) |
| 110 | + |
| 111 | + return init_params |
0 commit comments