Debian Codegraph @

docs/source/concept_guides/internal_mechanism.md

raw
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 # Accelerate's internal mechanisms
17
18 Internally, Accelerate works by first analyzing the environment in which the script is launched to determine which
19 kind of distributed setup is used, how many different processes there are and which one the current script is in. All
20 that information is stored in the [`~AcceleratorState`].
21
22 This class is initialized the first time you instantiate an [`~Accelerator`] as well as performing any
23 specific initialization your distributed setup needs. Its state is then uniquely shared through all instances of
24 [`~state.AcceleratorState`]. (The same can also be done with the [`PartialState`], a more barebones version it inherits)
25
26 Then, when calling [`~Accelerator.prepare`], the library:
27
28 - wraps your model(s) in the container adapted for the distributed setup,
29 - wraps your optimizer(s) in an [`~optimizer.AcceleratedOptimizer`],
30 - wraps your scheduler(s) in an [`~scheduler.AcceleratedScheduler`]
31 - creates a new version of your dataloader(s) in a [`~data_loader.DataLoaderShard`] or [`~data_loader.DataLoaderDispatcher`]
32
33 While the model(s), optimizer(s), and scheduler(s) are just put in simple wrappers, the dataloader(s) are re-created. This is mostly
34 because PyTorch does not let the user change the `batch_sampler` of a dataloader once it's been created and the
35 library handles the sharding of your data between processes by changing that `batch_sampler` to yield every other
36 `num_processes` batches (if enabled).
37
38 The [`~data_loader.DataLoaderShard`] subclasses `DataLoader` to add the following functionality:
39
40 - it synchronizes the appropriate random number generator of all processes at each new iteration, to ensure any
41 randomization (like shuffling) is done the exact same way across processes.
42 - it puts the batches on the proper device before yielding them (unless you have opted out of
43 `device_placement=True`).
44
45 The [`~data_loader.DataLoaderDispatcher`] subclasses differs from the [`~data_loader.DataLoaderShard`] in that when iterating through the `DataLoader`, the data is all starting from process 0 and *then* split and sent off to each process rather than it happening at the dataset level.
46
47 The random number generator synchronization will by default synchronize:
48
49 - the `generator` attribute of a given sampler (like the PyTorch `RandomSampler`) for PyTorch >= 1.6
50 - the main random number generator in PyTorch <=1.5.1
51
52 You can choose which random number generator(s) to synchronize with the `rng_types` argument of the main
53 [`Accelerator`]. In PyTorch >= 1.6, it is recommended to rely on a local `generator` to avoid
54 setting the same seed in the main random number generator in all processes.
55
56 <Tip warning={true}>
57
58 Synchronization of the main torch (or CUDA or XLA) random number generator will affect any other potential random
59 artifacts you could have in your dataset (like random data augmentation) in the sense that all processes will get
60 the same random numbers from the torch random modules (so will apply the same random data augmentation if it's
61 controlled by torch).
62
63 </Tip>
64
65 <Tip>
66
67 The randomization part of your custom sampler, batch sampler or iterable dataset should be done using a local
68 `torch.Generator` object (in PyTorch >= 1.6), see the traditional `RandomSampler`, as an example.
69
70 </Tip>
71
72 If you have [`torchdata>=0.8.0`](https://github.com/pytorch/data/tree/main) installed, and you have passed `use_stateful_dataloader=True` into your [`~utils.DataLoaderConfiguration`], these classes will directly inherit from `StatefulDataLoader` instead, and maintain a `state_dict`.
73
74 For more details about the internals, see the [Internals page](../package_reference/torch_wrappers).