Debian Codegraph @

docs/source/package_reference/utilities.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 # Utility functions and classes
17
18 Below are a variety of utility functions that 🤗 Accelerate provides, broken down by use-case.
19
20 ## Constants
21
22 Constants used throughout 🤗 Accelerate for reference
23
24 The following are constants used when utilizing [`Accelerator.save_state`]
25
26 `utils.MODEL_NAME`: `"pytorch_model"`
27 `utils.OPTIMIZER_NAME`: `"optimizer"`
28 `utils.RNG_STATE_NAME`: `"random_states"`
29 `utils.SCALER_NAME`: `"scaler.pt`
30 `utils.SCHEDULER_NAME`: `"scheduler`
31
32 The following are constants used when utilizing [`Accelerator.save_model`]
33
34 `utils.WEIGHTS_NAME`: `"pytorch_model.bin"`
35 `utils.SAFE_WEIGHTS_NAME`: `"model.safetensors"`
36 `utils.WEIGHTS_INDEX_NAME`: `"pytorch_model.bin.index.json"`
37 `utils.SAFE_WEIGHTS_INDEX_NAME`: `"model.safetensors.index.json"`
38
39 ## Data Classes
40
41 These are basic dataclasses used throughout 🤗 Accelerate and they can be passed in as parameters.
42
43 ### Standalone
44
45 These are standalone dataclasses used for checks, such as the type of distributed system being used
46
47 [[autodoc]] utils.ComputeEnvironment
48
49 [[autodoc]] utils.DistributedType
50
51 [[autodoc]] utils.DynamoBackend
52
53 [[autodoc]] utils.LoggerType
54
55 [[autodoc]] utils.PrecisionType
56
57 [[autodoc]] utils.RNGType
58
59 [[autodoc]] utils.SageMakerDistributedType
60
61 ### Kwargs
62
63 These are configurable arguments for specific interactions throughout the PyTorch ecosystem that Accelerate handles under the hood.
64
65 [[autodoc]] utils.AutocastKwargs
66
67 [[autodoc]] utils.DistributedDataParallelKwargs
68
69 [[autodoc]] utils.FP8RecipeKwargs
70
71 [[autodoc]] utils.GradScalerKwargs
72
73 [[autodoc]] utils.InitProcessGroupKwargs
74
75 [[autodoc]] utils.KwargsHandler
76
77 ## Plugins
78
79 These are plugins that can be passed to the [`Accelerator`] object. While they are defined elsewhere in the documentation,
80 for convenience all of them are available to see here:
81
82 [[autodoc]] utils.DeepSpeedPlugin
83
84 [[autodoc]] utils.FullyShardedDataParallelPlugin
85
86 [[autodoc]] utils.GradientAccumulationPlugin
87
88 [[autodoc]] utils.MegatronLMPlugin
89
90 [[autodoc]] utils.TorchDynamoPlugin
91
92 ## Configurations
93
94 These are classes which can be configured and passed through to the appropriate integration
95
96 [[autodoc]] utils.BnbQuantizationConfig
97
98 [[autodoc]] utils.DataLoaderConfiguration
99
100 [[autodoc]] utils.ProjectConfiguration
101
102 ## Environmental Variables
103
104 These are environmental variables that can be enabled for different use cases
105
106 * `ACCELERATE_DEBUG_MODE` (`str`): Whether to run accelerate in debug mode. More info available [here](../usage_guides/debug.md).
107
108
109
110
111 ## Data Manipulation and Operations
112
113 These include data operations that mimic the same `torch` ops but can be used on distributed processes.
114
115 [[autodoc]] utils.broadcast
116
117 [[autodoc]] utils.broadcast_object_list
118
119 [[autodoc]] utils.concatenate
120
121 [[autodoc]] utils.convert_outputs_to_fp32
122
123 [[autodoc]] utils.convert_to_fp32
124
125 [[autodoc]] utils.gather
126
127 [[autodoc]] utils.gather_object
128
129 [[autodoc]] utils.get_grad_scaler
130
131 [[autodoc]] utils.get_mixed_precision_context_manager
132
133 [[autodoc]] utils.listify
134
135 [[autodoc]] utils.pad_across_processes
136
137 [[autodoc]] utils.recursively_apply
138
139 [[autodoc]] utils.reduce
140
141 [[autodoc]] utils.send_to_device
142
143 [[autodoc]] utils.slice_tensors
144
145 ## Environment Checks
146
147 These functionalities check the state of the current working environment including information about the operating system itself, what it can support, and if particular dependencies are installed.
148
149 [[autodoc]] utils.is_bf16_available
150
151 [[autodoc]] utils.is_mps_available
152
153 [[autodoc]] utils.is_npu_available
154
155 [[autodoc]] utils.is_torch_version
156
157 [[autodoc]] utils.is_torch_xla_available
158
159 [[autodoc]] utils.is_xpu_available
160
161 ## Environment Manipulation
162
163 [[autodoc]] utils.patch_environment
164
165 [[autodoc]] utils.clear_environment
166
167 [[autodoc]] utils.write_basic_config
168
169 When setting up 🤗 Accelerate for the first time, rather than running `accelerate config` [~utils.write_basic_config] can be used as an alternative for quick configuration.
170
171 [[autodoc]] utils.set_numa_affinity
172
173 [[autodoc]] utils.environment.override_numa_affinity
174
175 [[autodoc]] utils.purge_accelerate_environment
176
177 ## Memory
178
179 [[autodoc]] utils.find_executable_batch_size
180
181 ## Modeling
182
183 These utilities relate to interacting with PyTorch models
184
185 [[autodoc]] utils.calculate_maximum_sizes
186
187 [[autodoc]] utils.compute_module_sizes
188
189 [[autodoc]] utils.extract_model_from_parallel
190
191 [[autodoc]] utils.get_balanced_memory
192
193 [[autodoc]] utils.get_max_layer_size
194
195 [[autodoc]] utils.infer_auto_device_map
196
197 [[autodoc]] utils.load_checkpoint_in_model
198
199 [[autodoc]] utils.load_offloaded_weights
200
201 [[autodoc]] utils.load_state_dict
202
203 [[autodoc]] utils.offload_state_dict
204
205 [[autodoc]] utils.retie_parameters
206
207 [[autodoc]] utils.set_module_tensor_to_device
208
209 [[autodoc]] utils.get_module_children_bottom_up
210
211 ## Parallel
212
213 These include general utilities that should be used when working in parallel.
214
215 [[autodoc]] utils.extract_model_from_parallel
216
217 [[autodoc]] utils.save
218
219 [[autodoc]] utils.load
220
221 [[autodoc]] utils.wait_for_everyone
222
223
224 ## Random
225
226 These utilities relate to setting and synchronizing of all the random states.
227
228 [[autodoc]] utils.set_seed
229
230 [[autodoc]] utils.synchronize_rng_state
231
232 [[autodoc]] utils.synchronize_rng_states
233
234
235 ## PyTorch XLA
236
237 These include utilities that are useful while using PyTorch with XLA.
238
239 [[autodoc]] utils.install_xla
240
241 ## Loading model weights
242
243 These include utilities that are useful to load checkpoints.
244
245 [[autodoc]] utils.load_checkpoint_in_model
246
247 ## Quantization
248
249 These include utilities that are useful to quantize model.
250
251 [[autodoc]] utils.load_and_quantize_model