|
| 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 | + |
| 17 | +from sagemaker.training_compiler.config import TrainingCompilerConfig as BaseConfig |
| 18 | + |
| 19 | +logger = logging.getLogger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +class TrainingCompilerConfig(BaseConfig): |
| 23 | + """The SageMaker Training Compiler configuration class.""" |
| 24 | + |
| 25 | + SUPPORTED_INSTANCE_CLASS_PREFIXES = ["p3", "g4dn", "p4"] |
| 26 | + |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + enabled=True, |
| 30 | + debug=False, |
| 31 | + ): |
| 32 | + """This class initializes a ``TrainingCompilerConfig`` instance. |
| 33 | +
|
| 34 | + `Amazon SageMaker Training Compiler |
| 35 | + <https://docs.aws.amazon.com/sagemaker/latest/dg/training-compiler.html>`_ |
| 36 | + is a feature of SageMaker Training |
| 37 | + and speeds up training jobs by optimizing model execution graphs. |
| 38 | +
|
| 39 | + You can compile Hugging Face models |
| 40 | + by passing the object of this configuration class to the ``compiler_config`` |
| 41 | + parameter of the :class:`~sagemaker.huggingface.HuggingFace` |
| 42 | + estimator. |
| 43 | +
|
| 44 | + Args: |
| 45 | + enabled (bool): Optional. Switch to enable SageMaker Training Compiler. |
| 46 | + The default is ``True``. |
| 47 | + debug (bool): Optional. Whether to dump detailed logs for debugging. |
| 48 | + This comes with a potential performance slowdown. |
| 49 | + The default is ``False``. |
| 50 | +
|
| 51 | + **Example**: The following code shows the basic usage of the |
| 52 | + :class:`sagemaker.huggingface.TrainingCompilerConfig()` class |
| 53 | + to run a HuggingFace training job with the compiler. |
| 54 | +
|
| 55 | + .. code-block:: python |
| 56 | +
|
| 57 | + from sagemaker.huggingface import HuggingFace, TrainingCompilerConfig |
| 58 | +
|
| 59 | + huggingface_estimator=HuggingFace( |
| 60 | + ... |
| 61 | + compiler_config=TrainingCompilerConfig() |
| 62 | + ) |
| 63 | +
|
| 64 | + .. seealso:: |
| 65 | +
|
| 66 | + For more information about how to enable SageMaker Training Compiler |
| 67 | + for various training settings such as using TensorFlow-based models, |
| 68 | + PyTorch-based models, and distributed training, |
| 69 | + see `Enable SageMaker Training Compiler |
| 70 | + <https://docs.aws.amazon.com/sagemaker/latest/dg/training-compiler-enable.html>`_ |
| 71 | + in the `Amazon SageMaker Training Compiler developer guide |
| 72 | + <https://docs.aws.amazon.com/sagemaker/latest/dg/training-compiler.html>`_. |
| 73 | +
|
| 74 | + """ |
| 75 | + |
| 76 | + super(TrainingCompilerConfig, self).__init__(enabled=enabled, debug=debug) |
| 77 | + |
| 78 | + @classmethod |
| 79 | + def validate( |
| 80 | + cls, |
| 81 | + estimator, |
| 82 | + ): |
| 83 | + """Checks if SageMaker Training Compiler is configured correctly. |
| 84 | +
|
| 85 | + Args: |
| 86 | + estimator (str): A estimator object |
| 87 | + If SageMaker Training Compiler is enabled, it will validate whether |
| 88 | + the estimator is configured to be compatible with Training Compiler. |
| 89 | +
|
| 90 | + Raises: |
| 91 | + ValueError: Raised if the requested configuration is not compatible |
| 92 | + with SageMaker Training Compiler. |
| 93 | + """ |
| 94 | + |
| 95 | + super(TrainingCompilerConfig, cls).validate(estimator) |
| 96 | + |
| 97 | + if estimator.image_uri: |
| 98 | + error_helper_string = ( |
| 99 | + "Overriding the image URI is currently not supported " |
| 100 | + "for SageMaker Training Compiler." |
| 101 | + "Specify the following parameters to run the Hugging Face training job " |
| 102 | + "with SageMaker Training Compiler enabled: " |
| 103 | + "transformer_version, tensorflow_version or pytorch_version, and compiler_config." |
| 104 | + ) |
| 105 | + raise ValueError(error_helper_string) |
0 commit comments