Debian Codegraph @

docs/source/basic_tutorials/execution.md

raw
1 <!--Copyright 2024 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 # Execution process
17
18 When working with distributed training systems, it is important to manage how and when processes are executed across GPUs. Some processes are completed faster than others, and some processes shouldn't begin if others haven't finished yet. Accelerate provides tools for orchestrating when processes are executed to ensure everything remains synchronized across all devices.
19
20 This tutorial will teach you how to execute a process on only one machine and how to delay execution until all processes have reached a certain point.
21
22 ## Execute on one process
23
24 Certain code only needs to be run once on a given machine, such as printing a log statement or only displaying one progress bar on the local main process.
25
26 <hfoptions id="local-execution">
27 <hfoption id="statements">
28
29 You should use `accelerator.is_local_main_process` to indicate code that should only be executed once.
30
31 ```py
32 from tqdm.auto import tqdm
33
34 progress_bar = tqdm(range(args.max_train_steps), disable=not accelerator.is_local_main_process)
35 ```
36
37 You could also wrap a statement with `accelerator.is_local_main_process`.
38
39 > [!TIP]
40 > For standalone `print` statements that aren't wrapped in `accelerator.is_local_main_process`, replace `print` with Accelerate's [`~Accelerator.print`] method to only print once per process.
41
42 ```py
43 if accelerator.is_local_main_process:
44 print("Accelerate is the best")
45 ```
46
47 </hfoption>
48 <hfoption id="function">
49
50 For a function that should only be executed once, use [`~Accelerator.on_local_main_process`].
51
52 ```py
53 @accelerator.on_local_main_process
54 def do_my_thing():
55 "Something done once per server"
56 do_thing_once_per_server()
57 ```
58
59 </hfoption>
60 </hfoptions>
61
62 You could also direct Accelerate to execute code once across *all processes* regardless of the number of machines. This is useful if you're uploading a final model to the Hub.
63
64 <hfoptions id="main-execution">
65 <hfoption id="statement">
66
67 You should use `accelerator.is_main_process` to indicate code that should only be executed once across all processes.
68
69 ```py
70 if accelerator.is_main_process:
71 repo.push_to_hub()
72 ```
73
74 </hfoption>
75 <hfoption id="function">
76
77 For a function that should only be executed once across all processes, use [`~Accelerator.on_main_process`].
78
79 ```py
80 @accelerator.on_main_process
81 def do_my_thing():
82 "Something done once per server"
83 do_thing_once()
84 ```
85
86 </hfoption>
87 </hfoptions>
88
89 ## Execute on a specific process
90
91 Accelerate can also help you execute functions that should only be executed on a specific process or a local process index.
92
93 <hfoptions id="specific-execution">
94 <hfoption id="specific process">
95
96 Use the [`~Accelerator.on_process`] method and specify the process index to execute a function on.
97
98 ```py
99 @accelerator.on_process(process_index=0)
100 def do_my_thing():
101 "Something done on process index 0"
102 do_thing_on_index_zero()
103 ```
104
105 </hfoption>
106 <hfoption id="local process">
107
108 Use the [`~Accelerator.on_local_process`] method and specify the local process index to execute a function on.
109
110 ```py
111 @accelerator.on_local_process(local_process_idx=0)
112 def do_my_thing():
113 "Something done on process index 0 on each server"
114 do_thing_on_index_zero_on_each_server()
115 ```
116
117 </hfoption>
118 </hfoptions>
119
120 ## Defer execution
121
122 When you run your script on several GPUs at the same time, some code may be executed faster than others. You might need to wait for all processes to reach a certain point before executing the next set of instructions. For instance, you shouldn’t save a model before making sure every process is done with training.
123
124 To do this, add [`~Accelerator.wait_for_everyone`] in your code. This blocks all processes that have finished first from continuing until all remaining processes have reached the same point (this has no effect if you're running on a single GPU or CPU).
125
126 ```py
127 accelerator.wait_for_everyone()
128 ```