Debian Codegraph @

docs/source/basic_tutorials/launch.md

raw
1 <!--Copyright 2022 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 # Launching Accelerate scripts
17
18 In the previous tutorial, you were introduced to how to modify your current training script to use Accelerate.
19 The final version of that code is shown below:
20
21 ```python
22 from accelerate import Accelerator
23
24 accelerator = Accelerator()
25
26 model, optimizer, training_dataloader, scheduler = accelerator.prepare(
27 model, optimizer, training_dataloader, scheduler
28 )
29
30 for batch in training_dataloader:
31 optimizer.zero_grad()
32 inputs, targets = batch
33 outputs = model(inputs)
34 loss = loss_function(outputs, targets)
35 accelerator.backward(loss)
36 optimizer.step()
37 scheduler.step()
38 ```
39
40 But how do you run this code and have it utilize the special hardware available to it?
41
42 First, you should rewrite the above code into a function, and make it callable as a script. For example:
43
44 ```diff
45 from accelerate import Accelerator
46
47 + def main():
48 accelerator = Accelerator()
49
50 model, optimizer, training_dataloader, scheduler = accelerator.prepare(
51 model, optimizer, training_dataloader, scheduler
52 )
53
54 for batch in training_dataloader:
55 optimizer.zero_grad()
56 inputs, targets = batch
57 outputs = model(inputs)
58 loss = loss_function(outputs, targets)
59 accelerator.backward(loss)
60 optimizer.step()
61 scheduler.step()
62
63 + if __name__ == "__main__":
64 + main()
65 ```
66
67 Next, you need to launch it with `accelerate launch`.
68
69 <Tip warning={true}>
70
71 It's recommended you run `accelerate config` before using `accelerate launch` to configure your environment to your liking.
72 Otherwise Accelerate will use very basic defaults depending on your system setup.
73
74 </Tip>
75
76
77 ## Using accelerate launch
78
79 Accelerate has a special CLI command to help you launch your code in your system through `accelerate launch`.
80 This command wraps around all of the different commands needed to launch your script on various platforms, without you having to remember what each of them is.
81
82 <Tip>
83
84 If you are familiar with launching scripts in PyTorch yourself such as with `torchrun`, you can still do this. It is not required to use `accelerate launch`.
85
86 </Tip>
87
88 You can launch your script quickly by using:
89
90 ```bash
91 accelerate launch {script_name.py} --arg1 --arg2 ...
92 ```
93
94 Just put `accelerate launch` at the start of your command, and pass in additional arguments and parameters to your script afterward like normal!
95
96 Since this runs the various torch spawn methods, all of the expected environment variables can be modified here as well.
97 For example, here is how to use `accelerate launch` with a single GPU:
98
99 ```bash
100 # for cuda device:
101 CUDA_VISIBLE_DEVICES="0" accelerate launch {script_name.py} --arg1 --arg2 ...
102 # for xpu device:
103 ZE_AFFINITY_MASK="0" accelerate launch {script_name.py} --arg1 --arg2 ...
104 ```
105
106 You can also use `accelerate launch` without performing `accelerate config` first, but you may need to manually pass in the right configuration parameters.
107 In this case, Accelerate will make some hyperparameter decisions for you, e.g., if GPUs are available, it will use all of them by default without the mixed precision.
108 Here is how you would use all GPUs and train with mixed precision disabled:
109
110 ```bash
111 accelerate launch --multi_gpu {script_name.py} {--arg1} {--arg2} ...
112 ```
113
114 Or by specifying a number of GPUs to use:
115
116 ```bash
117 accelerate launch --num_processes=2 {script_name.py} {--arg1} {--arg2} ...
118 ```
119
120 To get more specific you should pass in the needed parameters yourself. For instance, here is how you
121 would also launch that same script on two GPUs using mixed precision while avoiding all of the warnings:
122
123 ```bash
124 accelerate launch --multi_gpu --mixed_precision=fp16 --num_processes=2 {script_name.py} {--arg1} {--arg2} ...
125 ```
126
127 For a complete list of parameters you can pass in, run:
128
129 ```bash
130 accelerate launch -h
131 ```
132
133 <Tip>
134
135 Even if you are not using Accelerate in your code, you can still use the launcher for starting your scripts!
136
137 </Tip>
138
139 For a visualization of this difference, that earlier `accelerate launch` on multi-gpu would look something like so with `torchrun`:
140
141 ```bash
142 MIXED_PRECISION="fp16" torchrun --nproc_per_node=2 --nnodes=1 {script_name.py} {--arg1} {--arg2} ...
143 ```
144
145 You can also launch your script utilizing the launch CLI as a python module itself, enabling the ability to pass in other python-specific
146 launching behaviors. To do so, use `accelerate.commands.launch` instead of `accelerate launch`:
147
148 ```bash
149 python -m accelerate.commands.launch --num_processes=2 {script_name.py} {--arg1} {--arg2}
150 ```
151
152 If you want to execute the script with any other python flags, you can pass them in as well similar to `-m`, such as
153 the below example enabling unbuffered stdout and stderr:
154
155 ```bash
156 python -u -m accelerate.commands.launch --num_processes=2 {script_name.py} {--arg1} {--arg2}
157 ```
158
159 <Tip>
160
161 You can run your code on CPU as well! This is helpful for debugging and testing purposes on toy models and datasets.
162
163 ```bash
164 accelerate launch --cpu {script_name.py} {--arg1} {--arg2}
165 ```
166
167 </Tip>
168
169 ## Why you should always use `accelerate config`
170
171 Why is it useful to the point you should **always** run `accelerate config`?
172
173 Remember that earlier call to `accelerate launch` as well as `torchrun`?
174 Post configuration, to run that script with the needed parts you just need to use `accelerate launch` outright, without passing anything else in:
175
176 ```bash
177 accelerate launch {script_name.py} {--arg1} {--arg2} ...
178 ```
179
180
181 ## Custom Configurations
182
183 As briefly mentioned earlier, `accelerate launch` should be mostly used through combining set configurations
184 made with the `accelerate config` command. These configs are saved to a `default_config.yaml` file in your cache folder for Accelerate.
185 This cache folder is located at (with decreasing order of priority):
186
187 - The content of your environment variable `HF_HOME` suffixed with `accelerate`.
188 - If it does not exist, the content of your environment variable `XDG_CACHE_HOME` suffixed with
189 `huggingface/accelerate`.
190 - If this does not exist either, the folder `~/.cache/huggingface/accelerate`.
191
192 To have multiple configurations, the flag `--config_file` can be passed to the `accelerate launch` command paired
193 with the location of the custom yaml.
194
195 An example yaml may look something like the following for two GPUs on a single machine using `fp16` for mixed precision:
196 ```yaml
197 compute_environment: LOCAL_MACHINE
198 deepspeed_config: {}
199 distributed_type: MULTI_GPU
200 fsdp_config: {}
201 machine_rank: 0
202 main_process_ip: null
203 main_process_port: null
204 main_training_function: main
205 mixed_precision: fp16
206 num_machines: 1
207 num_processes: 2
208 use_cpu: false
209 ```
210
211 Launching a script from the location of that custom yaml file looks like the following:
212 ```bash
213 accelerate launch --config_file {path/to/config/my_config_file.yaml} {script_name.py} {--arg1} {--arg2} ...
214 ```
215
216 ## Multi-node training
217 Multi-node training with Accelerate is similar to [multi-node training with torchrun](https://pytorch.org/tutorials/intermediate/ddp_series_multinode.html). The simplest way to launch a multi-node training run is to do the following:
218
219 - Copy your codebase and data to all nodes. (or place them on a shared filesystem)
220 - Setup your python packages on all nodes.
221 - Run `accelerate config` on the main single node first. After specifying the number of nodes, you will be asked to specify the rank of each node (this will be 0 for the main/master node), along with the IP address and port for the main process. This is required for the worker nodes to communicate with the main process. Afterwards, you can copy or send this config file across all of your nodes, changing the `machine_rank` to 1, 2,3, etc. to avoid having to run the command (or just follow their directions directly for launching with `torchrun` as well)
222
223 Once you have done this, you can start your multi-node training run by running `accelerate launch` (or `torchrun`) on all nodes.
224
225 <Tip>
226 It is required that the command be run on all nodes for everything to start, not just running it from the main node. You can use something like SLURM or a different process executor to wrap around this requirement and call everything from a single command.
227 </Tip>
228
229 <Tip>
230
231 It is recommended to use the intranet IP of your main node over the public IP for better latency. This is the `192.168.x.x` or the `172.x.x.x` address you see when you run `hostname -I` on the main node.
232
233 </Tip>
234
235 To get a better idea about multi-node training, check out our example for [multi-node training with FSDP](https://huggingface.co/blog/ram-efficient-pytorch-fsdp).