|
| 1 | +# Copyright 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 | +"""Configuration for the SageMaker Training Compiler.""" |
| 14 | +from __future__ import absolute_import |
| 15 | +import logging |
| 16 | +from typing import Union |
| 17 | +from packaging.specifiers import SpecifierSet |
| 18 | +from packaging.version import Version |
| 19 | + |
| 20 | +from sagemaker.training_compiler.config import TrainingCompilerConfig as BaseConfig |
| 21 | +from sagemaker.workflow.entities import PipelineVariable |
| 22 | + |
| 23 | +logger = logging.getLogger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +class TrainingCompilerConfig(BaseConfig): |
| 27 | + """The SageMaker Training Compiler configuration class.""" |
| 28 | + |
| 29 | + SUPPORTED_INSTANCE_CLASS_PREFIXES = ["p3", "p3dn", "g4dn", "p4d", "g5"] |
| 30 | + SUPPORTED_INSTANCE_TYPES_WITH_EFA = [ |
| 31 | + "ml.g4dn.8xlarge", |
| 32 | + "ml.g4dn.12xlarge", |
| 33 | + "ml.g5.48xlarge", |
| 34 | + "ml.p3dn.24xlarge", |
| 35 | + "ml.p4d.24xlarge", |
| 36 | + ] |
| 37 | + |
| 38 | + def __init__( |
| 39 | + self, |
| 40 | + enabled: Union[bool, PipelineVariable] = True, |
| 41 | + debug: Union[bool, PipelineVariable] = False, |
| 42 | + ): |
| 43 | + """This class initializes a ``TrainingCompilerConfig`` instance. |
| 44 | +
|
| 45 | + `Amazon SageMaker Training Compiler |
| 46 | + <https://docs.aws.amazon.com/sagemaker/latest/dg/training-compiler.html>`_ |
| 47 | + is a feature of SageMaker Training |
| 48 | + and speeds up training jobs by optimizing model execution graphs. |
| 49 | +
|
| 50 | + You can compile PyTorch models |
| 51 | + by passing the object of this configuration class to the ``compiler_config`` |
| 52 | + parameter of the :class:`~sagemaker.pytorch.PyTorch` |
| 53 | + estimator. |
| 54 | +
|
| 55 | + Args: |
| 56 | + enabled (bool or PipelineVariable): Optional. Switch to enable SageMaker |
| 57 | + Training Compiler. The default is ``True``. |
| 58 | + debug (bool or PipelineVariable): Optional. Whether to dump detailed logs |
| 59 | + for debugging. This comes with a potential performance slowdown. |
| 60 | + The default is ``False``. |
| 61 | +
|
| 62 | + **Example**: The following code shows the basic usage of the |
| 63 | + :class:`sagemaker.pytorch.TrainingCompilerConfig()` class |
| 64 | + to run a PyTorch training job with the compiler. |
| 65 | +
|
| 66 | + .. code-block:: python |
| 67 | +
|
| 68 | + from sagemaker.pytorch import PyTorch, TrainingCompilerConfig |
| 69 | +
|
| 70 | + pytorch_estimator=PyTorch( |
| 71 | + ... |
| 72 | + compiler_config=TrainingCompilerConfig() |
| 73 | + ) |
| 74 | +
|
| 75 | + .. seealso:: |
| 76 | +
|
| 77 | + For more information about how to enable SageMaker Training Compiler |
| 78 | + for various training settings such as distributed training, |
| 79 | + see `Enable SageMaker Training Compiler |
| 80 | + <https://docs.aws.amazon.com/sagemaker/latest/dg/training-compiler-enable.html>`_ |
| 81 | + in the `Amazon SageMaker Training Compiler developer guide |
| 82 | + <https://docs.aws.amazon.com/sagemaker/latest/dg/training-compiler.html>`_. |
| 83 | +
|
| 84 | + """ |
| 85 | + |
| 86 | + super(TrainingCompilerConfig, self).__init__(enabled=enabled, debug=debug) |
| 87 | + |
| 88 | + @classmethod |
| 89 | + def validate( |
| 90 | + cls, |
| 91 | + estimator, |
| 92 | + ): |
| 93 | + """Checks if SageMaker Training Compiler is configured correctly. |
| 94 | +
|
| 95 | + Args: |
| 96 | + estimator (:class:`sagemaker.pytorch.PyTorch`): An estimator object. |
| 97 | + If SageMaker Training Compiler is enabled, it will validate whether |
| 98 | + the estimator is configured to be compatible with Training Compiler. |
| 99 | +
|
| 100 | + Raises: |
| 101 | + ValueError: Raised if the requested configuration is not compatible |
| 102 | + with SageMaker Training Compiler. |
| 103 | + """ |
| 104 | + |
| 105 | + super(TrainingCompilerConfig, cls).validate(estimator) |
| 106 | + |
| 107 | + if estimator.image_uri: |
| 108 | + error_helper_string = ( |
| 109 | + "Overriding the image URI is currently not supported " |
| 110 | + "for SageMaker Training Compiler." |
| 111 | + "Specify the following parameters to run the PyTorch training job " |
| 112 | + "with SageMaker Training Compiler enabled: " |
| 113 | + "framework_version, and compiler_config." |
| 114 | + ) |
| 115 | + raise ValueError(error_helper_string) |
| 116 | + |
| 117 | + if estimator.distribution: |
| 118 | + pt_xla_present = "pytorchxla" in estimator.distribution |
| 119 | + pt_xla_enabled = estimator.distribution.get("pytorchxla", {}).get("enabled", False) |
| 120 | + if pt_xla_enabled: |
| 121 | + if estimator.framework_version: |
| 122 | + if Version(estimator.framework_version) in SpecifierSet("< 1.12"): |
| 123 | + error_helper_string = ( |
| 124 | + "Distribution mechanism 'pytorchxla' is currently only supported for " |
| 125 | + "PyTorch >= 1.12 when SageMaker Training Compiler is enabled." |
| 126 | + " Received framework_version={} which is unsupported." |
| 127 | + ) |
| 128 | + raise ValueError(error_helper_string.format(estimator.framework_version)) |
| 129 | + if estimator.instance_type not in cls.SUPPORTED_INSTANCE_TYPES_WITH_EFA: |
| 130 | + logger.warning( |
| 131 | + "Consider using instances with EFA support when " |
| 132 | + "training with PyTorch >= 1.12 and SageMaker Training Compiler " |
| 133 | + "enabled. SageMaker Training Compiler leverages EFA to provide better " |
| 134 | + "performance for distributed training." |
| 135 | + ) |
| 136 | + if not pt_xla_present: |
| 137 | + if estimator.framework_version: |
| 138 | + if Version(estimator.framework_version) in SpecifierSet(">= 1.12"): |
| 139 | + error_helper_string = ( |
| 140 | + "'pytorchxla' is the only distribution mechanism currently supported " |
| 141 | + "for PyTorch >= 1.12 when SageMaker Training Compiler is enabled." |
| 142 | + " Received distribution={} which is unsupported." |
| 143 | + ) |
| 144 | + raise ValueError(error_helper_string.format(estimator.distribution)) |
| 145 | + elif estimator.instance_count and estimator.instance_count > 1: |
| 146 | + if estimator.framework_version: |
| 147 | + if Version(estimator.framework_version) in SpecifierSet(">= 1.12"): |
| 148 | + logger.warning( |
| 149 | + "Consider setting 'distribution' to 'pytorchxla' for distributed " |
| 150 | + "training with PyTorch >= 1.12 and SageMaker Training Compiler enabled." |
| 151 | + ) |
0 commit comments