| 1 |
<!--Copyright 2021 The HuggingFace Team. All rights reserved. |
| 2 |
|
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 |
the License. You may obtain a copy of the License at |
| 5 |
|
| 6 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
|
| 8 |
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 |
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 |
specific language governing permissions and limitations under the License. |
| 11 |
|
| 12 |
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be |
| 13 |
rendered properly in your Markdown viewer. |
| 14 |
--> |
| 15 |
|
| 16 |
# Amazon SageMaker |
| 17 |
|
| 18 |
Hugging Face and Amazon introduced new [Hugging Face Deep Learning Containers (DLCs)](https://github.com/aws/deep-learning-containers/blob/master/available_images.md#huggingface-training-containers) to |
| 19 |
make it easier than ever to train Hugging Face Transformer models in [Amazon SageMaker](https://aws.amazon.com/sagemaker/). |
| 20 |
|
| 21 |
## Getting Started |
| 22 |
|
| 23 |
### Setup & Installation |
| 24 |
|
| 25 |
|
| 26 |
Before you can run your Accelerate scripts on Amazon SageMaker you need to sign up for an AWS account. If you do not |
| 27 |
have an AWS account yet learn more [here](https://docs.aws.amazon.com/sagemaker/latest/dg/gs-set-up.html). |
| 28 |
|
| 29 |
After you have your AWS Account you need to install the `sagemaker` sdk for Accelerate with: |
| 30 |
|
| 31 |
```bash |
| 32 |
pip install "accelerate[sagemaker]" --upgrade |
| 33 |
``` |
| 34 |
|
| 35 |
Accelerate currently uses the DLCs, with `transformers`, `datasets` and `tokenizers` pre-installed. Accelerate is not in the DLC yet (will soon be added!) so to use it within Amazon SageMaker you need to create a |
| 36 |
`requirements.txt` in the same directory where your training script is located and add it as dependency: |
| 37 |
|
| 38 |
``` |
| 39 |
accelerate |
| 40 |
``` |
| 41 |
|
| 42 |
You should also add any other dependencies you have to this `requirements.txt`. |
| 43 |
|
| 44 |
|
| 45 |
### Configure Accelerate |
| 46 |
|
| 47 |
You can configure the launch configuration for Amazon SageMaker the same as you do for non SageMaker training jobs with |
| 48 |
the Accelerate CLI: |
| 49 |
|
| 50 |
```bash |
| 51 |
accelerate config |
| 52 |
# In which compute environment are you running? ([0] This machine, [1] AWS (Amazon SageMaker)): 1 |
| 53 |
``` |
| 54 |
|
| 55 |
Accelerate will go through a questionnaire about your Amazon SageMaker setup and create a config file you can edit. |
| 56 |
|
| 57 |
<Tip> |
| 58 |
|
| 59 |
Accelerate is not saving any of your credentials. |
| 60 |
|
| 61 |
</Tip> |
| 62 |
|
| 63 |
### Prepare a Accelerate fine-tuning script |
| 64 |
|
| 65 |
The training script is very similar to a training script you might run outside of SageMaker, but to save your model |
| 66 |
after training you need to specify either `/opt/ml/model` or use `os.environ["SM_MODEL_DIR"]` as your save |
| 67 |
directory. After training, artifacts in this directory are uploaded to S3: |
| 68 |
|
| 69 |
|
| 70 |
```diff |
| 71 |
- torch.save('/opt/ml/model`) |
| 72 |
+ accelerator.save('/opt/ml/model') |
| 73 |
``` |
| 74 |
|
| 75 |
<Tip warning={true}> |
| 76 |
|
| 77 |
SageMaker doesn’t support argparse actions. If you want to use, for example, boolean hyperparameters, you need to |
| 78 |
specify type as bool in your script and provide an explicit True or False value for this hyperparameter. [[REF]](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/using_pytorch.html#prepare-a-pytorch-training-script). |
| 79 |
|
| 80 |
</Tip> |
| 81 |
|
| 82 |
### Launch Training |
| 83 |
|
| 84 |
You can launch your training with Accelerate CLI with: |
| 85 |
|
| 86 |
``` |
| 87 |
accelerate launch path_to_script.py --args_to_the_script |
| 88 |
``` |
| 89 |
|
| 90 |
This will launch your training script using your configuration. The only thing you have to do is provide all the |
| 91 |
arguments needed by your training script as named arguments. |
| 92 |
|
| 93 |
**Examples** |
| 94 |
|
| 95 |
<Tip> |
| 96 |
|
| 97 |
If you run one of the example scripts, don't forget to add `accelerator.save('/opt/ml/model')` to it. |
| 98 |
|
| 99 |
</Tip> |
| 100 |
|
| 101 |
```bash |
| 102 |
accelerate launch ./examples/sagemaker_example.py |
| 103 |
``` |
| 104 |
|
| 105 |
Outputs: |
| 106 |
|
| 107 |
``` |
| 108 |
Configuring Amazon SageMaker environment |
| 109 |
Converting Arguments to Hyperparameters |
| 110 |
Creating Estimator |
| 111 |
2021-04-08 11:56:50 Starting - Starting the training job... |
| 112 |
2021-04-08 11:57:13 Starting - Launching requested ML instancesProfilerReport-1617883008: InProgress |
| 113 |
......... |
| 114 |
2021-04-08 11:58:54 Starting - Preparing the instances for training......... |
| 115 |
2021-04-08 12:00:24 Downloading - Downloading input data |
| 116 |
2021-04-08 12:00:24 Training - Downloading the training image.................. |
| 117 |
2021-04-08 12:03:39 Training - Training image download completed. Training in progress.. |
| 118 |
........ |
| 119 |
epoch 0: {'accuracy': 0.7598039215686274, 'f1': 0.8178438661710037} |
| 120 |
epoch 1: {'accuracy': 0.8357843137254902, 'f1': 0.882249560632689} |
| 121 |
epoch 2: {'accuracy': 0.8406862745098039, 'f1': 0.8869565217391304} |
| 122 |
........ |
| 123 |
2021-04-08 12:05:40 Uploading - Uploading generated training model |
| 124 |
2021-04-08 12:05:40 Completed - Training job completed |
| 125 |
Training seconds: 331 |
| 126 |
Billable seconds: 331 |
| 127 |
You can find your model data at: s3://your-bucket/accelerate-sagemaker-1-2021-04-08-11-56-47-108/output/model.tar.gz |
| 128 |
``` |
| 129 |
|
| 130 |
## Advanced Features |
| 131 |
|
| 132 |
### Distributed Training: Data Parallelism |
| 133 |
|
| 134 |
Set up the accelerate config by running `accelerate config` and answer the SageMaker questions and set it up. |
| 135 |
To use SageMaker DDP, select it when asked |
| 136 |
`What is the distributed mode? ([0] No distributed training, [1] data parallelism):`. |
| 137 |
Example config below: |
| 138 |
```yaml |
| 139 |
base_job_name: accelerate-sagemaker-1 |
| 140 |
compute_environment: AMAZON_SAGEMAKER |
| 141 |
distributed_type: DATA_PARALLEL |
| 142 |
ec2_instance_type: ml.p3.16xlarge |
| 143 |
iam_role_name: xxxxx |
| 144 |
image_uri: null |
| 145 |
mixed_precision: fp16 |
| 146 |
num_machines: 1 |
| 147 |
profile: xxxxx |
| 148 |
py_version: py10 |
| 149 |
pytorch_version: 2.5.0 |
| 150 |
region: us-east-1 |
| 151 |
transformers_version: 4.17.0 |
| 152 |
use_cpu: false |
| 153 |
``` |
| 154 |
|
| 155 |
### Distributed Training: Model Parallelism |
| 156 |
|
| 157 |
*currently in development, will be supported soon.* |
| 158 |
|
| 159 |
### Python packages and dependencies |
| 160 |
|
| 161 |
Accelerate currently uses the DLCs, with `transformers`, `datasets` and `tokenizers` pre-installed. If you |
| 162 |
want to use different/other Python packages you can do this by adding them to the `requirements.txt`. These packages |
| 163 |
will be installed before your training script is started. |
| 164 |
|
| 165 |
### Local Training: SageMaker Local mode |
| 166 |
|
| 167 |
The local mode in the SageMaker SDK allows you to run your training script locally inside the HuggingFace DLC (Deep Learning container) |
| 168 |
or using your custom container image. This is useful for debugging and testing your training script inside the final container environment. |
| 169 |
Local mode uses Docker compose (*Note: Docker Compose V2 is not supported yet*). The SDK will handle the authentication against ECR |
| 170 |
to pull the DLC to your local environment. You can emulate CPU (single and multi-instance) and GPU (single instance) SageMaker training jobs. |
| 171 |
|
| 172 |
To use local mode, you need to set your `ec2_instance_type` to `local`. |
| 173 |
|
| 174 |
```yaml |
| 175 |
ec2_instance_type: local |
| 176 |
``` |
| 177 |
|
| 178 |
### Advanced configuration |
| 179 |
|
| 180 |
The configuration allows you to override parameters for the [Estimator](https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html). |
| 181 |
These settings have to be applied in the config file and are not part of `accelerate config`. You can control many additional aspects of the training job, e.g. use Spot instances, enable network isolation and many more. |
| 182 |
|
| 183 |
```yaml |
| 184 |
additional_args: |
| 185 |
# enable network isolation to restrict internet access for containers |
| 186 |
enable_network_isolation: True |
| 187 |
``` |
| 188 |
|
| 189 |
You can find all available configuration [here](https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html). |
| 190 |
|
| 191 |
### Use Spot Instances |
| 192 |
|
| 193 |
You can use Spot Instances e.g. using (see [Advanced configuration](#advanced-configuration)): |
| 194 |
```yaml |
| 195 |
additional_args: |
| 196 |
use_spot_instances: True |
| 197 |
max_wait: 86400 |
| 198 |
``` |
| 199 |
|
| 200 |
*Note: Spot Instances are subject to be terminated and training to be continued from a checkpoint. This is not handled in Accelerate out of the box. Contact us if you would like this feature.* |
| 201 |
|
| 202 |
### Remote scripts: Use scripts located on Github |
| 203 |
|
| 204 |
*undecided if feature is needed. Contact us if you would like this feature.* |