Debian Codegraph @

docs/source/concept_guides/deferring_execution.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 # Executing and deferring jobs
17
18 When you run your usual script, instructions are executed in order. Using Accelerate to deploy your script on several
19 GPUs at the same time introduces a complication: while each process executes all instructions in order, some may be
20 faster than others.
21
22 You might need to wait for all processes to have reached a certain point before executing a given instruction. For
23 instance, you shouldn't save a model before being sure every process is done with training, and you wouldn't want to
24 continue training before all the model weights have been loaded in. To do this, just write the following line in your code:
25
26 ```
27 accelerator.wait_for_everyone()
28 ```
29
30 This instruction will block all the processes that arrive first until all the other processes have reached that
31 point (if you run your script on just one GPU or CPU, this won't do anything).
32
33 A few example cases of when to use this utility are listed below:
34
35 <Tip>
36
37 Some of these are utilized with the [`~Accelerator.main_process_first`] context manager, which utilizes [`~Accelerator.wait_for_everyone`] to
38 run a particular set of code on the main process beforehand before triggering and launching the other processes
39
40 </Tip>
41
42 ## Downloading a Dataset
43
44 When downloading a dataset, you should download it first on the main process and then load the cached dataset afterward
45
46 <Tip>
47
48 `load_dataset` will perform a lock under the hood to stop multiple downloads from happening at once, but if you are downloading something
49 not using this library you should use this method.
50
51 </Tip>
52
53 ```python
54 with accelerator.main_process_first():
55 datasets = load_dataset("glue", "mrpc")
56 ```
57
58 Under the hood this is the same as calling:
59
60 ```python
61 # First do something on the main process
62 if accelerator.is_main_process:
63 datasets = load_dataset("glue", "mrpc")
64 else:
65 accelerator.wait_for_everyone()
66
67 # And then send it to the rest of them
68 if not accelerator.is_main_process:
69 datasets = load_dataset("glue", "mrpc")
70 else:
71 accelerator.wait_for_everyone()
72 ```
73
74 ## Saving the `state_dict`
75
76 When saving the `state_dict` of the model, since you would normally save one file on just the main process
77 you should specify that:
78
79 ```python
80 if accelerator.is_main_process:
81 model = accelerator.unwrap_model(model)
82 torch.save(model.state_dict(), "weights.pth")
83 ```
84
85 ## Loading in the `state_dict`
86
87 When loading in the `state_dict` to a model, optimizer, or scheduler, you should wait
88 for all workers to have the weights loaded in before moving on to training
89
90 ```python
91 with accelerator.main_process_first():
92 state = torch.load("weights.pth")
93 model.load_state_dict(state)
94 ```
95
96 ## Applying a multi-worker CPU operation
97
98 Applying a `map()` operation on multiple workers, such as tokenizing should be done on the
99 main process first, and then propagated to each one.
100
101 ```python
102 datasets = load_dataset("glue", "mrpc")
103
104 with accelerator.main_process_first():
105 tokenized_datasets = datasets.map(
106 tokenize_function,
107 batched=True,
108 remove_columns=["idx", "sentence1", "sentence2"],
109 )
110 ```
111
112 ## Applying checks such as Early Stopping
113
114 To have a check that works with a flag set by a particular process, the `set_trigger` and `check_trigger` API should be used. Useful examples
115 for doing so can include situations such as using early stopping and monitoring the loss (as each loss slightly differs on each process).
116
117 Call [`Accelerator.set_trigger`] when your condition has been met, and [`Accelerator.check_trigger`] when checking if that condition has been met in any process:
118
119 ```python
120 for (x,y) in data_loader:
121 logits = model(x)
122 loss = loss_func(logits, y)
123 # Assume `should_do_early_stopping` is a custom defined function that returns a conditional
124 if should_do_early_stopping(loss):
125 accelerator.set_trigger()
126
127 # Later in the training script when we need to check for the breakpoint
128 if accelerator.check_trigger():
129 break
130 ```