Debian Codegraph @

examples/README.md

raw
1 <!---
2 Copyright 2021 The HuggingFace Team. All rights reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 -->
16
17 # In this folder we showcase various full examples using 🤗 Accelerate
18
19 ## Simple NLP example
20
21 The [nlp_example.py](./nlp_example.py) script is a simple example to train a Bert model on a classification task ([GLUE's MRPC](https://www.microsoft.com/en-us/download/details.aspx?id=52398)).
22
23 Prior to running it you should install 🤗 Dataset and 🤗 Transformers:
24
25 ```bash
26 pip install datasets evaluate transformers
27 ```
28
29 The same script can be run in any of the following configurations:
30 - single CPU or single GPU
31 - multi CPUs
32 - multi GPUs (using PyTorch distributed mode)
33 - (multi) TPUs
34 - fp16 (mixed-precision) or fp32 (normal precision)
35
36 To run it in each of these various modes, use the following commands:
37 - single CPU:
38 * from a server without GPU
39 ```bash
40 python ./nlp_example.py
41 ```
42 * from any server by passing `cpu=True` to the `Accelerator`.
43 ```bash
44 python ./nlp_example.py --cpu
45 ```
46 * from any server with Accelerate launcher
47 ```bash
48 accelerate launch --cpu ./nlp_example.py
49 ```
50 - single GPU:
51 ```bash
52 python ./nlp_example.py # from a server with a GPU
53 ```
54 - with fp16 (mixed-precision)
55 * from any server by passing `mixed_precison=fp16` to the `Accelerator`.
56 ```bash
57 python ./nlp_example.py --mixed_precision fp16
58 ```
59 * from any server with Accelerate launcher
60 ```bash
61 accelerate launch --mixed_precision fp16 ./nlp_example.py
62 - multi CPUs (requires Open MPI, Intel MPI, or MVAPICH)
63 * With Accelerate config and launcher, execute the following from node 0:
64 ```bash
65 accelerate config # Select to have accelerate launch mpirun
66 accelerate launch ./nlp_example.py # This will run the script on each server
67 ```
68 * With Intel MPI:
69 ```bash
70 export CCL_WORKER_COUNT=1
71 export MASTER_ADDR=xxx.xxx.xxx.xxx #node0 ip
72 mpirun -f hostfile -n 16 -ppn 4 python ./nlp_example.py
73 ```
74 - multi GPUs (using PyTorch distributed mode)
75 * With Accelerate config and launcher
76 ```bash
77 accelerate config # This will create a config file on your server
78 accelerate launch ./nlp_example.py # This will run the script on your server
79 ```
80 * With traditional PyTorch launcher (`python -m torch.distributed.run` can be used instead of `torchrun`)
81 ```bash
82 torchrun --nproc_per_node 2 ./nlp_example.py
83 ```
84 - multi GPUs, multi node (several machines, using PyTorch distributed mode)
85 * With Accelerate config and launcher, on each machine:
86 ```bash
87 accelerate config # This will create a config file on each server
88 accelerate launch ./nlp_example.py # This will run the script on each server
89 ```
90 * With PyTorch launcher only (`python -m torch.distributed.run` can be used instead of `torchrun`). Run this command on each node:
91 ```bash
92 torchrun \ # python -m torch.distributed.run
93 --nproc_per_node 2 \
94 --nnodes 2 \
95 --rdzv_id 2299 \ # A unique job id
96 --rdzv_backend c10d \
97 --rdzv_endpoint master_node_ip_address:29500 \
98 ./nlp_example.py
99 ```
100 - (multi) TPUs
101 * With Accelerate config and launcher
102 ```bash
103 accelerate config # This will create a config file on your TPU server
104 accelerate launch ./nlp_example.py # This will run the script on each server
105 ```
106 * In PyTorch:
107 Add an `xmp.spawn` line in your script as you usually do.
108
109
110 ## Simple vision example
111
112 The [cv_example.py](./cv_example.py) script is a simple example to fine-tune a ResNet-50 on a classification task ([Oxford-IIT Pet Dataset](https://www.robots.ox.ac.uk/~vgg/data/pets/)).
113
114 The same script can be run in any of the following configurations:
115 - single CPU or single GPU
116 - multi CPUs
117 - multi GPUs (using PyTorch distributed mode)
118 - (multi) TPUs
119 - fp16 (mixed-precision) or fp32 (normal precision)
120
121 Prior to running it you should install timm and torchvision:
122
123 ```bash
124 pip install timm torchvision
125 ```
126
127 and you should download the data with the following commands:
128
129 ```bash
130 wget https://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz
131 tar -xzf images.tar.gz
132 ```
133
134 To run it in each of these various modes, use the following commands:
135 - single CPU:
136 * from a server without GPU
137 ```bash
138 python ./cv_example.py --data_dir path_to_data
139 ```
140 * from any server by passing `cpu=True` to the `Accelerator`.
141 ```bash
142 python ./cv_example.py --data_dir path_to_data --cpu
143 ```
144 * from any server with Accelerate launcher
145 ```bash
146 accelerate launch --cpu ./cv_example.py --data_dir path_to_data
147 ```
148 - single GPU:
149 ```bash
150 python ./cv_example.py # from a server with a GPU
151 ```
152 - with fp16 (mixed-precision)
153 * from any server by passing `mixed_precison=fp16` to the `Accelerator`.
154 ```bash
155 python ./cv_example.py --data_dir path_to_data --mixed_precison fp16
156 ```
157 * from any server with Accelerate launcher
158 ```bash
159 accelerate launch --mixed_precison fp16 ./cv_example.py --data_dir path_to_data
160 - multi CPUs (requires Open MPI, Intel MPI, or MVAPICH)
161 * With Accelerate config and launcher, run the following from node 0:
162 ```bash
163 accelerate config --config_file config.yaml # Select to have accelerate launch mpirun
164 accelerate launch ./cv_example.py --data_dir path_to_data # This will run the script on each server
165 ```
166 * With Intel MPI, execute mpirun from node 0:
167 ```bash
168 export CCL_WORKER_COUNT=1
169 export MASTER_ADDR=xxx.xxx.xxx.xxx #node0 ip
170 mpirun -f hostfile -n 16 -ppn 4 python ./cv_example.py --data_dir path_to_data
171 ```
172 - multi GPUs (using PyTorch distributed mode)
173 * With Accelerate config and launcher
174 ```bash
175 accelerate config --config_file config.yaml # This will create a config file on your server to `config.yaml`
176 accelerate launch --config_file config.yaml ./cv_example.py --data_dir path_to_data # This will run the script on your server
177 ```
178 * With traditional PyTorch launcher (`python -m torch.distributed.run` can be used instead of `torchrun`)
179 ```bash
180 torchrun --nproc_per_node 2 ./cv_example.py --data_dir path_to_data
181 ```
182 - multi GPUs, multi node (several machines, using PyTorch distributed mode)
183 * With Accelerate config and launcher, on each machine:
184 ```bash
185 accelerate config --config_file config.yaml # This will create a config file on your server to `config.yaml`
186 accelerate launch --config_file config.yaml ./cv_example.py --data_dir path_to_data # This will run the script on each server
187 ```
188 * With PyTorch launcher only (`python -m torch.distributed.run` can be used instead of `torchrun`). Run this command on each node:
189 ```bash
190 torchrun \ # python -m torch.distributed.run
191 --nproc_per_node 2 \
192 --nnodes 2 \
193 --rdzv_id 2299 \ # A unique job id
194 --rdzv_backend c10d \
195 --rdzv_endpoint master_node_ip_address:29500 \
196 ./cv_example.py --data_dir path_to_data
197 ```
198 - (multi) TPUs
199 * With Accelerate config and launcher
200 ```bash
201 accelerate config --config_file config.yaml # This will create a config file on your server to `config.yaml`
202 accelerate launch --config_file config.yaml ./cv_example.py --data_dir path_to_data # This will run the script on each server
203 ```
204 * In PyTorch:
205 Add an `xmp.spawn` line in your script as you usually do.
206
207 ### Simple vision example (GANs)
208
209 - [huggan project](https://github.com/huggingface/community-events/tree/main/huggan)
210
211
212 ### Using AWS SageMaker integration
213 - [Examples showcasing AWS SageMaker integration of 🤗 Accelerate.](https://github.com/pacman100/accelerate-aws-sagemaker)
214
215 ## Configuration zoo
216 In [/config_yaml_templates](./config_yaml_templates/) we have a variety of *minimal* `config.yaml` templates and examples to help you learn
217 how to create your own configuration files depending on the scenario.
218
219 ## SLURM Scripts
220 In [/slurm/submit_multigpu.sh](./slurm/submit_multigpu.sh) and [/slurm/submit_multinode.sh](./slurm/submit_multinode.sh) we present two scripts for running the examples on a machine with [SLURM](https://slurm.schedmd.com/documentation.html) workload manager.
221
222 In [/slurm/submit_multigpu.sh](./slurm/submit_multigpu.sh) the only parameter in the launcher that needs to be modified is `--num_processes`, which determines the number of GPUs we will use. In this case, using the environment variable `$SLURM_GPUS`, we indicate that we want to utilize all the GPUs available on the node we have requested.
223
224 In [/slurm/submit_multinode.sh](./slurm/submit_multinode.sh) we must specify the number of nodes that will be part of the training (`--num_machines`), how many GPUs we will use in total (`--num_processes`), the [`backend`](https://pytorch.org/docs/stable/elastic/run.html#note-on-rendezvous-backend), `--main_process_ip` which will be the address the master node and the `--main_process_port`.
225
226 In [/slurm/submit_multicpu.sh](./slurm/submit_multicpu.sh) we must specify the number of nodes that will be part of the training (`--num_machines`), how many CPU processes we will use in total (`--num_processes`), the [`backend`](https://pytorch.org/docs/stable/elastic/run.html#note-on-rendezvous-backend), `--main_process_ip` which will be the address the master node and the `--main_process_port`. `mpirun_hostfile` specifies to run the job using MPIRun.
227
228 In both scripts, we run `activateEnvironment.sh` at the beginning. This script should contain the necessary instructions to initialize the environment for execution. Below, we show an example that loads the necessary libraries ([Environment modules](https://github.com/cea-hpc/modules)), activates the Python environment, and sets up various environment variables, most of them to run the scripts in offline mode in case we don't have internet connection from the cluster.
229
230 ```bash
231 # activateEnvironment.sh
232 module purge
233 module load anaconda3/2020.02 cuda/10.2 cudnn/8.0.5 nccl/2.9.9 arrow/7.0.0 openmpi
234 source activate /home/nct01/nct01328/pytorch_antoni_local
235
236 export HF_HOME=/gpfs/projects/nct01/nct01328/
237 export HF_LOCAL_HOME=/gpfs/projects/nct01/nct01328/HF_LOCAL
238 export HF_DATASETS_OFFLINE=1
239 export TRANSFORMERS_OFFLINE=1
240 export PYTHONPATH=/home/nct01/nct01328/transformers-in-supercomputers:$PYTHONPATH
241 export GPUS_PER_NODE=4
242 ```
243
244 ## Simple Multi-GPU Hardware Launcher (using an external platform)
245
246 [multigpu_remote_launcher.py](./multigpu_remote_launcher.py) is a minimal script that demonstrates launching accelerate
247 on multiple remote GPUs, and with automatic hardware environment and dependency setup for reproducibility. You can
248 easily customize the training function used, training arguments, hyperparameters, and type of compute hardware, and then
249 run the script to automatically launch multi GPU training on remote hardware.
250
251 This script uses [Runhouse](https://github.com/run-house/runhouse) to launch on self-hosted hardware (e.g. in your own
252 cloud account or on-premise cluster) but there are other options for running remotely as well. Runhouse can be installed
253 with `pip install runhouse`, and you can refer to
254 [hardware setup](https://runhouse-docs.readthedocs-hosted.com/en/latest/api/python/cluster.html#hardware-setup)
255 for hardware setup instructions, or this
256 [Colab tutorial](https://colab.research.google.com/drive/1qVwYyLTCPYPSdz9ZX7BZl9Qm0A3j7RJe) for a more in-depth walkthrough.
257
258 ## Simple fine-tuning script that works on TPU
259
260 [finetune_lm_tpu.py](./finetune_lm_tpu.py) is a classical language modeling generation fine tuning script that has been
261 adapted to run best on TPUs. It has been successfully run and tested on a TPU v5 litepod-8, and it shows how it is
262 possible to perform a fine-tuning task on such hardware thanks to accelerate and FSDPv2, using transformers and Torch XLA.
263
264 ## Finer Examples
265
266 While the first two scripts are extremely barebones when it comes to what you can do with accelerate, more advanced features are documented in two other locations.
267
268 ### `by_feature` examples
269
270 These scripts are *individual* examples highlighting one particular feature or use-case within Accelerate. They all stem from the [nlp_example.py](./nlp_example.py) script, and any changes or modifications is denoted with a `# New Code #` comment.
271
272 Read the README.md file located in the `by_feature` folder for more information.
273
274 ### `complete_*` examples
275
276 These two scripts contain *every* single feature currently available in Accelerate in one place, as one giant script.
277
278 New arguments that can be passed include:
279
280 - `checkpointing_steps`, whether the various states should be saved at the end of every `n` steps, or `"epoch"` for each epoch. States are then saved to folders named `step_{n}` or `epoch_{n}`
281 - `resume_from_checkpoint`, should be used if you want to resume training off of a previous call to the script and passed a `checkpointing_steps` to it.
282 - `with_tracking`, should be used if you want to log the training run using all available experiment trackers in your environment. Currently supported trackers include TensorBoard, Weights and Biases, and CometML.