ZhengyangZhang commited on
Commit
098233e
·
verified ·
1 Parent(s): 40c3acd

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. lib/python3.12/site-packages/accelerate-1.7.0.dist-info/INSTALLER +1 -0
  2. lib/python3.12/site-packages/accelerate-1.7.0.dist-info/LICENSE +201 -0
  3. lib/python3.12/site-packages/accelerate-1.7.0.dist-info/METADATA +382 -0
  4. lib/python3.12/site-packages/accelerate-1.7.0.dist-info/RECORD +177 -0
  5. lib/python3.12/site-packages/accelerate-1.7.0.dist-info/REQUESTED +0 -0
  6. lib/python3.12/site-packages/accelerate-1.7.0.dist-info/WHEEL +5 -0
  7. lib/python3.12/site-packages/accelerate-1.7.0.dist-info/entry_points.txt +6 -0
  8. lib/python3.12/site-packages/accelerate-1.7.0.dist-info/top_level.txt +1 -0
  9. lib/python3.12/site-packages/accelerate/__init__.py +50 -0
  10. lib/python3.12/site-packages/accelerate/accelerator.py +0 -0
  11. lib/python3.12/site-packages/accelerate/big_modeling.py +749 -0
  12. lib/python3.12/site-packages/accelerate/checkpointing.py +319 -0
  13. lib/python3.12/site-packages/accelerate/commands/__init__.py +13 -0
  14. lib/python3.12/site-packages/accelerate/commands/__pycache__/__init__.cpython-312.pyc +0 -0
  15. lib/python3.12/site-packages/accelerate/commands/__pycache__/accelerate_cli.cpython-312.pyc +0 -0
  16. lib/python3.12/site-packages/accelerate/commands/__pycache__/env.cpython-312.pyc +0 -0
  17. lib/python3.12/site-packages/accelerate/commands/__pycache__/estimate.cpython-312.pyc +0 -0
  18. lib/python3.12/site-packages/accelerate/commands/__pycache__/launch.cpython-312.pyc +0 -0
  19. lib/python3.12/site-packages/accelerate/commands/__pycache__/merge.cpython-312.pyc +0 -0
  20. lib/python3.12/site-packages/accelerate/commands/__pycache__/test.cpython-312.pyc +0 -0
  21. lib/python3.12/site-packages/accelerate/commands/__pycache__/to_fsdp2.cpython-312.pyc +0 -0
  22. lib/python3.12/site-packages/accelerate/commands/__pycache__/tpu.cpython-312.pyc +0 -0
  23. lib/python3.12/site-packages/accelerate/commands/__pycache__/utils.cpython-312.pyc +0 -0
  24. lib/python3.12/site-packages/accelerate/commands/accelerate_cli.py +54 -0
  25. lib/python3.12/site-packages/accelerate/commands/config/__init__.py +52 -0
  26. lib/python3.12/site-packages/accelerate/commands/config/__pycache__/__init__.cpython-312.pyc +0 -0
  27. lib/python3.12/site-packages/accelerate/commands/config/__pycache__/cluster.cpython-312.pyc +0 -0
  28. lib/python3.12/site-packages/accelerate/commands/config/__pycache__/config.cpython-312.pyc +0 -0
  29. lib/python3.12/site-packages/accelerate/commands/config/__pycache__/config_args.cpython-312.pyc +0 -0
  30. lib/python3.12/site-packages/accelerate/commands/config/__pycache__/config_utils.cpython-312.pyc +0 -0
  31. lib/python3.12/site-packages/accelerate/commands/config/__pycache__/default.cpython-312.pyc +0 -0
  32. lib/python3.12/site-packages/accelerate/commands/config/__pycache__/sagemaker.cpython-312.pyc +0 -0
  33. lib/python3.12/site-packages/accelerate/commands/config/__pycache__/update.cpython-312.pyc +0 -0
  34. lib/python3.12/site-packages/accelerate/commands/config/cluster.py +869 -0
  35. lib/python3.12/site-packages/accelerate/commands/config/config.py +89 -0
  36. lib/python3.12/site-packages/accelerate/commands/config/config_args.py +252 -0
  37. lib/python3.12/site-packages/accelerate/commands/config/config_utils.py +122 -0
  38. lib/python3.12/site-packages/accelerate/commands/config/default.py +163 -0
  39. lib/python3.12/site-packages/accelerate/commands/config/sagemaker.py +274 -0
  40. lib/python3.12/site-packages/accelerate/commands/config/update.py +63 -0
  41. lib/python3.12/site-packages/accelerate/commands/env.py +131 -0
  42. lib/python3.12/site-packages/accelerate/commands/estimate.py +312 -0
  43. lib/python3.12/site-packages/accelerate/commands/launch.py +1208 -0
  44. lib/python3.12/site-packages/accelerate/commands/menu/__init__.py +14 -0
  45. lib/python3.12/site-packages/accelerate/commands/menu/__pycache__/__init__.cpython-312.pyc +0 -0
  46. lib/python3.12/site-packages/accelerate/commands/menu/__pycache__/cursor.cpython-312.pyc +0 -0
  47. lib/python3.12/site-packages/accelerate/commands/menu/__pycache__/helpers.cpython-312.pyc +0 -0
  48. lib/python3.12/site-packages/accelerate/commands/menu/__pycache__/input.cpython-312.pyc +0 -0
  49. lib/python3.12/site-packages/accelerate/commands/menu/__pycache__/keymap.cpython-312.pyc +0 -0
  50. lib/python3.12/site-packages/accelerate/commands/menu/__pycache__/selection_menu.cpython-312.pyc +0 -0
lib/python3.12/site-packages/accelerate-1.7.0.dist-info/INSTALLER ADDED
@@ -0,0 +1 @@
 
 
1
+ pip
lib/python3.12/site-packages/accelerate-1.7.0.dist-info/LICENSE ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
lib/python3.12/site-packages/accelerate-1.7.0.dist-info/METADATA ADDED
@@ -0,0 +1,382 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Metadata-Version: 2.1
2
+ Name: accelerate
3
+ Version: 1.7.0
4
+ Summary: Accelerate
5
+ Home-page: https://github.com/huggingface/accelerate
6
+ Author: The HuggingFace team
7
+ Author-email: zach.mueller@huggingface.co
8
+ License: Apache
9
+ Keywords: deep learning
10
+ Classifier: Development Status :: 5 - Production/Stable
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Intended Audience :: Education
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: License :: OSI Approved :: Apache Software License
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
19
+ Requires-Python: >=3.9.0
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Requires-Dist: numpy<3.0.0,>=1.17
23
+ Requires-Dist: packaging>=20.0
24
+ Requires-Dist: psutil
25
+ Requires-Dist: pyyaml
26
+ Requires-Dist: torch>=2.0.0
27
+ Requires-Dist: huggingface-hub>=0.21.0
28
+ Requires-Dist: safetensors>=0.4.3
29
+ Provides-Extra: deepspeed
30
+ Requires-Dist: deepspeed; extra == "deepspeed"
31
+ Provides-Extra: dev
32
+ Requires-Dist: black~=23.1; extra == "dev"
33
+ Requires-Dist: hf-doc-builder>=0.3.0; extra == "dev"
34
+ Requires-Dist: ruff~=0.11.2; extra == "dev"
35
+ Requires-Dist: pytest<=8.0.0,>=7.2.0; extra == "dev"
36
+ Requires-Dist: pytest-xdist; extra == "dev"
37
+ Requires-Dist: pytest-subtests; extra == "dev"
38
+ Requires-Dist: parameterized; extra == "dev"
39
+ Requires-Dist: pytest-order; extra == "dev"
40
+ Requires-Dist: datasets; extra == "dev"
41
+ Requires-Dist: diffusers; extra == "dev"
42
+ Requires-Dist: evaluate; extra == "dev"
43
+ Requires-Dist: torchdata>=0.8.0; extra == "dev"
44
+ Requires-Dist: torchpippy>=0.2.0; extra == "dev"
45
+ Requires-Dist: transformers; extra == "dev"
46
+ Requires-Dist: scipy; extra == "dev"
47
+ Requires-Dist: scikit-learn; extra == "dev"
48
+ Requires-Dist: tqdm; extra == "dev"
49
+ Requires-Dist: bitsandbytes; extra == "dev"
50
+ Requires-Dist: timm; extra == "dev"
51
+ Requires-Dist: rich; extra == "dev"
52
+ Provides-Extra: docs
53
+ Provides-Extra: quality
54
+ Requires-Dist: black~=23.1; extra == "quality"
55
+ Requires-Dist: hf-doc-builder>=0.3.0; extra == "quality"
56
+ Requires-Dist: ruff~=0.11.2; extra == "quality"
57
+ Provides-Extra: rich
58
+ Requires-Dist: rich; extra == "rich"
59
+ Provides-Extra: sagemaker
60
+ Requires-Dist: sagemaker; extra == "sagemaker"
61
+ Provides-Extra: test_dev
62
+ Requires-Dist: datasets; extra == "test-dev"
63
+ Requires-Dist: diffusers; extra == "test-dev"
64
+ Requires-Dist: evaluate; extra == "test-dev"
65
+ Requires-Dist: torchdata>=0.8.0; extra == "test-dev"
66
+ Requires-Dist: torchpippy>=0.2.0; extra == "test-dev"
67
+ Requires-Dist: transformers; extra == "test-dev"
68
+ Requires-Dist: scipy; extra == "test-dev"
69
+ Requires-Dist: scikit-learn; extra == "test-dev"
70
+ Requires-Dist: tqdm; extra == "test-dev"
71
+ Requires-Dist: bitsandbytes; extra == "test-dev"
72
+ Requires-Dist: timm; extra == "test-dev"
73
+ Provides-Extra: test_fp8
74
+ Requires-Dist: torchao; extra == "test-fp8"
75
+ Provides-Extra: test_prod
76
+ Requires-Dist: pytest<=8.0.0,>=7.2.0; extra == "test-prod"
77
+ Requires-Dist: pytest-xdist; extra == "test-prod"
78
+ Requires-Dist: pytest-subtests; extra == "test-prod"
79
+ Requires-Dist: parameterized; extra == "test-prod"
80
+ Requires-Dist: pytest-order; extra == "test-prod"
81
+ Provides-Extra: test_trackers
82
+ Requires-Dist: wandb; extra == "test-trackers"
83
+ Requires-Dist: comet-ml; extra == "test-trackers"
84
+ Requires-Dist: tensorboard; extra == "test-trackers"
85
+ Requires-Dist: dvclive; extra == "test-trackers"
86
+ Requires-Dist: mlflow; extra == "test-trackers"
87
+ Requires-Dist: matplotlib; extra == "test-trackers"
88
+ Provides-Extra: testing
89
+ Requires-Dist: pytest<=8.0.0,>=7.2.0; extra == "testing"
90
+ Requires-Dist: pytest-xdist; extra == "testing"
91
+ Requires-Dist: pytest-subtests; extra == "testing"
92
+ Requires-Dist: parameterized; extra == "testing"
93
+ Requires-Dist: pytest-order; extra == "testing"
94
+ Requires-Dist: datasets; extra == "testing"
95
+ Requires-Dist: diffusers; extra == "testing"
96
+ Requires-Dist: evaluate; extra == "testing"
97
+ Requires-Dist: torchdata>=0.8.0; extra == "testing"
98
+ Requires-Dist: torchpippy>=0.2.0; extra == "testing"
99
+ Requires-Dist: transformers; extra == "testing"
100
+ Requires-Dist: scipy; extra == "testing"
101
+ Requires-Dist: scikit-learn; extra == "testing"
102
+ Requires-Dist: tqdm; extra == "testing"
103
+ Requires-Dist: bitsandbytes; extra == "testing"
104
+ Requires-Dist: timm; extra == "testing"
105
+
106
+ <!---
107
+ Copyright 2021 The HuggingFace Team. All rights reserved.
108
+
109
+ Licensed under the Apache License, Version 2.0 (the "License");
110
+ you may not use this file except in compliance with the License.
111
+ You may obtain a copy of the License at
112
+
113
+ http://www.apache.org/licenses/LICENSE-2.0
114
+
115
+ Unless required by applicable law or agreed to in writing, software
116
+ distributed under the License is distributed on an "AS IS" BASIS,
117
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
118
+ See the License for the specific language governing permissions and
119
+ limitations under the License.
120
+ -->
121
+
122
+ <p align="center">
123
+ <br>
124
+ <img src="https://raw.githubusercontent.com/huggingface/accelerate/main/docs/source/imgs/accelerate_logo.png" width="400"/>
125
+ <br>
126
+ <p>
127
+
128
+ <p align="center">
129
+ <!-- Uncomment when CircleCI is set up
130
+ <a href="https://circleci.com/gh/huggingface/accelerate"><img alt="Build" src="https://img.shields.io/circleci/build/github/huggingface/transformers/master"></a>
131
+ -->
132
+ <a href="https://github.com/huggingface/accelerate/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/github/license/huggingface/accelerate.svg?color=blue"></a>
133
+ <a href="https://huggingface.co/docs/accelerate/index.html"><img alt="Documentation" src="https://img.shields.io/website/http/huggingface.co/docs/accelerate/index.html.svg?down_color=red&down_message=offline&up_message=online"></a>
134
+ <a href="https://github.com/huggingface/accelerate/releases"><img alt="GitHub release" src="https://img.shields.io/github/release/huggingface/accelerate.svg"></a>
135
+ <a href="https://github.com/huggingface/accelerate/blob/main/CODE_OF_CONDUCT.md"><img alt="Contributor Covenant" src="https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg"></a>
136
+ </p>
137
+
138
+ <h3 align="center">
139
+ <p>Run your *raw* PyTorch training script on any kind of device
140
+ </h3>
141
+
142
+ <h3 align="center">
143
+ <a href="https://hf.co/course"><img src="https://raw.githubusercontent.com/huggingface/accelerate/main/docs/source/imgs/course_banner.png"></a>
144
+ </h3>
145
+
146
+ ## Easy to integrate
147
+
148
+ 🤗 Accelerate was created for PyTorch users who like to write the training loop of PyTorch models but are reluctant to write and maintain the boilerplate code needed to use multi-GPUs/TPU/fp16.
149
+
150
+ 🤗 Accelerate abstracts exactly and only the boilerplate code related to multi-GPUs/TPU/fp16 and leaves the rest of your code unchanged.
151
+
152
+ Here is an example:
153
+
154
+ ```diff
155
+ import torch
156
+ import torch.nn.functional as F
157
+ from datasets import load_dataset
158
+ + from accelerate import Accelerator
159
+
160
+ + accelerator = Accelerator()
161
+ - device = 'cpu'
162
+ + device = accelerator.device
163
+
164
+ model = torch.nn.Transformer().to(device)
165
+ optimizer = torch.optim.Adam(model.parameters())
166
+
167
+ dataset = load_dataset('my_dataset')
168
+ data = torch.utils.data.DataLoader(dataset, shuffle=True)
169
+
170
+ + model, optimizer, data = accelerator.prepare(model, optimizer, data)
171
+
172
+ model.train()
173
+ for epoch in range(10):
174
+ for source, targets in data:
175
+ source = source.to(device)
176
+ targets = targets.to(device)
177
+
178
+ optimizer.zero_grad()
179
+
180
+ output = model(source)
181
+ loss = F.cross_entropy(output, targets)
182
+
183
+ - loss.backward()
184
+ + accelerator.backward(loss)
185
+
186
+ optimizer.step()
187
+ ```
188
+
189
+ As you can see in this example, by adding 5-lines to any standard PyTorch training script you can now run on any kind of single or distributed node setting (single CPU, single GPU, multi-GPUs and TPUs) as well as with or without mixed precision (fp8, fp16, bf16).
190
+
191
+ In particular, the same code can then be run without modification on your local machine for debugging or your training environment.
192
+
193
+ 🤗 Accelerate even handles the device placement for you (which requires a few more changes to your code, but is safer in general), so you can even simplify your training loop further:
194
+
195
+ ```diff
196
+ import torch
197
+ import torch.nn.functional as F
198
+ from datasets import load_dataset
199
+ + from accelerate import Accelerator
200
+
201
+ - device = 'cpu'
202
+ + accelerator = Accelerator()
203
+
204
+ - model = torch.nn.Transformer().to(device)
205
+ + model = torch.nn.Transformer()
206
+ optimizer = torch.optim.Adam(model.parameters())
207
+
208
+ dataset = load_dataset('my_dataset')
209
+ data = torch.utils.data.DataLoader(dataset, shuffle=True)
210
+
211
+ + model, optimizer, data = accelerator.prepare(model, optimizer, data)
212
+
213
+ model.train()
214
+ for epoch in range(10):
215
+ for source, targets in data:
216
+ - source = source.to(device)
217
+ - targets = targets.to(device)
218
+
219
+ optimizer.zero_grad()
220
+
221
+ output = model(source)
222
+ loss = F.cross_entropy(output, targets)
223
+
224
+ - loss.backward()
225
+ + accelerator.backward(loss)
226
+
227
+ optimizer.step()
228
+ ```
229
+
230
+ Want to learn more? Check out the [documentation](https://huggingface.co/docs/accelerate) or have a look at our [examples](https://github.com/huggingface/accelerate/tree/main/examples).
231
+
232
+ ## Launching script
233
+
234
+ 🤗 Accelerate also provides an optional CLI tool that allows you to quickly configure and test your training environment before launching the scripts. No need to remember how to use `torch.distributed.run` or to write a specific launcher for TPU training!
235
+ On your machine(s) just run:
236
+
237
+ ```bash
238
+ accelerate config
239
+ ```
240
+
241
+ and answer the questions asked. This will generate a config file that will be used automatically to properly set the default options when doing
242
+
243
+ ```bash
244
+ accelerate launch my_script.py --args_to_my_script
245
+ ```
246
+
247
+ For instance, here is how you would run the GLUE example on the MRPC task (from the root of the repo):
248
+
249
+ ```bash
250
+ accelerate launch examples/nlp_example.py
251
+ ```
252
+
253
+ This CLI tool is **optional**, and you can still use `python my_script.py` or `python -m torchrun my_script.py` at your convenience.
254
+
255
+ You can also directly pass in the arguments you would to `torchrun` as arguments to `accelerate launch` if you wish to not run` accelerate config`.
256
+
257
+ For example, here is how to launch on two GPUs:
258
+
259
+ ```bash
260
+ accelerate launch --multi_gpu --num_processes 2 examples/nlp_example.py
261
+ ```
262
+
263
+ To learn more, check the CLI documentation available [here](https://huggingface.co/docs/accelerate/package_reference/cli).
264
+
265
+ Or view the configuration zoo [here](https://github.com/huggingface/accelerate/blob/main/examples/config_yaml_templates/)
266
+
267
+ ## Launching multi-CPU run using MPI
268
+
269
+ 🤗 Here is another way to launch multi-CPU run using MPI. You can learn how to install Open MPI on [this page](https://www.open-mpi.org/faq/?category=building#easy-build). You can use Intel MPI or MVAPICH as well.
270
+ Once you have MPI setup on your cluster, just run:
271
+ ```bash
272
+ accelerate config
273
+ ```
274
+ Answer the questions that are asked, selecting to run using multi-CPU, and answer "yes" when asked if you want accelerate to launch mpirun.
275
+ Then, use `accelerate launch` with your script like:
276
+ ```bash
277
+ accelerate launch examples/nlp_example.py
278
+ ```
279
+ Alternatively, you can use mpirun directly, without using the CLI like:
280
+ ```bash
281
+ mpirun -np 2 python examples/nlp_example.py
282
+ ```
283
+
284
+ ## Launching training using DeepSpeed
285
+
286
+ 🤗 Accelerate supports training on single/multiple GPUs using DeepSpeed. To use it, you don't need to change anything in your training code; you can set everything using just `accelerate config`. However, if you desire to tweak your DeepSpeed related args from your Python script, we provide you the `DeepSpeedPlugin`.
287
+
288
+ ```python
289
+ from accelerate import Accelerator, DeepSpeedPlugin
290
+
291
+ # deepspeed needs to know your gradient accumulation steps beforehand, so don't forget to pass it
292
+ # Remember you still need to do gradient accumulation by yourself, just like you would have done without deepspeed
293
+ deepspeed_plugin = DeepSpeedPlugin(zero_stage=2, gradient_accumulation_steps=2)
294
+ accelerator = Accelerator(mixed_precision='fp16', deepspeed_plugin=deepspeed_plugin)
295
+
296
+ # How to save your 🤗 Transformer?
297
+ accelerator.wait_for_everyone()
298
+ unwrapped_model = accelerator.unwrap_model(model)
299
+ unwrapped_model.save_pretrained(save_dir, save_function=accelerator.save, state_dict=accelerator.get_state_dict(model))
300
+ ```
301
+
302
+ Note: DeepSpeed support is experimental for now. In case you get into some problem, please open an issue.
303
+
304
+ ## Launching your training from a notebook
305
+
306
+ 🤗 Accelerate also provides a `notebook_launcher` function you can use in a notebook to launch a distributed training. This is especially useful for Colab or Kaggle notebooks with a TPU backend. Just define your training loop in a `training_function` then in your last cell, add:
307
+
308
+ ```python
309
+ from accelerate import notebook_launcher
310
+
311
+ notebook_launcher(training_function)
312
+ ```
313
+
314
+ An example can be found in [this notebook](https://github.com/huggingface/notebooks/blob/main/examples/accelerate_examples/simple_nlp_example.ipynb). [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/accelerate_examples/simple_nlp_example.ipynb)
315
+
316
+ ## Why should I use 🤗 Accelerate?
317
+
318
+ You should use 🤗 Accelerate when you want to easily run your training scripts in a distributed environment without having to renounce full control over your training loop. This is not a high-level framework above PyTorch, just a thin wrapper so you don't have to learn a new library. In fact, the whole API of 🤗 Accelerate is in one class, the `Accelerator` object.
319
+
320
+ ## Why shouldn't I use 🤗 Accelerate?
321
+
322
+ You shouldn't use 🤗 Accelerate if you don't want to write a training loop yourself. There are plenty of high-level libraries above PyTorch that will offer you that, 🤗 Accelerate is not one of them.
323
+
324
+ ## Frameworks using 🤗 Accelerate
325
+
326
+ If you like the simplicity of 🤗 Accelerate but would prefer a higher-level abstraction around its capabilities, some frameworks and libraries that are built on top of 🤗 Accelerate are listed below:
327
+
328
+ * [Amphion](https://github.com/open-mmlab/Amphion) is a toolkit for Audio, Music, and Speech Generation. Its purpose is to support reproducible research and help junior researchers and engineers get started in the field of audio, music, and speech generation research and development.
329
+ * [Animus](https://github.com/Scitator/animus) is a minimalistic framework to run machine learning experiments. Animus highlights common "breakpoints" in ML experiments and provides a unified interface for them within [IExperiment](https://github.com/Scitator/animus/blob/main/animus/core.py#L76).
330
+ * [Catalyst](https://github.com/catalyst-team/catalyst#getting-started) is a PyTorch framework for Deep Learning Research and Development. It focuses on reproducibility, rapid experimentation, and codebase reuse so you can create something new rather than write yet another train loop. Catalyst provides a [Runner](https://catalyst-team.github.io/catalyst/api/core.html#runner) to connect all parts of the experiment: hardware backend, data transformations, model training, and inference logic.
331
+ * [fastai](https://github.com/fastai/fastai#installing) is a PyTorch framework for Deep Learning that simplifies training fast and accurate neural nets using modern best practices. fastai provides a [Learner](https://docs.fast.ai/learner.html#Learner) to handle the training, fine-tuning, and inference of deep learning algorithms.
332
+ * [Finetuner](https://github.com/jina-ai/finetuner) is a service that enables models to create higher-quality embeddings for semantic search, visual similarity search, cross-modal text<->image search, recommendation systems, clustering, duplication detection, anomaly detection, or other uses.
333
+ * [InvokeAI](https://github.com/invoke-ai/InvokeAI) is a creative engine for Stable Diffusion models, offering industry-leading WebUI, terminal usage support, and serves as the foundation for many commercial products.
334
+ * [Kornia](https://kornia.readthedocs.io/en/latest/get-started/introduction.html) is a differentiable library that allows classical computer vision to be integrated into deep learning models. Kornia provides a [Trainer](https://kornia.readthedocs.io/en/latest/x.html#kornia.x.Trainer) with the specific purpose to train and fine-tune the supported deep learning algorithms within the library.
335
+ * [Open Assistant](https://projects.laion.ai/Open-Assistant/) is a chat-based assistant that understands tasks, can interact with their party systems, and retrieve information dynamically to do so.
336
+ * [pytorch-accelerated](https://github.com/Chris-hughes10/pytorch-accelerated) is a lightweight training library, with a streamlined feature set centered around a general-purpose [Trainer](https://pytorch-accelerated.readthedocs.io/en/latest/trainer.html), that places a huge emphasis on simplicity and transparency; enabling users to understand exactly what is going on under the hood, but without having to write and maintain the boilerplate themselves!
337
+ * [Stable Diffusion web UI](https://github.com/AUTOMATIC1111/stable-diffusion-webui) is an open-source browser-based easy-to-use interface based on the Gradio library for Stable Diffusion.
338
+ * [torchkeras](https://github.com/lyhue1991/torchkeras) is a simple tool for training pytorch model just in a keras style, a dynamic and beautiful plot is provided in notebook to monitor your loss or metric.
339
+ * [transformers](https://github.com/huggingface/transformers) as a tool for helping train state-of-the-art machine learning models in PyTorch, Tensorflow, and JAX. (Accelerate is the backend for the PyTorch side).
340
+
341
+
342
+ ## Installation
343
+
344
+ This repository is tested on Python 3.8+ and PyTorch 1.10.0+
345
+
346
+ You should install 🤗 Accelerate in a [virtual environment](https://docs.python.org/3/library/venv.html). If you're unfamiliar with Python virtual environments, check out the [user guide](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/).
347
+
348
+ First, create a virtual environment with the version of Python you're going to use and activate it.
349
+
350
+ Then, you will need to install PyTorch: refer to the [official installation page](https://pytorch.org/get-started/locally/#start-locally) regarding the specific install command for your platform. Then 🤗 Accelerate can be installed using pip as follows:
351
+
352
+ ```bash
353
+ pip install accelerate
354
+ ```
355
+
356
+ ## Supported integrations
357
+
358
+ - CPU only
359
+ - multi-CPU on one node (machine)
360
+ - multi-CPU on several nodes (machines)
361
+ - single GPU
362
+ - multi-GPU on one node (machine)
363
+ - multi-GPU on several nodes (machines)
364
+ - TPU
365
+ - FP16/BFloat16 mixed precision
366
+ - FP8 mixed precision with [Transformer Engine](https://github.com/NVIDIA/TransformerEngine) or [MS-AMP](https://github.com/Azure/MS-AMP/)
367
+ - DeepSpeed support (Experimental)
368
+ - PyTorch Fully Sharded Data Parallel (FSDP) support (Experimental)
369
+ - Megatron-LM support (Experimental)
370
+
371
+ ## Citing 🤗 Accelerate
372
+
373
+ If you use 🤗 Accelerate in your publication, please cite it by using the following BibTeX entry.
374
+
375
+ ```bibtex
376
+ @Misc{accelerate,
377
+ title = {Accelerate: Training and inference at scale made simple, efficient and adaptable.},
378
+ author = {Sylvain Gugger and Lysandre Debut and Thomas Wolf and Philipp Schmid and Zachary Mueller and Sourab Mangrulkar and Marc Sun and Benjamin Bossan},
379
+ howpublished = {\url{https://github.com/huggingface/accelerate}},
380
+ year = {2022}
381
+ }
382
+ ```
lib/python3.12/site-packages/accelerate-1.7.0.dist-info/RECORD ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ../../../bin/accelerate,sha256=c1wvf3H-k_L4tPnGX-XJg6WjN-gXaWNL5vZQ135LI6I,269
2
+ ../../../bin/accelerate-config,sha256=_W21OVf-14rQkJiDAXxKnqrC4Ty4PO9wzVPe7dp_LEc,261
3
+ ../../../bin/accelerate-estimate-memory,sha256=NpxmelW1G5wWZKFvbMPayrm-zUERPFTF7AwjoQiEkCw,263
4
+ ../../../bin/accelerate-launch,sha256=_77xdrxokfW2zuZ7LX5Nr-Ex9CoCQMRcUqGTE_EImrs,261
5
+ ../../../bin/accelerate-merge-weights,sha256=WWSDTcMy5pPVGPPBk2TF1EioBPiDQ4hDLFKDVtqiNtE,260
6
+ accelerate-1.7.0.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
7
+ accelerate-1.7.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
8
+ accelerate-1.7.0.dist-info/METADATA,sha256=Uh2USvM41iRR_WxMnmlQVVt8jrf8oGLwiyqvHNfRO1o,19490
9
+ accelerate-1.7.0.dist-info/RECORD,,
10
+ accelerate-1.7.0.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
+ accelerate-1.7.0.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
12
+ accelerate-1.7.0.dist-info/entry_points.txt,sha256=Vpy8gUGfZ-1VnM2229fb8CpJNLBdMH_wtJ9PQ7b_2tQ,296
13
+ accelerate-1.7.0.dist-info/top_level.txt,sha256=esVfdxTidsjQ90zsN_rPpjLFJ4ijRlx4mnLrG09hlt4,11
14
+ accelerate/__init__.py,sha256=mXUQGHBk-xdH1kg9XYR2xoW3DW5L5lz-7JI3mNlL5QY,1504
15
+ accelerate/__pycache__/__init__.cpython-312.pyc,,
16
+ accelerate/__pycache__/accelerator.cpython-312.pyc,,
17
+ accelerate/__pycache__/big_modeling.cpython-312.pyc,,
18
+ accelerate/__pycache__/checkpointing.cpython-312.pyc,,
19
+ accelerate/__pycache__/data_loader.cpython-312.pyc,,
20
+ accelerate/__pycache__/hooks.cpython-312.pyc,,
21
+ accelerate/__pycache__/inference.cpython-312.pyc,,
22
+ accelerate/__pycache__/launchers.cpython-312.pyc,,
23
+ accelerate/__pycache__/local_sgd.cpython-312.pyc,,
24
+ accelerate/__pycache__/logging.cpython-312.pyc,,
25
+ accelerate/__pycache__/memory_utils.cpython-312.pyc,,
26
+ accelerate/__pycache__/optimizer.cpython-312.pyc,,
27
+ accelerate/__pycache__/scheduler.cpython-312.pyc,,
28
+ accelerate/__pycache__/state.cpython-312.pyc,,
29
+ accelerate/__pycache__/tracking.cpython-312.pyc,,
30
+ accelerate/accelerator.py,sha256=B0JUgaTHvWkTCL1TOSG8h0JRSjS-YvbatdTAWXKRZuw,174281
31
+ accelerate/big_modeling.py,sha256=bqqO4Lw00YErTup7MUM6saOVHRFvMNOdNqAwg96fBG0,34765
32
+ accelerate/checkpointing.py,sha256=BaDOrpQzRI2U1BvN2vK4lepTRNNqbxGd4QPa1zOShoc,13612
33
+ accelerate/commands/__init__.py,sha256=m1PPTDT4ziIAvM0-FDSgIMIZ69Konn126s6LwuzH6v8,606
34
+ accelerate/commands/__pycache__/__init__.cpython-312.pyc,,
35
+ accelerate/commands/__pycache__/accelerate_cli.cpython-312.pyc,,
36
+ accelerate/commands/__pycache__/env.cpython-312.pyc,,
37
+ accelerate/commands/__pycache__/estimate.cpython-312.pyc,,
38
+ accelerate/commands/__pycache__/launch.cpython-312.pyc,,
39
+ accelerate/commands/__pycache__/merge.cpython-312.pyc,,
40
+ accelerate/commands/__pycache__/test.cpython-312.pyc,,
41
+ accelerate/commands/__pycache__/to_fsdp2.cpython-312.pyc,,
42
+ accelerate/commands/__pycache__/tpu.cpython-312.pyc,,
43
+ accelerate/commands/__pycache__/utils.cpython-312.pyc,,
44
+ accelerate/commands/accelerate_cli.py,sha256=SkwFad6Z1ZsGjtm7TiXFq8je-akshp_0WxX_6rGSBw8,1972
45
+ accelerate/commands/config/__init__.py,sha256=iJK8dgj3pc5Vdr1E7UuGoFu-BlybyXLxYDoTg9gXngE,1645
46
+ accelerate/commands/config/__pycache__/__init__.cpython-312.pyc,,
47
+ accelerate/commands/config/__pycache__/cluster.cpython-312.pyc,,
48
+ accelerate/commands/config/__pycache__/config.cpython-312.pyc,,
49
+ accelerate/commands/config/__pycache__/config_args.cpython-312.pyc,,
50
+ accelerate/commands/config/__pycache__/config_utils.cpython-312.pyc,,
51
+ accelerate/commands/config/__pycache__/default.cpython-312.pyc,,
52
+ accelerate/commands/config/__pycache__/sagemaker.cpython-312.pyc,,
53
+ accelerate/commands/config/__pycache__/update.cpython-312.pyc,,
54
+ accelerate/commands/config/cluster.py,sha256=8Mka4XTatMrFlxZjidvepRTLYgdJOX7ut9_4pLlmS7k,37537
55
+ accelerate/commands/config/config.py,sha256=FuRlQvOjgATEtyqOSsGD-KEtOCvACOHjs2C-krrtldk,3035
56
+ accelerate/commands/config/config_args.py,sha256=hJo7E3J_T7MPNkgtyNjPilJuH-wJK09mGWF6CvGFYIQ,9970
57
+ accelerate/commands/config/config_utils.py,sha256=lbV9iKAVSF5bUyQDGpzFduhH5JENYgAilPpRLLlCEq8,3299
58
+ accelerate/commands/config/default.py,sha256=UY0dQ7w1peBkQe8QDCm3XJrTnsRcm_G-_hf44NzNAbU,6213
59
+ accelerate/commands/config/sagemaker.py,sha256=SRLup9XrV_Z-23A3d8gZbkqbonDcscMDDmbpmytCzbY,10636
60
+ accelerate/commands/config/update.py,sha256=NXW1J7GkUHpg71QlIXsmMB_0z8S8IZo2FWax5POwrhc,2395
61
+ accelerate/commands/env.py,sha256=gRGwtPhMMWmZ5NTMaMrgHXaOzBV6SvAtP78TuuJvC6g,4336
62
+ accelerate/commands/estimate.py,sha256=Qduq4xudVyIede37BMEe1rNhXf-rfW-MHV2KtwxdfEA,12585
63
+ accelerate/commands/launch.py,sha256=-ODLdyV6zJ6yMsDKTNyUgUicYikX-VW2AA5wentb45g,46908
64
+ accelerate/commands/menu/__init__.py,sha256=uqSlBM0TFHBwzdv3p3SXfpAk1lZFp4h1a7mbBdscPHs,645
65
+ accelerate/commands/menu/__pycache__/__init__.cpython-312.pyc,,
66
+ accelerate/commands/menu/__pycache__/cursor.cpython-312.pyc,,
67
+ accelerate/commands/menu/__pycache__/helpers.cpython-312.pyc,,
68
+ accelerate/commands/menu/__pycache__/input.cpython-312.pyc,,
69
+ accelerate/commands/menu/__pycache__/keymap.cpython-312.pyc,,
70
+ accelerate/commands/menu/__pycache__/selection_menu.cpython-312.pyc,,
71
+ accelerate/commands/menu/cursor.py,sha256=-lmpJVAzvNc0c3EOtSuLoKB59zqylVCbYyWLPnrOmvQ,2028
72
+ accelerate/commands/menu/helpers.py,sha256=KrSB5fJjH4MUEUAQJ6bYaN16AYcnl9UalDrPD3DYeeg,1483
73
+ accelerate/commands/menu/input.py,sha256=T8Mdd-Y_OURgqfDV9qZh4Wf6hmT22AneNtJzj4JA1Rk,2512
74
+ accelerate/commands/menu/keymap.py,sha256=eXj-suyYs1m5dEHoUKN4mKAMLc8DWHnwhP6G6JSU0jQ,4086
75
+ accelerate/commands/menu/selection_menu.py,sha256=bxy-DHaKKC6SCToOlMBv5_z0MdUzylEg6Sio9OuV3GM,4921
76
+ accelerate/commands/merge.py,sha256=3Uo14LADvk8RJwZ_i6kIsGwWB3Ul5y8Uq47oHkDYqKU,2387
77
+ accelerate/commands/test.py,sha256=YrPYEaAACOGZ6btn2MV6NbMSEdBUcMWADLbQWaZSHtk,2149
78
+ accelerate/commands/to_fsdp2.py,sha256=gfbhoUT4qFB3LVDMNmckElgLG0yWm8aj_aofszeiJmM,5991
79
+ accelerate/commands/tpu.py,sha256=KyxDP7IuveidZrbW4rx2s8Ku3o_ptI6tzwr_R7ck0os,5548
80
+ accelerate/commands/utils.py,sha256=aT8xUCe2pCkFII7yZxcfaohEjgBAzMUM7WiD4UuWSOY,4150
81
+ accelerate/data_loader.py,sha256=yt6Rog94SMLJVMDsZjyrYuEGii5297p7N4jQM18bEZU,64850
82
+ accelerate/hooks.py,sha256=y4eN7blCjOkXY9_mVBmkgiTKqJfasAKrHOddEHj91Vg,33293
83
+ accelerate/inference.py,sha256=NLANdzXm5PwmDWbPYkFmoRoQSLLvuhfvIG33xfpapT0,7668
84
+ accelerate/launchers.py,sha256=GCj7y4qgK34k-VWi6EeM0DvXfsHBeGOZBfgTv9wi1zE,13724
85
+ accelerate/local_sgd.py,sha256=aCj_yqXK_FhhZRWEpzXIkgXBERH6fC3HyrC3nsOj1uA,4160
86
+ accelerate/logging.py,sha256=4XcgY_BV7Qn_enh2tZ-8fNtuaE_3n-LsYJbgwhRx_PI,5042
87
+ accelerate/memory_utils.py,sha256=3R5LoeHl6GgTZ-IMPrDZMdaEehWarGdPqODushb-6pg,862
88
+ accelerate/optimizer.py,sha256=oOz7vzvb_Np41V7iIPPcicf_sSm7g0utGsfZVkCseOM,8144
89
+ accelerate/scheduler.py,sha256=des_4M_Tt1W8gCYZZbLla0GHBEgJY3Wx2EGBQPTzeiY,4238
90
+ accelerate/state.py,sha256=ucpBxWn9nrE_6qTCKGAlziHASg3V712rQyEMScrwrlY,55922
91
+ accelerate/test_utils/__init__.py,sha256=ROl3bvBqmuI6ueRlPRNkNC-83yb6u6z_FFzrHJtq8rM,1809
92
+ accelerate/test_utils/__pycache__/__init__.cpython-312.pyc,,
93
+ accelerate/test_utils/__pycache__/examples.cpython-312.pyc,,
94
+ accelerate/test_utils/__pycache__/testing.cpython-312.pyc,,
95
+ accelerate/test_utils/__pycache__/training.cpython-312.pyc,,
96
+ accelerate/test_utils/examples.py,sha256=CO3SlkkrORUjroreoPfBPwfcNCDlHSgNWIt81iz2oQ8,7257
97
+ accelerate/test_utils/scripts/__init__.py,sha256=m1PPTDT4ziIAvM0-FDSgIMIZ69Konn126s6LwuzH6v8,606
98
+ accelerate/test_utils/scripts/__pycache__/__init__.cpython-312.pyc,,
99
+ accelerate/test_utils/scripts/__pycache__/test_cli.cpython-312.pyc,,
100
+ accelerate/test_utils/scripts/__pycache__/test_ddp_comm_hook.cpython-312.pyc,,
101
+ accelerate/test_utils/scripts/__pycache__/test_distributed_data_loop.cpython-312.pyc,,
102
+ accelerate/test_utils/scripts/__pycache__/test_merge_weights.cpython-312.pyc,,
103
+ accelerate/test_utils/scripts/__pycache__/test_notebook.cpython-312.pyc,,
104
+ accelerate/test_utils/scripts/__pycache__/test_ops.cpython-312.pyc,,
105
+ accelerate/test_utils/scripts/__pycache__/test_script.cpython-312.pyc,,
106
+ accelerate/test_utils/scripts/__pycache__/test_sync.cpython-312.pyc,,
107
+ accelerate/test_utils/scripts/external_deps/__init__.py,sha256=m1PPTDT4ziIAvM0-FDSgIMIZ69Konn126s6LwuzH6v8,606
108
+ accelerate/test_utils/scripts/external_deps/__pycache__/__init__.cpython-312.pyc,,
109
+ accelerate/test_utils/scripts/external_deps/__pycache__/test_checkpointing.cpython-312.pyc,,
110
+ accelerate/test_utils/scripts/external_deps/__pycache__/test_ds_multiple_model.cpython-312.pyc,,
111
+ accelerate/test_utils/scripts/external_deps/__pycache__/test_metrics.cpython-312.pyc,,
112
+ accelerate/test_utils/scripts/external_deps/__pycache__/test_peak_memory_usage.cpython-312.pyc,,
113
+ accelerate/test_utils/scripts/external_deps/__pycache__/test_performance.cpython-312.pyc,,
114
+ accelerate/test_utils/scripts/external_deps/__pycache__/test_pippy.cpython-312.pyc,,
115
+ accelerate/test_utils/scripts/external_deps/__pycache__/test_zero3_integration.cpython-312.pyc,,
116
+ accelerate/test_utils/scripts/external_deps/test_checkpointing.py,sha256=XHaNRmnrARd1izXFjWGi5UjYGas-4vqayW51jAHBPCA,10699
117
+ accelerate/test_utils/scripts/external_deps/test_ds_multiple_model.py,sha256=Cg4-h0B4UcOQ5CxXjIdrsPVR5fFsWCv24DqZGjXEwW8,13790
118
+ accelerate/test_utils/scripts/external_deps/test_metrics.py,sha256=Ev2XKaiwmznoxKujskAAuISGChW646MOiyf0CXEPb9Y,12168
119
+ accelerate/test_utils/scripts/external_deps/test_peak_memory_usage.py,sha256=9Yn9Rc7d-yWr1fU0RagASPG5l8vrKeHVYbuYABbA-fU,12498
120
+ accelerate/test_utils/scripts/external_deps/test_performance.py,sha256=4SW108BHEdpzDA_VY4B0GKKAdms4QxVlVywhZ-CZwRI,11721
121
+ accelerate/test_utils/scripts/external_deps/test_pippy.py,sha256=ocZntbmAduln2ma4LeEA9o-S8hla3YXCJ_A8hEcWHgs,4762
122
+ accelerate/test_utils/scripts/external_deps/test_zero3_integration.py,sha256=P9alBOHZ9Lfqs5LoRP7bCbXl-tnsNrBkvJZGseibBeA,1665
123
+ accelerate/test_utils/scripts/test_cli.py,sha256=qfk1aYFtdvYFCYPkl05602SNGvk08QTv0xZVVcFVtzM,833
124
+ accelerate/test_utils/scripts/test_ddp_comm_hook.py,sha256=k_-2MBjLKNdMGIcneTbuGd84K05Wp1GEQX6DUVF9UBw,3566
125
+ accelerate/test_utils/scripts/test_distributed_data_loop.py,sha256=RUWTwd7DIpr2fl7JtKOsvTjMiJioTxO8FdSr2Lw_5uI,15137
126
+ accelerate/test_utils/scripts/test_merge_weights.py,sha256=UQa5cR_gZK94fNuopsQ4FEAfTNEsTnlepCxXhboGEOg,6144
127
+ accelerate/test_utils/scripts/test_notebook.py,sha256=qfIy3IvH74-kGn8nadBn_k7qrviqvsxy5ijsnUhuY6o,3894
128
+ accelerate/test_utils/scripts/test_ops.py,sha256=Bcs-h8EMJwULTfbizlFN5qkv3JraWEpoSZWMn-HswiI,6265
129
+ accelerate/test_utils/scripts/test_script.py,sha256=A_AFMhT1lYqwd5AYMYdUe4B3Xf6e9cdiZwHpVVXFgUs,36460
130
+ accelerate/test_utils/scripts/test_sync.py,sha256=PDe8sYZLCL2LKjj_L9b-Bh2BjAjeii9EZ8sZNfuYx5s,18817
131
+ accelerate/test_utils/testing.py,sha256=b0x9j5ezzJbSPJp8nC22sHPQsttagLfZuYVD0g0Gdjo,28677
132
+ accelerate/test_utils/training.py,sha256=jO5YEIr34jAcnJ_9WNp_x3zuHzSam_I6IgMvmcGm7yI,6456
133
+ accelerate/tracking.py,sha256=ucpsoYAT3pVXgOfwDdXf6qTugY2-tk-EINvZtfmRitM,42756
134
+ accelerate/utils/__init__.py,sha256=R3I5NnGZPmAcom9RQvLgDxElXJHK21RCpBW5EUv9JuQ,7903
135
+ accelerate/utils/__pycache__/__init__.cpython-312.pyc,,
136
+ accelerate/utils/__pycache__/ao.cpython-312.pyc,,
137
+ accelerate/utils/__pycache__/bnb.cpython-312.pyc,,
138
+ accelerate/utils/__pycache__/constants.cpython-312.pyc,,
139
+ accelerate/utils/__pycache__/dataclasses.cpython-312.pyc,,
140
+ accelerate/utils/__pycache__/deepspeed.cpython-312.pyc,,
141
+ accelerate/utils/__pycache__/environment.cpython-312.pyc,,
142
+ accelerate/utils/__pycache__/fsdp_utils.cpython-312.pyc,,
143
+ accelerate/utils/__pycache__/imports.cpython-312.pyc,,
144
+ accelerate/utils/__pycache__/launch.cpython-312.pyc,,
145
+ accelerate/utils/__pycache__/megatron_lm.cpython-312.pyc,,
146
+ accelerate/utils/__pycache__/memory.cpython-312.pyc,,
147
+ accelerate/utils/__pycache__/modeling.cpython-312.pyc,,
148
+ accelerate/utils/__pycache__/offload.cpython-312.pyc,,
149
+ accelerate/utils/__pycache__/operations.cpython-312.pyc,,
150
+ accelerate/utils/__pycache__/other.cpython-312.pyc,,
151
+ accelerate/utils/__pycache__/random.cpython-312.pyc,,
152
+ accelerate/utils/__pycache__/rich.cpython-312.pyc,,
153
+ accelerate/utils/__pycache__/torch_xla.cpython-312.pyc,,
154
+ accelerate/utils/__pycache__/tqdm.cpython-312.pyc,,
155
+ accelerate/utils/__pycache__/transformer_engine.cpython-312.pyc,,
156
+ accelerate/utils/__pycache__/versions.cpython-312.pyc,,
157
+ accelerate/utils/ao.py,sha256=yfi84ywNpXsKZ6di4jbmtd6bIvRi9bmygr3giN92_Pg,4777
158
+ accelerate/utils/bnb.py,sha256=PdgdXwjhQ4Sg9orj4hlUiMQlPxSsdsiNfFVq4yBtl-g,20651
159
+ accelerate/utils/constants.py,sha256=A5KmcIry76B9p4Fa8CuI5ugv7bVcyTc5g4IBU1cvky0,3516
160
+ accelerate/utils/dataclasses.py,sha256=8ZF_sKfCh6XxBHhHQDI0arJrvk6vwXNtGXYz22xL5b4,133891
161
+ accelerate/utils/deepspeed.py,sha256=QYIXv5LwHXw7wBFFo-7a0t86MbwNAfieJkkBaLGA6wI,14064
162
+ accelerate/utils/environment.py,sha256=h0zacbBkAp9szltTf5-aTr5NcbVsQp7wl6DFWp8XNuI,15257
163
+ accelerate/utils/fsdp_utils.py,sha256=6vYmHrW3FBpsCTd3EInqfsSCtT80ohKm2qmflfRjJcg,36005
164
+ accelerate/utils/imports.py,sha256=YI1ebPJAuxarclENTfzvDPPGf6jeEKnVQ42taFPuqh0,16759
165
+ accelerate/utils/launch.py,sha256=sgJREpAYakqGJSSWB6ls2NKrYMZT_nzxg2VRAMpLAQ4,31721
166
+ accelerate/utils/megatron_lm.py,sha256=ZMxcYAdGnMWla2PtB04ctb9mhMfKUP8qLYusGNjyScU,58054
167
+ accelerate/utils/memory.py,sha256=Tuyr1Xnhk9Vz5HyCROBEZmrCmDz3-ArYoZN6xKFDnEc,7201
168
+ accelerate/utils/modeling.py,sha256=yA4tg20fnMixT-mZG7dmHPyZRwqT_P2I6Rj_pvVtMZM,94633
169
+ accelerate/utils/offload.py,sha256=VFaL8oSJzqZ_47VuUQ69xZi9bF2heRSFoOSnnOxbGXc,7825
170
+ accelerate/utils/operations.py,sha256=84SpXwKsbNHHePboL9gUqK6bA5vo0kyH_gLAdwnzE_s,31266
171
+ accelerate/utils/other.py,sha256=mgLiIDxqtAGAOwfWqXYmP-2sRZqq6q3Rbp1Lw5QR-z0,17369
172
+ accelerate/utils/random.py,sha256=Xv_ZJm9eaC2Q7rgZy9OpOunKuTingMiDQCH00qhNVxE,6220
173
+ accelerate/utils/rich.py,sha256=8JZX_uGMQX-BufdXxJpdne7BWd1KyLHSgbiGxrDMYr8,847
174
+ accelerate/utils/torch_xla.py,sha256=Pq1tuqN0X_pWDVza6YgjfO45uoJdoRVRForLeLQzFus,1908
175
+ accelerate/utils/tqdm.py,sha256=k8e9JnieTEQHCCNBaiBys7hPxWlEbyRASdIma-qy_X8,1657
176
+ accelerate/utils/transformer_engine.py,sha256=498Y3z2BkbybYLtBiuF_TJgt8Iii943s4wgRAV8FDC4,6372
177
+ accelerate/utils/versions.py,sha256=UgmcbjBm--6CIx1ZamSAMjAK_B_2l48LbeaNygqej8M,2149
lib/python3.12/site-packages/accelerate-1.7.0.dist-info/REQUESTED ADDED
File without changes
lib/python3.12/site-packages/accelerate-1.7.0.dist-info/WHEEL ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.1.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
lib/python3.12/site-packages/accelerate-1.7.0.dist-info/entry_points.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ [console_scripts]
2
+ accelerate = accelerate.commands.accelerate_cli:main
3
+ accelerate-config = accelerate.commands.config:main
4
+ accelerate-estimate-memory = accelerate.commands.estimate:main
5
+ accelerate-launch = accelerate.commands.launch:main
6
+ accelerate-merge-weights = accelerate.commands.merge:main
lib/python3.12/site-packages/accelerate-1.7.0.dist-info/top_level.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ accelerate
lib/python3.12/site-packages/accelerate/__init__.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ __version__ = "1.7.0"
15
+
16
+ from .accelerator import Accelerator
17
+ from .big_modeling import (
18
+ cpu_offload,
19
+ cpu_offload_with_hook,
20
+ disk_offload,
21
+ dispatch_model,
22
+ init_empty_weights,
23
+ init_on_device,
24
+ load_checkpoint_and_dispatch,
25
+ )
26
+ from .data_loader import skip_first_batches
27
+ from .inference import prepare_pippy
28
+ from .launchers import debug_launcher, notebook_launcher
29
+ from .state import PartialState
30
+ from .utils import (
31
+ AutocastKwargs,
32
+ DataLoaderConfiguration,
33
+ DDPCommunicationHookType,
34
+ DeepSpeedPlugin,
35
+ DistributedDataParallelKwargs,
36
+ DistributedType,
37
+ FullyShardedDataParallelPlugin,
38
+ GradScalerKwargs,
39
+ InitProcessGroupKwargs,
40
+ ProfileKwargs,
41
+ find_executable_batch_size,
42
+ infer_auto_device_map,
43
+ is_rich_available,
44
+ load_checkpoint_in_model,
45
+ synchronize_rng_states,
46
+ )
47
+
48
+
49
+ if is_rich_available():
50
+ from .utils import rich
lib/python3.12/site-packages/accelerate/accelerator.py ADDED
The diff for this file is too large to render. See raw diff
 
lib/python3.12/site-packages/accelerate/big_modeling.py ADDED
@@ -0,0 +1,749 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2022 The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import logging
16
+ import os
17
+ import re
18
+ from contextlib import contextmanager
19
+ from functools import wraps
20
+ from typing import Optional, Union
21
+
22
+ import torch
23
+ import torch.nn as nn
24
+
25
+ from .hooks import (
26
+ AlignDevicesHook,
27
+ CpuOffload,
28
+ LayerwiseCastingHook,
29
+ UserCpuOffloadHook,
30
+ add_hook_to_module,
31
+ attach_align_device_hook,
32
+ attach_align_device_hook_on_blocks,
33
+ )
34
+ from .utils import (
35
+ OffloadedWeightsLoader,
36
+ check_cuda_p2p_ib_support,
37
+ check_device_map,
38
+ extract_submodules_state_dict,
39
+ find_tied_parameters,
40
+ get_balanced_memory,
41
+ infer_auto_device_map,
42
+ is_bnb_available,
43
+ is_mlu_available,
44
+ is_musa_available,
45
+ is_npu_available,
46
+ is_sdaa_available,
47
+ is_xpu_available,
48
+ load_checkpoint_in_model,
49
+ offload_state_dict,
50
+ parse_flag_from_env,
51
+ retie_parameters,
52
+ )
53
+ from .utils.constants import SUPPORTED_PYTORCH_LAYERS_FOR_UPCASTING
54
+ from .utils.other import recursive_getattr
55
+
56
+
57
+ logger = logging.getLogger(__name__)
58
+
59
+
60
+ @contextmanager
61
+ def init_empty_weights(include_buffers: bool = None):
62
+ """
63
+ A context manager under which models are initialized with all parameters on the meta device, therefore creating an
64
+ empty model. Useful when just initializing the model would blow the available RAM.
65
+
66
+ Args:
67
+ include_buffers (`bool`, *optional*):
68
+ Whether or not to also put all buffers on the meta device while initializing.
69
+
70
+ Example:
71
+
72
+ ```python
73
+ import torch.nn as nn
74
+ from accelerate import init_empty_weights
75
+
76
+ # Initialize a model with 100 billions parameters in no time and without using any RAM.
77
+ with init_empty_weights():
78
+ tst = nn.Sequential(*[nn.Linear(10000, 10000) for _ in range(1000)])
79
+ ```
80
+
81
+ <Tip warning={true}>
82
+
83
+ Any model created under this context manager has no weights. As such you can't do something like
84
+ `model.to(some_device)` with it. To load weights inside your empty model, see [`load_checkpoint_and_dispatch`].
85
+ Make sure to overwrite the default device_map param for [`load_checkpoint_and_dispatch`], otherwise dispatch is not
86
+ called.
87
+
88
+ </Tip>
89
+ """
90
+ if include_buffers is None:
91
+ include_buffers = parse_flag_from_env("ACCELERATE_INIT_INCLUDE_BUFFERS", False)
92
+ with init_on_device(torch.device("meta"), include_buffers=include_buffers) as f:
93
+ yield f
94
+
95
+
96
+ @contextmanager
97
+ def init_on_device(device: torch.device, include_buffers: bool = None):
98
+ """
99
+ A context manager under which models are initialized with all parameters on the specified device.
100
+
101
+ Args:
102
+ device (`torch.device`):
103
+ Device to initialize all parameters on.
104
+ include_buffers (`bool`, *optional*):
105
+ Whether or not to also put all buffers on the meta device while initializing.
106
+
107
+ Example:
108
+
109
+ ```python
110
+ import torch.nn as nn
111
+ from accelerate import init_on_device
112
+
113
+ with init_on_device(device=torch.device("cuda")):
114
+ tst = nn.Linear(100, 100) # on `cuda` device
115
+ ```
116
+ """
117
+ if include_buffers is None:
118
+ include_buffers = parse_flag_from_env("ACCELERATE_INIT_INCLUDE_BUFFERS", False)
119
+
120
+ if include_buffers:
121
+ with device:
122
+ yield
123
+ return
124
+
125
+ old_register_parameter = nn.Module.register_parameter
126
+ if include_buffers:
127
+ old_register_buffer = nn.Module.register_buffer
128
+
129
+ def register_empty_parameter(module, name, param):
130
+ old_register_parameter(module, name, param)
131
+ if param is not None:
132
+ param_cls = type(module._parameters[name])
133
+ kwargs = module._parameters[name].__dict__
134
+ kwargs["requires_grad"] = param.requires_grad
135
+ module._parameters[name] = param_cls(module._parameters[name].to(device), **kwargs)
136
+
137
+ def register_empty_buffer(module, name, buffer, persistent=True):
138
+ old_register_buffer(module, name, buffer, persistent=persistent)
139
+ if buffer is not None:
140
+ module._buffers[name] = module._buffers[name].to(device)
141
+
142
+ # Patch tensor creation
143
+ if include_buffers:
144
+ tensor_constructors_to_patch = {
145
+ torch_function_name: getattr(torch, torch_function_name)
146
+ for torch_function_name in ["empty", "zeros", "ones", "full"]
147
+ }
148
+ else:
149
+ tensor_constructors_to_patch = {}
150
+
151
+ def patch_tensor_constructor(fn):
152
+ def wrapper(*args, **kwargs):
153
+ kwargs["device"] = device
154
+ return fn(*args, **kwargs)
155
+
156
+ return wrapper
157
+
158
+ try:
159
+ nn.Module.register_parameter = register_empty_parameter
160
+ if include_buffers:
161
+ nn.Module.register_buffer = register_empty_buffer
162
+ for torch_function_name in tensor_constructors_to_patch.keys():
163
+ setattr(torch, torch_function_name, patch_tensor_constructor(getattr(torch, torch_function_name)))
164
+ yield
165
+ finally:
166
+ nn.Module.register_parameter = old_register_parameter
167
+ if include_buffers:
168
+ nn.Module.register_buffer = old_register_buffer
169
+ for torch_function_name, old_torch_function in tensor_constructors_to_patch.items():
170
+ setattr(torch, torch_function_name, old_torch_function)
171
+
172
+
173
+ def cpu_offload(
174
+ model: nn.Module,
175
+ execution_device: Optional[torch.device] = None,
176
+ offload_buffers: bool = False,
177
+ state_dict: Optional[dict[str, torch.Tensor]] = None,
178
+ preload_module_classes: Optional[list[str]] = None,
179
+ ):
180
+ """
181
+ Activates full CPU offload for a model. As a result, all parameters of the model will be offloaded and only one
182
+ copy of the state dict of the model will be kept. During the forward pass, parameters will be extracted from that
183
+ state dict and put on the execution device passed as they are needed, then offloaded again.
184
+
185
+ Args:
186
+ model (`torch.nn.Module`):
187
+ The model to offload.
188
+ execution_device (`torch.device`, *optional*):
189
+ The device on which the forward pass of the model will be executed (should be a GPU). Will default to the
190
+ model first parameter device.
191
+ offload_buffers (`bool`, *optional*, defaults to `False`):
192
+ Whether or not to offload the buffers with the model parameters.
193
+ state_dict (`Dict[str, torch.Tensor]`, *optional*):
194
+ The state dict of the model that will be kept on CPU.
195
+ preload_module_classes (`List[str]`, *optional*):
196
+ A list of classes whose instances should load all their weights (even in the submodules) at the beginning
197
+ of the forward. This should only be used for classes that have submodules which are registered but not
198
+ called directly during the forward, for instance if a `dense` linear layer is registered, but at forward,
199
+ `dense.weight` and `dense.bias` are used in some operations instead of calling `dense` directly.
200
+ """
201
+ if execution_device is None:
202
+ execution_device = next(iter(model.parameters())).device
203
+ if state_dict is None:
204
+ state_dict = {n: p.to("cpu") for n, p in model.state_dict().items()}
205
+
206
+ add_hook_to_module(model, AlignDevicesHook(io_same_device=True), append=True)
207
+ attach_align_device_hook(
208
+ model,
209
+ execution_device=execution_device,
210
+ offload=True,
211
+ offload_buffers=offload_buffers,
212
+ weights_map=state_dict,
213
+ preload_module_classes=preload_module_classes,
214
+ )
215
+
216
+ return model
217
+
218
+
219
+ def cpu_offload_with_hook(
220
+ model: torch.nn.Module,
221
+ execution_device: Optional[Union[int, str, torch.device]] = None,
222
+ prev_module_hook: Optional[UserCpuOffloadHook] = None,
223
+ ):
224
+ """
225
+ Offloads a model on the CPU and puts it back to an execution device when executed. The difference with
226
+ [`cpu_offload`] is that the model stays on the execution device after the forward and is only offloaded again when
227
+ the `offload` method of the returned `hook` is called. Useful for pipelines running a model in a loop.
228
+
229
+ Args:
230
+ model (`torch.nn.Module`):
231
+ The model to offload.
232
+ execution_device(`str`, `int` or `torch.device`, *optional*):
233
+ The device on which the model should be executed. Will default to the MPS device if it's available, then
234
+ GPU 0 if there is a GPU, and finally to the CPU.
235
+ prev_module_hook (`UserCpuOffloadHook`, *optional*):
236
+ The hook sent back by this function for a previous model in the pipeline you are running. If passed, its
237
+ offload method will be called just before the forward of the model to which this hook is attached.
238
+
239
+ Example:
240
+
241
+ ```py
242
+ model_1, hook_1 = cpu_offload_with_hook(model_1, cuda_device)
243
+ model_2, hook_2 = cpu_offload_with_hook(model_2, cuda_device, prev_module_hook=hook_1)
244
+ model_3, hook_3 = cpu_offload_with_hook(model_3, cuda_device, prev_module_hook=hook_2)
245
+
246
+ hid_1 = model_1(input)
247
+ for i in range(50):
248
+ # model1 is offloaded on the CPU at the first iteration, model 2 stays on the GPU for this whole loop.
249
+ hid_2 = model_2(hid_1)
250
+ # model2 is offloaded to the CPU just before this forward.
251
+ hid_3 = model_3(hid_3)
252
+
253
+ # For model3, you need to manually call the hook offload method.
254
+ hook_3.offload()
255
+ ```
256
+ """
257
+ hook = CpuOffload(execution_device=execution_device, prev_module_hook=prev_module_hook)
258
+ add_hook_to_module(model, hook, append=True)
259
+ user_hook = UserCpuOffloadHook(model, hook)
260
+ return model, user_hook
261
+
262
+
263
+ def disk_offload(
264
+ model: nn.Module,
265
+ offload_dir: Union[str, os.PathLike],
266
+ execution_device: Optional[torch.device] = None,
267
+ offload_buffers: bool = False,
268
+ preload_module_classes: Optional[list[str]] = None,
269
+ ):
270
+ """
271
+ Activates full disk offload for a model. As a result, all parameters of the model will be offloaded as
272
+ memory-mapped array in a given folder. During the forward pass, parameters will be accessed from that folder and
273
+ put on the execution device passed as they are needed, then offloaded again.
274
+
275
+ Args:
276
+ model (`torch.nn.Module`): The model to offload.
277
+ offload_dir (`str` or `os.PathLike`):
278
+ The folder in which to offload the model weights (or where the model weights are already offloaded).
279
+ execution_device (`torch.device`, *optional*):
280
+ The device on which the forward pass of the model will be executed (should be a GPU). Will default to the
281
+ model's first parameter device.
282
+ offload_buffers (`bool`, *optional*, defaults to `False`):
283
+ Whether or not to offload the buffers with the model parameters.
284
+ preload_module_classes (`List[str]`, *optional*):
285
+ A list of classes whose instances should load all their weights (even in the submodules) at the beginning
286
+ of the forward. This should only be used for classes that have submodules which are registered but not
287
+ called directly during the forward, for instance if a `dense` linear layer is registered, but at forward,
288
+ `dense.weight` and `dense.bias` are used in some operations instead of calling `dense` directly.
289
+ """
290
+ if not os.path.isdir(offload_dir) or not os.path.isfile(os.path.join(offload_dir, "index.json")):
291
+ offload_state_dict(offload_dir, model.state_dict())
292
+ if execution_device is None:
293
+ execution_device = next(iter(model.parameters())).device
294
+ weights_map = OffloadedWeightsLoader(save_folder=offload_dir)
295
+
296
+ add_hook_to_module(model, AlignDevicesHook(io_same_device=True), append=True)
297
+ attach_align_device_hook(
298
+ model,
299
+ execution_device=execution_device,
300
+ offload=True,
301
+ offload_buffers=offload_buffers,
302
+ weights_map=weights_map,
303
+ preload_module_classes=preload_module_classes,
304
+ )
305
+
306
+ return model
307
+
308
+
309
+ def dispatch_model(
310
+ model: nn.Module,
311
+ device_map: dict[str, Union[str, int, torch.device]],
312
+ main_device: Optional[torch.device] = None,
313
+ state_dict: Optional[dict[str, torch.Tensor]] = None,
314
+ offload_dir: Optional[Union[str, os.PathLike]] = None,
315
+ offload_index: Optional[dict[str, str]] = None,
316
+ offload_buffers: bool = False,
317
+ skip_keys: Optional[Union[str, list[str]]] = None,
318
+ preload_module_classes: Optional[list[str]] = None,
319
+ force_hooks: bool = False,
320
+ ):
321
+ """
322
+ Dispatches a model according to a given device map. Layers of the model might be spread across GPUs, offloaded on
323
+ the CPU or even the disk.
324
+
325
+ Args:
326
+ model (`torch.nn.Module`):
327
+ The model to dispatch.
328
+ device_map (`Dict[str, Union[str, int, torch.device]]`):
329
+ A dictionary mapping module names in the models `state_dict` to the device they should go to. Note that
330
+ `"disk"` is accepted even if it's not a proper value for `torch.device`.
331
+ main_device (`str`, `int` or `torch.device`, *optional*):
332
+ The main execution device. Will default to the first device in the `device_map` different from `"cpu"` or
333
+ `"disk"`.
334
+ state_dict (`Dict[str, torch.Tensor]`, *optional*):
335
+ The state dict of the part of the model that will be kept on CPU.
336
+ offload_dir (`str` or `os.PathLike`):
337
+ The folder in which to offload the model weights (or where the model weights are already offloaded).
338
+ offload_index (`Dict`, *optional*):
339
+ A dictionary from weight name to their information (`dtype`/ `shape` or safetensors filename). Will default
340
+ to the index saved in `save_folder`.
341
+ offload_buffers (`bool`, *optional*, defaults to `False`):
342
+ Whether or not to offload the buffers with the model parameters.
343
+ skip_keys (`str` or `List[str]`, *optional*):
344
+ A list of keys to ignore when moving inputs or outputs between devices.
345
+ preload_module_classes (`List[str]`, *optional*):
346
+ A list of classes whose instances should load all their weights (even in the submodules) at the beginning
347
+ of the forward. This should only be used for classes that have submodules which are registered but not
348
+ called directly during the forward, for instance if a `dense` linear layer is registered, but at forward,
349
+ `dense.weight` and `dense.bias` are used in some operations instead of calling `dense` directly.
350
+ force_hooks (`bool`, *optional*, defaults to `False`):
351
+ Whether or not to force device hooks to be attached to the model even if all layers are dispatched to a
352
+ single device.
353
+ """
354
+ # Error early if the device map is incomplete.
355
+ check_device_map(model, device_map)
356
+
357
+ # We need to force hook for quantized model that can't be moved with to()
358
+ if getattr(model, "quantization_method", "bitsandbytes") == "bitsandbytes":
359
+ # since bnb 0.43.2, we can move 4-bit model
360
+ if getattr(model, "is_loaded_in_8bit", False) or (
361
+ getattr(model, "is_loaded_in_4bit", False) and not is_bnb_available(min_version="0.43.2")
362
+ ):
363
+ force_hooks = True
364
+
365
+ # We attach hooks if the device_map has at least 2 different devices or if
366
+ # force_hooks is set to `True`. Otherwise, the model in already loaded
367
+ # in the unique device and the user can decide where to dispatch the model.
368
+ # If the model is quantized, we always force-dispatch the model
369
+ if (len(set(device_map.values())) > 1) or force_hooks:
370
+ if main_device is None:
371
+ if set(device_map.values()) == {"cpu"} or set(device_map.values()) == {"cpu", "disk"}:
372
+ main_device = "cpu"
373
+ else:
374
+ main_device = [d for d in device_map.values() if d not in ["cpu", "disk"]][0]
375
+
376
+ if main_device != "cpu":
377
+ cpu_modules = [name for name, device in device_map.items() if device == "cpu"]
378
+ if state_dict is None and len(cpu_modules) > 0:
379
+ state_dict = extract_submodules_state_dict(model.state_dict(), cpu_modules)
380
+
381
+ disk_modules = [name for name, device in device_map.items() if device == "disk"]
382
+ if offload_dir is None and offload_index is None and len(disk_modules) > 0:
383
+ raise ValueError(
384
+ "We need an `offload_dir` to dispatch this model according to this `device_map`, the following submodules "
385
+ f"need to be offloaded: {', '.join(disk_modules)}."
386
+ )
387
+ if (
388
+ len(disk_modules) > 0
389
+ and offload_index is None
390
+ and (not os.path.isdir(offload_dir) or not os.path.isfile(os.path.join(offload_dir, "index.json")))
391
+ ):
392
+ disk_state_dict = extract_submodules_state_dict(model.state_dict(), disk_modules)
393
+ offload_state_dict(offload_dir, disk_state_dict)
394
+
395
+ execution_device = {
396
+ name: main_device if device in ["cpu", "disk"] else device for name, device in device_map.items()
397
+ }
398
+ execution_device[""] = main_device
399
+ offloaded_devices = ["disk"] if main_device == "cpu" or main_device == "mps" else ["cpu", "disk"]
400
+ offload = {name: device in offloaded_devices for name, device in device_map.items()}
401
+ save_folder = offload_dir if len(disk_modules) > 0 else None
402
+ if state_dict is not None or save_folder is not None or offload_index is not None:
403
+ device = main_device if offload_index is not None else None
404
+ weights_map = OffloadedWeightsLoader(
405
+ state_dict=state_dict, save_folder=save_folder, index=offload_index, device=device
406
+ )
407
+ else:
408
+ weights_map = None
409
+
410
+ # When dispatching the model's parameters to the devices specified in device_map, we want to avoid allocating memory several times for the
411
+ # tied parameters. The dictionary tied_params_map keeps track of the already allocated data for a given tied parameter (represented by its
412
+ # original pointer) on each devices.
413
+ tied_params = find_tied_parameters(model)
414
+
415
+ tied_params_map = {}
416
+ for group in tied_params:
417
+ for param_name in group:
418
+ # data_ptr() is enough here, as `find_tied_parameters` finds tied params simply by comparing `param1 is param2`, so we don't need
419
+ # to care about views of tensors through storage_offset.
420
+ data_ptr = recursive_getattr(model, param_name).data_ptr()
421
+ tied_params_map[data_ptr] = {}
422
+
423
+ # Note: To handle the disk offloading case, we can not simply use weights_map[param_name].data_ptr() as the reference pointer,
424
+ # as we have no guarantee that safetensors' `file.get_tensor()` will always give the same pointer.
425
+
426
+ attach_align_device_hook_on_blocks(
427
+ model,
428
+ execution_device=execution_device,
429
+ offload=offload,
430
+ offload_buffers=offload_buffers,
431
+ weights_map=weights_map,
432
+ skip_keys=skip_keys,
433
+ preload_module_classes=preload_module_classes,
434
+ tied_params_map=tied_params_map,
435
+ )
436
+
437
+ # warn if there is any params on the meta device
438
+ offloaded_devices_str = " and ".join(
439
+ [device for device in set(device_map.values()) if device in ("cpu", "disk")]
440
+ )
441
+ if len(offloaded_devices_str) > 0:
442
+ logger.warning(
443
+ f"Some parameters are on the meta device because they were offloaded to the {offloaded_devices_str}."
444
+ )
445
+
446
+ # Attaching the hook may break tied weights, so we retie them
447
+ retie_parameters(model, tied_params)
448
+
449
+ # add warning to cuda and to method
450
+ def add_warning(fn, model):
451
+ @wraps(fn)
452
+ def wrapper(*args, **kwargs):
453
+ warning_msg = "You shouldn't move a model that is dispatched using accelerate hooks."
454
+ if str(fn.__name__) == "to":
455
+ to_device = torch._C._nn._parse_to(*args, **kwargs)[0]
456
+ if to_device is not None:
457
+ logger.warning(warning_msg)
458
+ else:
459
+ logger.warning(warning_msg)
460
+ for param in model.parameters():
461
+ if param.device == torch.device("meta"):
462
+ raise RuntimeError("You can't move a model that has some modules offloaded to cpu or disk.")
463
+ return fn(*args, **kwargs)
464
+
465
+ return wrapper
466
+
467
+ # Make sure to update _accelerate_added_attributes in hooks.py if you add any hook
468
+ model.to = add_warning(model.to, model)
469
+ if is_npu_available():
470
+ model.npu = add_warning(model.npu, model)
471
+ elif is_mlu_available():
472
+ model.mlu = add_warning(model.mlu, model)
473
+ elif is_sdaa_available():
474
+ model.sdaa = add_warning(model.sdaa, model)
475
+ elif is_musa_available():
476
+ model.musa = add_warning(model.musa, model)
477
+ elif is_xpu_available():
478
+ model.xpu = add_warning(model.xpu, model)
479
+ else:
480
+ model.cuda = add_warning(model.cuda, model)
481
+
482
+ # Check if we are using multi-gpus with RTX 4000 series
483
+ use_multi_gpu = len([device for device in set(device_map.values()) if device not in ("cpu", "disk")]) > 1
484
+ if use_multi_gpu and not check_cuda_p2p_ib_support():
485
+ logger.warning(
486
+ "We've detected an older driver with an RTX 4000 series GPU. These drivers have issues with P2P. "
487
+ "This can affect the multi-gpu inference when using accelerate device_map."
488
+ "Please make sure to update your driver to the latest version which resolves this."
489
+ )
490
+ else:
491
+ device = list(device_map.values())[0]
492
+ # `torch.Tensor.to(<int num>)` is not supported by `torch_npu` (see this [issue](https://github.com/Ascend/pytorch/issues/16)).
493
+ if is_npu_available() and isinstance(device, int):
494
+ device = f"npu:{device}"
495
+ elif is_mlu_available() and isinstance(device, int):
496
+ device = f"mlu:{device}"
497
+ elif is_sdaa_available() and isinstance(device, int):
498
+ device = f"sdaa:{device}"
499
+ elif is_musa_available() and isinstance(device, int):
500
+ device = f"musa:{device}"
501
+ if device != "disk":
502
+ model.to(device)
503
+ else:
504
+ raise ValueError(
505
+ "You are trying to offload the whole model to the disk. Please use the `disk_offload` function instead."
506
+ )
507
+ # Convert OrderedDict back to dict for easier usage
508
+ model.hf_device_map = dict(device_map)
509
+ return model
510
+
511
+
512
+ def load_checkpoint_and_dispatch(
513
+ model: nn.Module,
514
+ checkpoint: Union[str, os.PathLike],
515
+ device_map: Optional[Union[str, dict[str, Union[int, str, torch.device]]]] = None,
516
+ max_memory: Optional[dict[Union[int, str], Union[int, str]]] = None,
517
+ no_split_module_classes: Optional[list[str]] = None,
518
+ offload_folder: Optional[Union[str, os.PathLike]] = None,
519
+ offload_buffers: bool = False,
520
+ dtype: Optional[Union[str, torch.dtype]] = None,
521
+ offload_state_dict: Optional[bool] = None,
522
+ skip_keys: Optional[Union[str, list[str]]] = None,
523
+ preload_module_classes: Optional[list[str]] = None,
524
+ force_hooks: bool = False,
525
+ strict: bool = False,
526
+ full_state_dict: bool = True,
527
+ broadcast_from_rank0: bool = False,
528
+ ):
529
+ """
530
+ Loads a (potentially sharded) checkpoint inside a model, potentially sending weights to a given device as they are
531
+ loaded and adds the various hooks that will make this model run properly (even if split across devices).
532
+
533
+ Args:
534
+ model (`torch.nn.Module`): The model in which we want to load a checkpoint.
535
+ checkpoint (`str` or `os.PathLike`):
536
+ The folder checkpoint to load. It can be:
537
+ - a path to a file containing a whole model state dict
538
+ - a path to a `.json` file containing the index to a sharded checkpoint
539
+ - a path to a folder containing a unique `.index.json` file and the shards of a checkpoint.
540
+ device_map (`Dict[str, Union[int, str, torch.device]]`, *optional*):
541
+ A map that specifies where each submodule should go. It doesn't need to be refined to each parameter/buffer
542
+ name, once a given module name is inside, every submodule of it will be sent to the same device.
543
+
544
+ To have Accelerate compute the most optimized `device_map` automatically, set `device_map="auto"`. For more
545
+ information about each option see [here](../concept_guides/big_model_inference#designing-a-device-map).
546
+ Defaults to None, which means [`dispatch_model`] will not be called.
547
+ max_memory (`Dict`, *optional*):
548
+ A dictionary device identifier to maximum memory. Will default to the maximum memory available for each GPU
549
+ and the available CPU RAM if unset.
550
+ no_split_module_classes (`List[str]`, *optional*):
551
+ A list of layer class names that should never be split across device (for instance any layer that has a
552
+ residual connection).
553
+ offload_folder (`str` or `os.PathLike`, *optional*):
554
+ If the `device_map` contains any value `"disk"`, the folder where we will offload weights.
555
+ offload_buffers (`bool`, *optional*, defaults to `False`):
556
+ In the layers that are offloaded on the CPU or the hard drive, whether or not to offload the buffers as
557
+ well as the parameters.
558
+ dtype (`str` or `torch.dtype`, *optional*):
559
+ If provided, the weights will be converted to that type when loaded.
560
+ offload_state_dict (`bool`, *optional*):
561
+ If `True`, will temporarily offload the CPU state dict on the hard drive to avoid getting out of CPU RAM if
562
+ the weight of the CPU state dict + the biggest shard does not fit. Will default to `True` if the device map
563
+ picked contains `"disk"` values.
564
+ skip_keys (`str` or `List[str]`, *optional*):
565
+ A list of keys to ignore when moving inputs or outputs between devices.
566
+ preload_module_classes (`List[str]`, *optional*):
567
+ A list of classes whose instances should load all their weights (even in the submodules) at the beginning
568
+ of the forward. This should only be used for classes that have submodules which are registered but not
569
+ called directly during the forward, for instance if a `dense` linear layer is registered, but at forward,
570
+ `dense.weight` and `dense.bias` are used in some operations instead of calling `dense` directly.
571
+ force_hooks (`bool`, *optional*, defaults to `False`):
572
+ Whether or not to force device hooks to be attached to the model even if all layers are dispatched to a
573
+ single device.
574
+ strict (`bool`, *optional*, defaults to `False`):
575
+ Whether to strictly enforce that the keys in the checkpoint state_dict match the keys of the model's
576
+ state_dict.
577
+ full_state_dict (`bool`, *optional*, defaults to `True`): if this is set to `True`, all the tensors in the
578
+ loaded state_dict will be gathered. No ShardedTensor and DTensor will be in the loaded state_dict.
579
+ broadcast_from_rank0 (`False`, *optional*, defaults to `False`): when the option is `True`, a distributed
580
+ `ProcessGroup` must be initialized. rank0 should receive a full state_dict and will broadcast the tensors
581
+ in the state_dict one by one to other ranks. Other ranks will receive the tensors and shard (if applicable)
582
+ according to the local shards in the model.
583
+
584
+ Example:
585
+
586
+ ```python
587
+ >>> from accelerate import init_empty_weights, load_checkpoint_and_dispatch
588
+ >>> from huggingface_hub import hf_hub_download
589
+ >>> from transformers import AutoConfig, AutoModelForCausalLM
590
+
591
+ >>> # Download the Weights
592
+ >>> checkpoint = "EleutherAI/gpt-j-6B"
593
+ >>> weights_location = hf_hub_download(checkpoint, "pytorch_model.bin")
594
+
595
+ >>> # Create a model and initialize it with empty weights
596
+ >>> config = AutoConfig.from_pretrained(checkpoint)
597
+ >>> with init_empty_weights():
598
+ ... model = AutoModelForCausalLM.from_config(config)
599
+
600
+ >>> # Load the checkpoint and dispatch it to the right devices
601
+ >>> model = load_checkpoint_and_dispatch(
602
+ ... model, weights_location, device_map="auto", no_split_module_classes=["GPTJBlock"]
603
+ ... )
604
+ ```
605
+ """
606
+ if isinstance(device_map, str) and device_map not in ["auto", "balanced", "balanced_low_0", "sequential"]:
607
+ raise ValueError(
608
+ "If passing a string for `device_map`, please choose 'auto', 'balanced', 'balanced_low_0' or 'sequential'."
609
+ )
610
+ if isinstance(device_map, str):
611
+ if device_map != "sequential":
612
+ max_memory = get_balanced_memory(
613
+ model,
614
+ max_memory=max_memory,
615
+ no_split_module_classes=no_split_module_classes,
616
+ dtype=dtype,
617
+ low_zero=(device_map == "balanced_low_0"),
618
+ )
619
+ device_map = infer_auto_device_map(
620
+ model,
621
+ max_memory=max_memory,
622
+ no_split_module_classes=no_split_module_classes,
623
+ dtype=dtype,
624
+ offload_buffers=offload_buffers,
625
+ )
626
+ if offload_state_dict is None and device_map is not None and "disk" in device_map.values():
627
+ offload_state_dict = True
628
+ load_checkpoint_in_model(
629
+ model,
630
+ checkpoint,
631
+ device_map=device_map,
632
+ offload_folder=offload_folder,
633
+ dtype=dtype,
634
+ offload_state_dict=offload_state_dict,
635
+ offload_buffers=offload_buffers,
636
+ strict=strict,
637
+ full_state_dict=full_state_dict,
638
+ broadcast_from_rank0=broadcast_from_rank0,
639
+ )
640
+ if device_map is None:
641
+ return model
642
+ return dispatch_model(
643
+ model,
644
+ device_map=device_map,
645
+ offload_dir=offload_folder,
646
+ offload_buffers=offload_buffers,
647
+ skip_keys=skip_keys,
648
+ preload_module_classes=preload_module_classes,
649
+ force_hooks=force_hooks,
650
+ )
651
+
652
+
653
+ def attach_layerwise_casting_hooks(
654
+ module: torch.nn.Module,
655
+ storage_dtype: torch.dtype,
656
+ compute_dtype: torch.dtype,
657
+ skip_modules_pattern: Union[str, tuple[str, ...]] = None,
658
+ skip_modules_classes: Optional[tuple[type[torch.nn.Module], ...]] = None,
659
+ non_blocking: bool = False,
660
+ ) -> None:
661
+ r"""
662
+ Applies layerwise casting to a given module. The module expected here is a PyTorch `nn.Module`. This is helpful for
663
+ reducing memory requirements when one doesn't want to fully quantize a model. Model params can be kept in say,
664
+ `torch.float8_e4m3fn` and upcasted to a higher precision like `torch.bfloat16` during forward pass and downcasted
665
+ back to `torch.float8_e4m3fn` to realize memory savings.
666
+
667
+ Args:
668
+ module (`torch.nn.Module`):
669
+ The module whose leaf modules will be cast to a high precision dtype for computation, and to a low
670
+ precision dtype for storage.
671
+ storage_dtype (`torch.dtype`):
672
+ The dtype to cast the module to before/after the forward pass for storage.
673
+ compute_dtype (`torch.dtype`):
674
+ The dtype to cast the module to during the forward pass for computation.
675
+ skip_modules_pattern (`tuple[str, ...]`, defaults to `None`):
676
+ A list of patterns to match the names of the modules to skip during the layerwise casting process. If set
677
+ to `None` alongside `skip_modules_classes` being `None`, the layerwise casting is applied directly to the
678
+ module instead of its internal submodules.
679
+ skip_modules_classes (`tuple[type[torch.nn.Module], ...]`, defaults to `None`):
680
+ A list of module classes to skip during the layerwise casting process.
681
+ non_blocking (`bool`, defaults to `False`):
682
+ If `True`, the weight casting operations are non-blocking.
683
+
684
+ Example:
685
+
686
+ ```python
687
+ >>> from accelerate.hooks import attach_layerwise_casting_hooks
688
+ >>> from transformers import AutoModelForCausalLM
689
+ >>> import torch
690
+
691
+ >>> # Model
692
+ >>> checkpoint = "EleutherAI/gpt-j-6B"
693
+ >>> model = AutoModelForCausalLM.from_pretrained(checkpoint)
694
+
695
+ >>> # Attach hooks and perform inference
696
+ >>> attach_layerwise_casting_hooks(model, storage_dtype=torch.float8_e4m3fn, compute_dtype=torch.bfloat16)
697
+ >>> with torch.no_grad():
698
+ ... model(...)
699
+ ```
700
+
701
+ Users can also pass modules they want to avoid from getting downcasted.
702
+
703
+ ```py
704
+ >>> attach_layerwise_casting_hooks(
705
+ ... model, storage_dtype=torch.float8_e4m3fn, compute_dtype=torch.bfloat16, skip_modules_pattern=["norm"]
706
+ ... )
707
+ ```
708
+ """
709
+ _attach_layerwise_casting_hooks(
710
+ module, storage_dtype, compute_dtype, skip_modules_pattern, skip_modules_classes, non_blocking
711
+ )
712
+
713
+
714
+ def _attach_layerwise_casting_hooks(
715
+ module: torch.nn.Module,
716
+ storage_dtype: torch.dtype,
717
+ compute_dtype: torch.dtype,
718
+ skip_modules_pattern: Union[str, tuple[str, ...]] = None,
719
+ skip_modules_classes: Optional[tuple[type[torch.nn.Module], ...]] = None,
720
+ non_blocking: bool = False,
721
+ _prefix: str = "",
722
+ ):
723
+ should_skip = (skip_modules_classes is not None and isinstance(module, skip_modules_classes)) or (
724
+ skip_modules_pattern is not None and any(re.search(pattern, _prefix) for pattern in skip_modules_pattern)
725
+ )
726
+ if should_skip:
727
+ logger.debug(f'Skipping layerwise casting for layer "{_prefix}"')
728
+ return
729
+
730
+ if isinstance(module, SUPPORTED_PYTORCH_LAYERS_FOR_UPCASTING):
731
+ logger.debug(f'Applying layerwise casting to layer "{_prefix}"')
732
+ add_hook_to_module(
733
+ module,
734
+ LayerwiseCastingHook(storage_dtype=storage_dtype, compute_dtype=compute_dtype, non_blocking=non_blocking),
735
+ append=True,
736
+ )
737
+ return
738
+
739
+ for name, submodule in module.named_children():
740
+ layer_name = f"{_prefix}.{name}" if _prefix else name
741
+ _attach_layerwise_casting_hooks(
742
+ submodule,
743
+ storage_dtype,
744
+ compute_dtype,
745
+ skip_modules_pattern,
746
+ skip_modules_classes,
747
+ non_blocking,
748
+ _prefix=layer_name,
749
+ )
lib/python3.12/site-packages/accelerate/checkpointing.py ADDED
@@ -0,0 +1,319 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2022 The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import random
16
+ from pathlib import Path
17
+
18
+ import numpy as np
19
+ import torch
20
+ from safetensors.torch import load_model
21
+ from torch.cuda.amp import GradScaler
22
+
23
+ from .utils import (
24
+ MODEL_NAME,
25
+ OPTIMIZER_NAME,
26
+ RNG_STATE_NAME,
27
+ SAFE_MODEL_NAME,
28
+ SAFE_WEIGHTS_NAME,
29
+ SAMPLER_NAME,
30
+ SCALER_NAME,
31
+ SCHEDULER_NAME,
32
+ WEIGHTS_NAME,
33
+ get_pretty_name,
34
+ is_cuda_available,
35
+ is_hpu_available,
36
+ is_mlu_available,
37
+ is_musa_available,
38
+ is_sdaa_available,
39
+ is_torch_xla_available,
40
+ is_xpu_available,
41
+ load,
42
+ save,
43
+ )
44
+
45
+
46
+ if is_torch_xla_available():
47
+ import torch_xla.core.xla_model as xm
48
+
49
+ from .logging import get_logger
50
+ from .state import PartialState
51
+
52
+
53
+ logger = get_logger(__name__)
54
+
55
+
56
+ def save_accelerator_state(
57
+ output_dir: str,
58
+ model_states: list[dict],
59
+ optimizers: list,
60
+ schedulers: list,
61
+ dataloaders: list,
62
+ process_index: int,
63
+ step: int,
64
+ scaler: GradScaler = None,
65
+ save_on_each_node: bool = False,
66
+ safe_serialization: bool = True,
67
+ ):
68
+ """
69
+ Saves the current states of the models, optimizers, scaler, and RNG generators to a given directory.
70
+
71
+ <Tip>
72
+
73
+ If `safe_serialization` is `True`, models will be saved with `safetensors` while the rest are saved using native
74
+ `pickle`.
75
+
76
+ </Tip>
77
+
78
+ Args:
79
+ output_dir (`str` or `os.PathLike`):
80
+ The name of the folder to save all relevant weights and states.
81
+ model_states (`List[torch.nn.Module]`):
82
+ A list of model states
83
+ optimizers (`List[torch.optim.Optimizer]`):
84
+ A list of optimizer instances
85
+ schedulers (`List[torch.optim.lr_scheduler._LRScheduler]`):
86
+ A list of learning rate schedulers
87
+ dataloaders (`List[torch.utils.data.DataLoader]`):
88
+ A list of dataloader instances to save their sampler states
89
+ process_index (`int`):
90
+ The current process index in the Accelerator state
91
+ step (`int`):
92
+ The current step in the internal step tracker
93
+ scaler (`torch.amp.GradScaler`, *optional*):
94
+ An optional gradient scaler instance to save;
95
+ save_on_each_node (`bool`, *optional*):
96
+ Whether to save on every node, or only the main node.
97
+ safe_serialization (`bool`, *optional*, defaults to `True`):
98
+ Whether to save the model using `safetensors` or the traditional PyTorch way (that uses `pickle`).
99
+ """
100
+ output_dir = Path(output_dir)
101
+ # Model states
102
+ for i, state in enumerate(model_states):
103
+ weights_name = WEIGHTS_NAME if not safe_serialization else SAFE_WEIGHTS_NAME
104
+ if i > 0:
105
+ weights_name = weights_name.replace(".", f"_{i}.")
106
+ output_model_file = output_dir.joinpath(weights_name)
107
+ save(state, output_model_file, save_on_each_node=save_on_each_node, safe_serialization=safe_serialization)
108
+ logger.info(f"Model weights saved in {output_model_file}")
109
+ # Optimizer states
110
+ for i, opt in enumerate(optimizers):
111
+ state = opt.state_dict()
112
+ optimizer_name = f"{OPTIMIZER_NAME}.bin" if i == 0 else f"{OPTIMIZER_NAME}_{i}.bin"
113
+ output_optimizer_file = output_dir.joinpath(optimizer_name)
114
+ save(state, output_optimizer_file, save_on_each_node=save_on_each_node, safe_serialization=False)
115
+ logger.info(f"Optimizer state saved in {output_optimizer_file}")
116
+ # Scheduler states
117
+ for i, scheduler in enumerate(schedulers):
118
+ state = scheduler.state_dict()
119
+ scheduler_name = f"{SCHEDULER_NAME}.bin" if i == 0 else f"{SCHEDULER_NAME}_{i}.bin"
120
+ output_scheduler_file = output_dir.joinpath(scheduler_name)
121
+ save(state, output_scheduler_file, save_on_each_node=save_on_each_node, safe_serialization=False)
122
+ logger.info(f"Scheduler state saved in {output_scheduler_file}")
123
+ # DataLoader states
124
+ for i, dataloader in enumerate(dataloaders):
125
+ sampler_name = f"{SAMPLER_NAME}.bin" if i == 0 else f"{SAMPLER_NAME}_{i}.bin"
126
+ output_sampler_file = output_dir.joinpath(sampler_name)
127
+ # Only save if we have our custom sampler
128
+ from .data_loader import IterableDatasetShard, SeedableRandomSampler
129
+
130
+ if isinstance(dataloader.dataset, IterableDatasetShard):
131
+ sampler = dataloader.get_sampler()
132
+ if isinstance(sampler, SeedableRandomSampler):
133
+ save(sampler, output_sampler_file, save_on_each_node=save_on_each_node, safe_serialization=False)
134
+ if getattr(dataloader, "use_stateful_dataloader", False):
135
+ dataloader_state_dict_name = "dl_state_dict.bin" if i == 0 else f"dl_state_dict_{i}.bin"
136
+ output_dataloader_state_dict_file = output_dir.joinpath(dataloader_state_dict_name)
137
+ state_dict = dataloader.state_dict()
138
+ torch.save(state_dict, output_dataloader_state_dict_file)
139
+ logger.info(f"Sampler state for dataloader {i} saved in {output_sampler_file}")
140
+
141
+ # GradScaler state
142
+ if scaler is not None:
143
+ state = scaler.state_dict()
144
+ output_scaler_file = output_dir.joinpath(SCALER_NAME)
145
+ torch.save(state, output_scaler_file)
146
+ logger.info(f"Gradient scaler state saved in {output_scaler_file}")
147
+ # Random number generator states
148
+ states = {}
149
+ states_name = f"{RNG_STATE_NAME}_{process_index}.pkl"
150
+ states["step"] = step
151
+ states["random_state"] = random.getstate()
152
+ states["numpy_random_seed"] = np.random.get_state()
153
+ states["torch_manual_seed"] = torch.get_rng_state()
154
+ if is_xpu_available():
155
+ states["torch_xpu_manual_seed"] = torch.xpu.get_rng_state_all()
156
+ if is_mlu_available():
157
+ states["torch_mlu_manual_seed"] = torch.mlu.get_rng_state_all()
158
+ elif is_sdaa_available():
159
+ states["torch_sdaa_manual_seed"] = torch.sdaa.get_rng_state_all()
160
+ elif is_musa_available():
161
+ states["torch_musa_manual_seed"] = torch.musa.get_rng_state_all()
162
+ if is_hpu_available():
163
+ states["torch_hpu_manual_seed"] = torch.hpu.get_rng_state_all()
164
+ if is_cuda_available():
165
+ states["torch_cuda_manual_seed"] = torch.cuda.get_rng_state_all()
166
+ if is_torch_xla_available():
167
+ states["xm_seed"] = xm.get_rng_state()
168
+ output_states_file = output_dir.joinpath(states_name)
169
+ torch.save(states, output_states_file)
170
+ logger.info(f"Random states saved in {output_states_file}")
171
+ return output_dir
172
+
173
+
174
+ def load_accelerator_state(
175
+ input_dir,
176
+ models,
177
+ optimizers,
178
+ schedulers,
179
+ dataloaders,
180
+ process_index,
181
+ scaler=None,
182
+ map_location=None,
183
+ **load_model_func_kwargs,
184
+ ):
185
+ """
186
+ Loads states of the models, optimizers, scaler, and RNG generators from a given directory.
187
+
188
+ Args:
189
+ input_dir (`str` or `os.PathLike`):
190
+ The name of the folder to load all relevant weights and states.
191
+ models (`List[torch.nn.Module]`):
192
+ A list of model instances
193
+ optimizers (`List[torch.optim.Optimizer]`):
194
+ A list of optimizer instances
195
+ schedulers (`List[torch.optim.lr_scheduler._LRScheduler]`):
196
+ A list of learning rate schedulers
197
+ process_index (`int`):
198
+ The current process index in the Accelerator state
199
+ scaler (`torch.amp.GradScaler`, *optional*):
200
+ An optional *GradScaler* instance to load
201
+ map_location (`str`, *optional*):
202
+ What device to load the optimizer state onto. Should be one of either "cpu" or "on_device".
203
+ load_model_func_kwargs (`dict`, *optional*):
204
+ Additional arguments that can be passed to the model's `load_state_dict` method.
205
+
206
+ Returns:
207
+ `dict`: Contains the `Accelerator` attributes to override while loading the state.
208
+ """
209
+ # stores the `Accelerator` attributes to override
210
+ override_attributes = dict()
211
+ if map_location not in [None, "cpu", "on_device"]:
212
+ raise TypeError(
213
+ "Unsupported optimizer map location passed, please choose one of `None`, `'cpu'`, or `'on_device'`"
214
+ )
215
+ if map_location is None:
216
+ map_location = "cpu"
217
+ elif map_location == "on_device":
218
+ map_location = PartialState().device
219
+
220
+ input_dir = Path(input_dir)
221
+ # Model states
222
+ for i, model in enumerate(models):
223
+ ending = f"_{i}" if i > 0 else ""
224
+ input_model_file = input_dir.joinpath(f"{SAFE_MODEL_NAME}{ending}.safetensors")
225
+ if input_model_file.exists():
226
+ load_model(model, input_model_file, device=str(map_location), **load_model_func_kwargs)
227
+ else:
228
+ # Load with torch
229
+ input_model_file = input_dir.joinpath(f"{MODEL_NAME}{ending}.bin")
230
+ state_dict = load(input_model_file, map_location=map_location)
231
+ model.load_state_dict(state_dict, **load_model_func_kwargs)
232
+ logger.info("All model weights loaded successfully")
233
+
234
+ # Optimizer states
235
+ for i, opt in enumerate(optimizers):
236
+ optimizer_name = f"{OPTIMIZER_NAME}.bin" if i == 0 else f"{OPTIMIZER_NAME}_{i}.bin"
237
+ input_optimizer_file = input_dir.joinpath(optimizer_name)
238
+ optimizer_state = load(input_optimizer_file, map_location=map_location)
239
+ optimizers[i].load_state_dict(optimizer_state)
240
+ logger.info("All optimizer states loaded successfully")
241
+
242
+ # Scheduler states
243
+ for i, scheduler in enumerate(schedulers):
244
+ scheduler_name = f"{SCHEDULER_NAME}.bin" if i == 0 else f"{SCHEDULER_NAME}_{i}.bin"
245
+ input_scheduler_file = input_dir.joinpath(scheduler_name)
246
+ scheduler_state = load(input_scheduler_file)
247
+ scheduler.load_state_dict(scheduler_state)
248
+ logger.info("All scheduler states loaded successfully")
249
+
250
+ for i, dataloader in enumerate(dataloaders):
251
+ sampler_name = f"{SAMPLER_NAME}.bin" if i == 0 else f"{SAMPLER_NAME}_{i}.bin"
252
+ input_sampler_file = input_dir.joinpath(sampler_name)
253
+ # Only load if we have our custom sampler
254
+ from .data_loader import IterableDatasetShard, SeedableRandomSampler
255
+
256
+ if isinstance(dataloader.dataset, IterableDatasetShard):
257
+ sampler = dataloader.get_sampler()
258
+ if isinstance(sampler, SeedableRandomSampler):
259
+ sampler = dataloader.set_sampler(load(input_sampler_file))
260
+ if getattr(dataloader, "use_stateful_dataloader", False):
261
+ dataloader_state_dict_name = "dl_state_dict.bin" if i == 0 else f"dl_state_dict_{i}.bin"
262
+ input_dataloader_state_dict_file = input_dir.joinpath(dataloader_state_dict_name)
263
+ if input_dataloader_state_dict_file.exists():
264
+ state_dict = load(input_dataloader_state_dict_file)
265
+ dataloader.load_state_dict(state_dict)
266
+ logger.info("All dataloader sampler states loaded successfully")
267
+
268
+ # GradScaler state
269
+ if scaler is not None:
270
+ input_scaler_file = input_dir.joinpath(SCALER_NAME)
271
+ scaler_state = load(input_scaler_file)
272
+ scaler.load_state_dict(scaler_state)
273
+ logger.info("GradScaler state loaded successfully")
274
+
275
+ # Random states
276
+ try:
277
+ states = load(input_dir.joinpath(f"{RNG_STATE_NAME}_{process_index}.pkl"))
278
+ if "step" in states:
279
+ override_attributes["step"] = states["step"]
280
+ random.setstate(states["random_state"])
281
+ np.random.set_state(states["numpy_random_seed"])
282
+ torch.set_rng_state(states["torch_manual_seed"])
283
+ if is_xpu_available():
284
+ torch.xpu.set_rng_state_all(states["torch_xpu_manual_seed"])
285
+ if is_mlu_available():
286
+ torch.mlu.set_rng_state_all(states["torch_mlu_manual_seed"])
287
+ elif is_sdaa_available():
288
+ torch.sdaa.set_rng_state_all(states["torch_sdaa_manual_seed"])
289
+ elif is_musa_available():
290
+ torch.musa.set_rng_state_all(states["torch_musa_manual_seed"])
291
+ else:
292
+ torch.cuda.set_rng_state_all(states["torch_cuda_manual_seed"])
293
+ if is_torch_xla_available():
294
+ xm.set_rng_state(states["xm_seed"])
295
+ logger.info("All random states loaded successfully")
296
+ except Exception:
297
+ logger.info("Could not load random states")
298
+
299
+ return override_attributes
300
+
301
+
302
+ def save_custom_state(obj, path, index: int = 0, save_on_each_node: bool = False):
303
+ """
304
+ Saves the state of `obj` to `{path}/custom_checkpoint_{index}.pkl`
305
+ """
306
+ # Should this be the right way to get a qual_name type value from `obj`?
307
+ save_location = Path(path) / f"custom_checkpoint_{index}.pkl"
308
+ logger.info(f"Saving the state of {get_pretty_name(obj)} to {save_location}")
309
+ save(obj.state_dict(), save_location, save_on_each_node=save_on_each_node)
310
+
311
+
312
+ def load_custom_state(obj, path, index: int = 0):
313
+ """
314
+ Loads the state of `obj` at `{path}/custom_checkpoint_{index}.pkl`. Will always set `weights_only=False` when
315
+ loading the state.
316
+ """
317
+ load_location = f"{path}/custom_checkpoint_{index}.pkl"
318
+ logger.info(f"Loading the state of {get_pretty_name(obj)} from {load_location}")
319
+ obj.load_state_dict(load(load_location, map_location="cpu", weights_only=False))
lib/python3.12/site-packages/accelerate/commands/__init__.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
lib/python3.12/site-packages/accelerate/commands/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (198 Bytes). View file
 
lib/python3.12/site-packages/accelerate/commands/__pycache__/accelerate_cli.cpython-312.pyc ADDED
Binary file (1.83 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/__pycache__/env.cpython-312.pyc ADDED
Binary file (5.15 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/__pycache__/estimate.cpython-312.pyc ADDED
Binary file (14 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/__pycache__/launch.cpython-312.pyc ADDED
Binary file (48.8 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/__pycache__/merge.cpython-312.pyc ADDED
Binary file (2.42 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/__pycache__/test.cpython-312.pyc ADDED
Binary file (2.18 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/__pycache__/to_fsdp2.cpython-312.pyc ADDED
Binary file (6.2 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/__pycache__/tpu.cpython-312.pyc ADDED
Binary file (6.01 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/__pycache__/utils.cpython-312.pyc ADDED
Binary file (5.2 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/accelerate_cli.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright 2021 The HuggingFace Team. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ from accelerate.commands.config import get_config_parser
18
+ from accelerate.commands.env import env_command_parser
19
+ from accelerate.commands.estimate import estimate_command_parser
20
+ from accelerate.commands.launch import launch_command_parser
21
+ from accelerate.commands.merge import merge_command_parser
22
+ from accelerate.commands.test import test_command_parser
23
+ from accelerate.commands.to_fsdp2 import to_fsdp2_command_parser
24
+ from accelerate.commands.tpu import tpu_command_parser
25
+ from accelerate.commands.utils import CustomArgumentParser
26
+
27
+
28
+ def main():
29
+ parser = CustomArgumentParser("Accelerate CLI tool", usage="accelerate <command> [<args>]", allow_abbrev=False)
30
+ subparsers = parser.add_subparsers(help="accelerate command helpers")
31
+
32
+ # Register commands
33
+ get_config_parser(subparsers=subparsers)
34
+ estimate_command_parser(subparsers=subparsers)
35
+ env_command_parser(subparsers=subparsers)
36
+ launch_command_parser(subparsers=subparsers)
37
+ merge_command_parser(subparsers=subparsers)
38
+ tpu_command_parser(subparsers=subparsers)
39
+ test_command_parser(subparsers=subparsers)
40
+ to_fsdp2_command_parser(subparsers=subparsers)
41
+
42
+ # Let's go
43
+ args = parser.parse_args()
44
+
45
+ if not hasattr(args, "func"):
46
+ parser.print_help()
47
+ exit(1)
48
+
49
+ # Run
50
+ args.func(args)
51
+
52
+
53
+ if __name__ == "__main__":
54
+ main()
lib/python3.12/site-packages/accelerate/commands/config/__init__.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright 2021 The HuggingFace Team. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ import argparse
18
+
19
+ from .config import config_command_parser
20
+ from .config_args import default_config_file, load_config_from_file # noqa: F401
21
+ from .default import default_command_parser
22
+ from .update import update_command_parser
23
+
24
+
25
+ def get_config_parser(subparsers=None):
26
+ parent_parser = argparse.ArgumentParser(add_help=False, allow_abbrev=False)
27
+ # The main config parser
28
+ config_parser = config_command_parser(subparsers)
29
+ # The subparser to add commands to
30
+ subcommands = config_parser.add_subparsers(title="subcommands", dest="subcommand")
31
+
32
+ # Then add other parsers with the parent parser
33
+ default_command_parser(subcommands, parents=[parent_parser])
34
+ update_command_parser(subcommands, parents=[parent_parser])
35
+
36
+ return config_parser
37
+
38
+
39
+ def main():
40
+ config_parser = get_config_parser()
41
+ args = config_parser.parse_args()
42
+
43
+ if not hasattr(args, "func"):
44
+ config_parser.print_help()
45
+ exit(1)
46
+
47
+ # Run
48
+ args.func(args)
49
+
50
+
51
+ if __name__ == "__main__":
52
+ main()
lib/python3.12/site-packages/accelerate/commands/config/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (1.48 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/config/__pycache__/cluster.cpython-312.pyc ADDED
Binary file (26.9 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/config/__pycache__/config.cpython-312.pyc ADDED
Binary file (3.26 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/config/__pycache__/config_args.cpython-312.pyc ADDED
Binary file (11.8 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/config/__pycache__/config_utils.cpython-312.pyc ADDED
Binary file (3.95 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/config/__pycache__/default.cpython-312.pyc ADDED
Binary file (5.96 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/config/__pycache__/sagemaker.cpython-312.pyc ADDED
Binary file (9.48 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/config/__pycache__/update.cpython-312.pyc ADDED
Binary file (2.44 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/config/cluster.py ADDED
@@ -0,0 +1,869 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright 2021 The HuggingFace Team. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ import os
18
+
19
+ from ...utils import (
20
+ ComputeEnvironment,
21
+ DistributedType,
22
+ is_deepspeed_available,
23
+ is_fp8_available,
24
+ is_hpu_available,
25
+ is_mlu_available,
26
+ is_mps_available,
27
+ is_msamp_available,
28
+ is_musa_available,
29
+ is_npu_available,
30
+ is_sdaa_available,
31
+ is_transformer_engine_available,
32
+ is_transformers_available,
33
+ is_xpu_available,
34
+ )
35
+ from ...utils.constants import (
36
+ DEEPSPEED_MULTINODE_LAUNCHERS,
37
+ FSDP2_STATE_DICT_TYPE,
38
+ FSDP_AUTO_WRAP_POLICY,
39
+ FSDP_BACKWARD_PREFETCH,
40
+ FSDP_SHARDING_STRATEGY,
41
+ FSDP_STATE_DICT_TYPE,
42
+ TORCH_DYNAMO_MODES,
43
+ )
44
+ from .config_args import ClusterConfig
45
+ from .config_utils import (
46
+ DYNAMO_BACKENDS,
47
+ _ask_field,
48
+ _ask_options,
49
+ _convert_distributed_mode,
50
+ _convert_dynamo_backend,
51
+ _convert_fp8_backend,
52
+ _convert_mixed_precision,
53
+ _convert_yes_no_to_bool,
54
+ )
55
+
56
+
57
+ def get_cluster_input():
58
+ distributed_type = _ask_options(
59
+ "Which type of machine are you using?",
60
+ [
61
+ "No distributed training",
62
+ "multi-CPU",
63
+ "multi-XPU",
64
+ "multi-HPU",
65
+ "multi-GPU",
66
+ "multi-NPU",
67
+ "multi-MLU",
68
+ "multi-SDAA",
69
+ "multi-MUSA",
70
+ "TPU",
71
+ ],
72
+ _convert_distributed_mode,
73
+ )
74
+
75
+ machine_rank = 0
76
+ num_machines = 1
77
+ num_processes = 1
78
+ gpu_ids = None
79
+ main_process_ip = None
80
+ main_process_port = None
81
+ rdzv_backend = "static"
82
+ same_network = True
83
+ debug = False
84
+
85
+ if distributed_type in [
86
+ DistributedType.MULTI_GPU,
87
+ DistributedType.MULTI_MLU,
88
+ DistributedType.MULTI_SDAA,
89
+ DistributedType.MULTI_MUSA,
90
+ DistributedType.MULTI_NPU,
91
+ DistributedType.MULTI_XPU,
92
+ DistributedType.MULTI_CPU,
93
+ DistributedType.MULTI_HPU,
94
+ ]:
95
+ num_machines = _ask_field(
96
+ "How many different machines will you use (use more than 1 for multi-node training)? [1]: ",
97
+ int,
98
+ default=1,
99
+ )
100
+ if num_machines > 1:
101
+ machine_rank = _ask_options(
102
+ "What is the rank of this machine?",
103
+ list(range(num_machines)),
104
+ int,
105
+ )
106
+ main_process_ip = _ask_field(
107
+ "What is the IP address of the machine that will host the main process? ",
108
+ )
109
+ main_process_port = _ask_field(
110
+ "What is the port you will use to communicate with the main process? ",
111
+ int,
112
+ )
113
+ same_network = _ask_field(
114
+ "Are all the machines on the same local network? Answer `no` if nodes are on the cloud and/or on different network hosts [YES/no]: ",
115
+ _convert_yes_no_to_bool,
116
+ default=True,
117
+ error_message="Please enter yes or no.",
118
+ )
119
+ if not same_network:
120
+ rdzv_backend = _ask_field(
121
+ "What rendezvous backend will you use? ('static', 'c10d', ...): ", default="static"
122
+ )
123
+ debug = _ask_field(
124
+ "Should distributed operations be checked while running for errors? This can avoid timeout issues but will be slower. [yes/NO]: ",
125
+ _convert_yes_no_to_bool,
126
+ default=False,
127
+ error_message="Please enter yes or no.",
128
+ )
129
+
130
+ if distributed_type == DistributedType.NO:
131
+ use_cpu = _ask_field(
132
+ "Do you want to run your training on CPU only (even if a GPU / Apple Silicon / Ascend NPU device is available)? [yes/NO]:",
133
+ _convert_yes_no_to_bool,
134
+ default=False,
135
+ error_message="Please enter yes or no.",
136
+ )
137
+ elif distributed_type == DistributedType.MULTI_CPU:
138
+ use_cpu = True
139
+ else:
140
+ use_cpu = False
141
+
142
+ ipex_config = {}
143
+ mpirun_config = {}
144
+ if use_cpu or is_xpu_available():
145
+ ipex_config["ipex"] = _ask_field(
146
+ "Do you want to use Intel PyTorch Extension (IPEX) to speed up training on CPU/XPU? [yes/NO]:",
147
+ _convert_yes_no_to_bool,
148
+ default=False,
149
+ error_message="Please enter yes or no.",
150
+ )
151
+
152
+ if use_cpu:
153
+ if distributed_type == DistributedType.MULTI_CPU:
154
+ use_mpirun = _ask_field(
155
+ "Do you want accelerate to launch mpirun? [yes/NO]: ",
156
+ _convert_yes_no_to_bool,
157
+ default=False,
158
+ error_message="Please enter yes or no.",
159
+ )
160
+ if use_mpirun:
161
+ mpirun_hostfile = _ask_field(
162
+ "Please enter the path to the hostfile to use with mpirun [~/hostfile]: ",
163
+ str,
164
+ default="~/hostfile",
165
+ )
166
+ mpirun_config["mpirun_hostfile"] = os.path.expanduser(mpirun_hostfile.strip())
167
+ mpirun_config["mpirun_ccl"] = _ask_field("Enter the number of oneCCL worker threads [1]: ", default=1)
168
+
169
+ dynamo_config = {}
170
+ use_dynamo = _ask_field(
171
+ "Do you wish to optimize your script with torch dynamo?[yes/NO]:",
172
+ _convert_yes_no_to_bool,
173
+ default=False,
174
+ error_message="Please enter yes or no.",
175
+ )
176
+ if use_dynamo:
177
+ prefix = "dynamo_"
178
+ dynamo_config[prefix + "backend"] = _ask_options(
179
+ "Which dynamo backend would you like to use?",
180
+ [x.lower() for x in DYNAMO_BACKENDS],
181
+ _convert_dynamo_backend,
182
+ default=2,
183
+ )
184
+ use_custom_options = _ask_field(
185
+ "Do you want to customize the defaults sent to torch.compile? [yes/NO]: ",
186
+ _convert_yes_no_to_bool,
187
+ default=False,
188
+ error_message="Please enter yes or no.",
189
+ )
190
+
191
+ if use_custom_options:
192
+ dynamo_config[prefix + "mode"] = _ask_options(
193
+ "Which mode do you want to use?",
194
+ TORCH_DYNAMO_MODES,
195
+ lambda x: TORCH_DYNAMO_MODES[int(x)],
196
+ default=0,
197
+ )
198
+ dynamo_config[prefix + "use_fullgraph"] = _ask_field(
199
+ "Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: ",
200
+ _convert_yes_no_to_bool,
201
+ default=False,
202
+ error_message="Please enter yes or no.",
203
+ )
204
+ dynamo_config[prefix + "use_dynamic"] = _ask_field(
205
+ "Do you want to enable dynamic shape tracing? [yes/NO]: ",
206
+ _convert_yes_no_to_bool,
207
+ default=False,
208
+ error_message="Please enter yes or no.",
209
+ )
210
+ dynamo_config[prefix + "use_regional_compilation"] = _ask_field(
211
+ "Do you want to enable regional compilation? [yes/NO]: ",
212
+ _convert_yes_no_to_bool,
213
+ default=False,
214
+ error_message="Please enter yes or no.",
215
+ )
216
+
217
+ use_mps = not use_cpu and is_mps_available()
218
+ deepspeed_config = {}
219
+ if (
220
+ distributed_type
221
+ in [
222
+ DistributedType.MULTI_GPU,
223
+ DistributedType.MULTI_XPU,
224
+ DistributedType.MULTI_HPU,
225
+ DistributedType.MULTI_NPU,
226
+ DistributedType.MULTI_MLU,
227
+ DistributedType.MULTI_SDAA,
228
+ DistributedType.MULTI_MUSA,
229
+ DistributedType.NO,
230
+ ]
231
+ and not use_mps
232
+ ):
233
+ use_deepspeed = _ask_field(
234
+ "Do you want to use DeepSpeed? [yes/NO]: ",
235
+ _convert_yes_no_to_bool,
236
+ default=False,
237
+ error_message="Please enter yes or no.",
238
+ )
239
+ if use_deepspeed:
240
+ distributed_type = DistributedType.DEEPSPEED
241
+ assert is_deepspeed_available(), (
242
+ "DeepSpeed is not installed => run `pip3 install deepspeed` or build it from source"
243
+ )
244
+
245
+ if distributed_type == DistributedType.DEEPSPEED:
246
+ use_deepspeed_config = _ask_field(
247
+ "Do you want to specify a json file to a DeepSpeed config? [yes/NO]: ",
248
+ _convert_yes_no_to_bool,
249
+ default=False,
250
+ error_message="Please enter yes or no.",
251
+ )
252
+ if use_deepspeed_config:
253
+ deepspeed_config["deepspeed_config_file"] = _ask_field(
254
+ "Please enter the path to the json DeepSpeed config file: ",
255
+ str,
256
+ default="none",
257
+ )
258
+ else:
259
+ deepspeed_config["zero_stage"] = _ask_options(
260
+ "What should be your DeepSpeed's ZeRO optimization stage?",
261
+ [0, 1, 2, 3],
262
+ int,
263
+ default=2,
264
+ )
265
+
266
+ deepspeed_devices = ["none", "cpu", "nvme"]
267
+ if deepspeed_config["zero_stage"] >= 2:
268
+ deepspeed_config["offload_optimizer_device"] = _ask_options(
269
+ "Where to offload optimizer states?", deepspeed_devices, lambda x: deepspeed_devices[int(x)]
270
+ )
271
+ deepspeed_config["offload_param_device"] = _ask_options(
272
+ "Where to offload parameters?", deepspeed_devices, lambda x: deepspeed_devices[int(x)]
273
+ )
274
+ if deepspeed_config["offload_param_device"] == "nvme":
275
+ deepspeed_config["offload_param_nvme_path"] = _ask_field(
276
+ "Nvme Path to offload parameters?",
277
+ str,
278
+ default="/nvme",
279
+ )
280
+ if deepspeed_config["offload_optimizer_device"] == "nvme":
281
+ deepspeed_config["offload_optimizer_nvme_path"] = _ask_field(
282
+ "Nvme Path to offload optimizer states?",
283
+ str,
284
+ default="/nvme",
285
+ )
286
+ deepspeed_config["gradient_accumulation_steps"] = _ask_field(
287
+ "How many gradient accumulation steps you're passing in your script? [1]: ",
288
+ int,
289
+ default=1,
290
+ )
291
+ use_gradient_clipping = _ask_field(
292
+ "Do you want to use gradient clipping? [yes/NO]: ",
293
+ _convert_yes_no_to_bool,
294
+ default=False,
295
+ error_message="Please enter yes or no.",
296
+ )
297
+ if use_gradient_clipping:
298
+ deepspeed_config["gradient_clipping"] = _ask_field(
299
+ "What is the gradient clipping value? [1.0]: ",
300
+ float,
301
+ default=1.0,
302
+ )
303
+ if deepspeed_config["zero_stage"] == 3:
304
+ deepspeed_config["zero3_save_16bit_model"] = _ask_field(
305
+ "Do you want to save 16-bit model weights when using ZeRO Stage-3? [yes/NO]: ",
306
+ _convert_yes_no_to_bool,
307
+ default=False,
308
+ error_message="Please enter yes or no.",
309
+ )
310
+ deepspeed_config["zero3_init_flag"] = _ask_field(
311
+ "Do you want to enable `deepspeed.zero.Init` when using ZeRO Stage-3 for constructing massive models? [yes/NO]: ",
312
+ _convert_yes_no_to_bool,
313
+ default=False,
314
+ error_message="Please enter yes or no.",
315
+ )
316
+ if deepspeed_config["zero3_init_flag"]:
317
+ if not is_transformers_available():
318
+ raise Exception(
319
+ "When `zero3_init_flag` is set, it requires Transformers to be installed. "
320
+ "Please run `pip3 install transformers`."
321
+ )
322
+ use_moe = _ask_field(
323
+ "Do you want to enable Mixture-of-Experts training (MoE)? [yes/NO]: ",
324
+ _convert_yes_no_to_bool,
325
+ default=False,
326
+ error_message="Please enter yes or no.",
327
+ )
328
+ if use_moe:
329
+ deepspeed_config["deepspeed_moe_layer_cls_names"] = _ask_field(
330
+ "Specify the comma-separated list of transformers MoE layer class names (case-sensitive), e.g : "
331
+ " `MixtralSparseMoeBlock`, `Qwen2MoeSparseMoeBlock`, `JetMoEAttention,JetMoEBlock` ... : ",
332
+ str,
333
+ )
334
+
335
+ if num_machines > 1:
336
+ launcher_query = "Which Type of launcher do you want to use?"
337
+ deepspeed_config["deepspeed_multinode_launcher"] = _ask_options(
338
+ launcher_query,
339
+ DEEPSPEED_MULTINODE_LAUNCHERS,
340
+ lambda x: DEEPSPEED_MULTINODE_LAUNCHERS[int(x)],
341
+ )
342
+
343
+ if deepspeed_config["deepspeed_multinode_launcher"] != DEEPSPEED_MULTINODE_LAUNCHERS[1]:
344
+ deepspeed_config["deepspeed_hostfile"] = _ask_field(
345
+ "DeepSpeed configures multi-node compute resources with hostfile. "
346
+ "Each row is of the format `hostname slots=[num_gpus]`, e.g., `localhost slots=2`; "
347
+ "for more information please refer official [documentation]"
348
+ "(https://www.deepspeed.ai/getting-started/#resource-configuration-multi-node). "
349
+ "Please specify the location of hostfile: ",
350
+ str,
351
+ )
352
+
353
+ is_exclusion_filter = _ask_field(
354
+ "Do you want to specify exclusion filter string? [yes/NO]: ",
355
+ _convert_yes_no_to_bool,
356
+ default=False,
357
+ error_message="Please enter yes or no.",
358
+ )
359
+ if is_exclusion_filter:
360
+ deepspeed_config["deepspeed_exclusion_filter"] = _ask_field(
361
+ "DeepSpeed exclusion filter string: ",
362
+ str,
363
+ )
364
+
365
+ is_inclusion_filter = _ask_field(
366
+ "Do you want to specify inclusion filter string? [yes/NO]: ",
367
+ _convert_yes_no_to_bool,
368
+ default=False,
369
+ error_message="Please enter yes or no.",
370
+ )
371
+ if is_inclusion_filter:
372
+ deepspeed_config["deepspeed_inclusion_filter"] = _ask_field(
373
+ "DeepSpeed inclusion filter string: ",
374
+ str,
375
+ )
376
+
377
+ fsdp_config = {}
378
+
379
+ if distributed_type in [
380
+ DistributedType.MULTI_GPU,
381
+ DistributedType.MULTI_NPU,
382
+ DistributedType.MULTI_MLU,
383
+ DistributedType.MULTI_SDAA,
384
+ DistributedType.MULTI_MUSA,
385
+ DistributedType.MULTI_XPU,
386
+ DistributedType.MULTI_HPU,
387
+ ]:
388
+ use_fsdp = _ask_field(
389
+ "Do you want to use FullyShardedDataParallel? [yes/NO]: ",
390
+ _convert_yes_no_to_bool,
391
+ default=False,
392
+ error_message="Please enter yes or no.",
393
+ )
394
+ if use_fsdp:
395
+ distributed_type = DistributedType.FSDP
396
+ if distributed_type == DistributedType.FSDP:
397
+ fsdp_config["fsdp_version"] = _ask_options(
398
+ "What should be your FSDP version? [2]: ",
399
+ [1, 2],
400
+ lambda x: int(x) + 1,
401
+ default=1,
402
+ )
403
+ fsdp_version = fsdp_config["fsdp_version"] # extract to a variable to simplify usage later
404
+
405
+ if fsdp_version == 1:
406
+ sharding_strategy_query = "What should be your sharding strategy?"
407
+ fsdp_config["fsdp_reshard_after_forward"] = _ask_options(
408
+ sharding_strategy_query,
409
+ FSDP_SHARDING_STRATEGY,
410
+ lambda x: FSDP_SHARDING_STRATEGY[int(x)],
411
+ )
412
+ else:
413
+ fsdp_config["fsdp_reshard_after_forward"] = _ask_field(
414
+ "Do you want to enable resharding after forward? [YES/no]: ",
415
+ _convert_yes_no_to_bool,
416
+ default=True,
417
+ error_message="Please enter yes or no.",
418
+ )
419
+
420
+ fsdp_config["fsdp_offload_params"] = _ask_field(
421
+ "Do you want to offload parameters and gradients to CPU? [yes/NO]: ",
422
+ _convert_yes_no_to_bool,
423
+ default=False,
424
+ error_message="Please enter yes or no.",
425
+ )
426
+
427
+ fsdp_wrap_query = "What should be your auto wrap policy?"
428
+ fsdp_config["fsdp_auto_wrap_policy"] = _ask_options(
429
+ fsdp_wrap_query,
430
+ FSDP_AUTO_WRAP_POLICY,
431
+ lambda x: FSDP_AUTO_WRAP_POLICY[int(x)],
432
+ )
433
+ if fsdp_config["fsdp_auto_wrap_policy"] == FSDP_AUTO_WRAP_POLICY[0]:
434
+ use_no_split_modules = _ask_field(
435
+ "Do you want to use the model's `_no_split_modules` to wrap. Only applicable for 🤗 Transformers [yes/NO]: ",
436
+ _convert_yes_no_to_bool,
437
+ default=False,
438
+ error_message="Please enter yes or no.",
439
+ )
440
+ if not use_no_split_modules:
441
+ fsdp_config["fsdp_transformer_layer_cls_to_wrap"] = _ask_field(
442
+ "Specify the comma-separated list of transformer layer class names (case-sensitive) to wrap ,e.g, :"
443
+ "`BertLayer`, `GPTJBlock`, `T5Block`, `BertLayer,BertEmbeddings,BertSelfOutput` ...? : ",
444
+ str,
445
+ )
446
+ elif fsdp_config["fsdp_auto_wrap_policy"] == FSDP_AUTO_WRAP_POLICY[1]:
447
+ fsdp_config["fsdp_min_num_params"] = _ask_field(
448
+ "What should be your FSDP's minimum number of parameters for Default Auto Wrapping Policy? [1e8]: ",
449
+ int,
450
+ default=100000000,
451
+ )
452
+ # Removed in FSDP2, ask for user input for FSDP1
453
+ if fsdp_version == 1:
454
+ fsdp_backward_prefetch_query = "What should be your FSDP's backward prefetch policy?"
455
+ fsdp_config["fsdp_backward_prefetch"] = _ask_options(
456
+ fsdp_backward_prefetch_query,
457
+ FSDP_BACKWARD_PREFETCH,
458
+ lambda x: FSDP_BACKWARD_PREFETCH[int(x)],
459
+ )
460
+
461
+ fsdp_state_dict_type_query = "What should be your FSDP's state dict type?"
462
+ fsdp_config["fsdp_state_dict_type"] = _ask_options(
463
+ fsdp_state_dict_type_query,
464
+ FSDP_STATE_DICT_TYPE if fsdp_version == 1 else FSDP2_STATE_DICT_TYPE,
465
+ lambda x: FSDP_STATE_DICT_TYPE[int(x)] if fsdp_version == 1 else FSDP2_STATE_DICT_TYPE[int(x)],
466
+ default=0,
467
+ )
468
+ # Not implemented in FSDP2, ask for user input for FSDP1
469
+ if fsdp_version == 1:
470
+ fsdp_config["fsdp_forward_prefetch"] = _ask_field(
471
+ "Do you want to enable FSDP's forward prefetch policy? [yes/NO]: ",
472
+ _convert_yes_no_to_bool,
473
+ default=False,
474
+ error_message="Please enter yes or no.",
475
+ )
476
+ # Obsolete in FSDP2, ask for user input for FSDP1
477
+ if fsdp_version == 1:
478
+ fsdp_config["fsdp_use_orig_params"] = _ask_field(
479
+ "Do you want to enable FSDP's `use_orig_params` feature? [YES/no]: ",
480
+ _convert_yes_no_to_bool,
481
+ default=True,
482
+ error_message="Please enter yes or no.",
483
+ )
484
+ fsdp_config["fsdp_cpu_ram_efficient_loading"] = _ask_field(
485
+ "Do you want to enable CPU RAM efficient model loading? Only applicable for 🤗 Transformers models. [YES/no]: ",
486
+ _convert_yes_no_to_bool,
487
+ default=True,
488
+ error_message="Please enter yes or no.",
489
+ )
490
+ # Obsolete in FSDP2, ask for user input for FSDP1
491
+ if fsdp_version == 1:
492
+ if fsdp_config["fsdp_cpu_ram_efficient_loading"]:
493
+ fsdp_config["fsdp_sync_module_states"] = True
494
+ else:
495
+ fsdp_config["fsdp_sync_module_states"] = _ask_field(
496
+ "Do you want each individually wrapped FSDP unit to broadcast module parameters from rank 0 at the start? [YES/no]: ",
497
+ _convert_yes_no_to_bool,
498
+ default=True,
499
+ error_message="Please enter yes or no.",
500
+ )
501
+ fsdp_config["fsdp_activation_checkpointing"] = _ask_field(
502
+ "Do you want to enable FSDP activation checkpointing? [yes/NO]: ",
503
+ _convert_yes_no_to_bool,
504
+ default=False,
505
+ error_message="Please enter yes or no.",
506
+ )
507
+
508
+ megatron_lm_config = {}
509
+ if distributed_type in [DistributedType.MULTI_GPU]:
510
+ use_megatron_lm = _ask_field(
511
+ "Do you want to use Megatron-LM ? [yes/NO]: ",
512
+ _convert_yes_no_to_bool,
513
+ default=False,
514
+ error_message="Please enter yes or no.",
515
+ )
516
+ if use_megatron_lm:
517
+ distributed_type = DistributedType.MEGATRON_LM
518
+ if distributed_type == DistributedType.MEGATRON_LM:
519
+ prefix = "megatron_lm_"
520
+ megatron_lm_config[prefix + "tp_degree"] = _ask_field(
521
+ "What is the Tensor Parallelism degree/size? [1]:",
522
+ int,
523
+ default=1,
524
+ error_message="Please enter an integer.",
525
+ )
526
+ if megatron_lm_config[prefix + "tp_degree"] > 1:
527
+ megatron_lm_config[prefix + "sequence_parallelism"] = _ask_field(
528
+ "Do you want to enable Sequence Parallelism? [YES/no]: ",
529
+ _convert_yes_no_to_bool,
530
+ default=True,
531
+ error_message="Please enter yes or no.",
532
+ )
533
+
534
+ megatron_lm_config[prefix + "pp_degree"] = _ask_field(
535
+ "What is the Pipeline Parallelism degree/size? [1]:",
536
+ int,
537
+ default=1,
538
+ error_message="Please enter an integer.",
539
+ )
540
+ if megatron_lm_config[prefix + "pp_degree"] > 1:
541
+ megatron_lm_config[prefix + "num_micro_batches"] = _ask_field(
542
+ "What is the number of micro-batches? [1]:",
543
+ int,
544
+ default=1,
545
+ error_message="Please enter an integer.",
546
+ )
547
+
548
+ megatron_lm_config[prefix + "recompute_activations"] = _ask_field(
549
+ "Do you want to enable selective activation recomputation? [YES/no]: ",
550
+ _convert_yes_no_to_bool,
551
+ default=True,
552
+ error_message="Please enter yes or no.",
553
+ )
554
+
555
+ megatron_lm_config[prefix + "use_distributed_optimizer"] = _ask_field(
556
+ "Do you want to use distributed optimizer "
557
+ "which shards optimizer state and gradients across data parallel ranks? [YES/no]: ",
558
+ _convert_yes_no_to_bool,
559
+ default=True,
560
+ error_message="Please enter yes or no.",
561
+ )
562
+
563
+ megatron_lm_config[prefix + "gradient_clipping"] = _ask_field(
564
+ "What is the gradient clipping value based on global L2 Norm (0 to disable)? [1.0]: ",
565
+ float,
566
+ default=1.0,
567
+ )
568
+ # TPU specific defaults
569
+ tpu_commands = None
570
+ tpu_command_file = None
571
+ tpu_downcast_bf16 = "no"
572
+ tpu_env = []
573
+ tpu_name = None
574
+ tpu_vm = None
575
+ tpu_zone = None
576
+ tpu_use_sudo = False
577
+ tpu_use_cluster = False
578
+
579
+ if distributed_type in [
580
+ DistributedType.MULTI_CPU,
581
+ DistributedType.MULTI_XPU,
582
+ DistributedType.MULTI_HPU,
583
+ DistributedType.MULTI_GPU,
584
+ DistributedType.MULTI_MLU,
585
+ DistributedType.MULTI_SDAA,
586
+ DistributedType.MULTI_MUSA,
587
+ DistributedType.MULTI_NPU,
588
+ DistributedType.XLA,
589
+ ]:
590
+ machine_type = str(distributed_type).split(".")[1].replace("MULTI_", "")
591
+ if machine_type == "TPU":
592
+ machine_type += " cores"
593
+ elif machine_type == "CPU":
594
+ machine_type = "processes"
595
+ else:
596
+ machine_type += "(s)"
597
+ num_processes = _ask_field(
598
+ f"How many {machine_type} should be used for distributed training? [1]:",
599
+ int,
600
+ default=1,
601
+ error_message="Please enter an integer.",
602
+ )
603
+ elif distributed_type in [DistributedType.FSDP, DistributedType.DEEPSPEED, DistributedType.MEGATRON_LM]:
604
+ num_processes = _ask_field(
605
+ "How many GPU(s) should be used for distributed training? [1]:",
606
+ int,
607
+ default=1,
608
+ error_message="Please enter an integer.",
609
+ )
610
+ else:
611
+ num_processes = 1
612
+
613
+ if (distributed_type == DistributedType.MULTI_GPU) and (num_machines == 1) and (num_processes == 1):
614
+ raise ValueError(
615
+ f"Specified distributed type {distributed_type} but only using 1 GPU on a single machine. Please select `No distributed training` for the type of machine you are using."
616
+ )
617
+
618
+ if (
619
+ distributed_type
620
+ in [
621
+ DistributedType.MULTI_GPU,
622
+ DistributedType.MULTI_MLU,
623
+ DistributedType.MULTI_SDAA,
624
+ DistributedType.MULTI_MUSA,
625
+ DistributedType.MULTI_NPU,
626
+ DistributedType.MULTI_XPU,
627
+ DistributedType.MULTI_HPU,
628
+ DistributedType.NO,
629
+ ]
630
+ and not use_cpu
631
+ and not use_mps
632
+ ):
633
+ if is_npu_available():
634
+ machine_type = "NPU(s)"
635
+ elif is_mlu_available():
636
+ machine_type = "MLU(s)"
637
+ elif is_sdaa_available():
638
+ machine_type = "SDAA(s)"
639
+ elif is_musa_available():
640
+ machine_type = "MUSA(s)"
641
+ elif is_xpu_available():
642
+ machine_type = "XPU(s)"
643
+ elif is_hpu_available():
644
+ machine_type = "HPU(s)"
645
+ else:
646
+ machine_type = "GPU(s)"
647
+ gpu_ids = _ask_field(
648
+ f"What {machine_type} (by id) should be used for training on this machine as a comma-separated list? [all]:",
649
+ default="all",
650
+ )
651
+
652
+ # CPU affinity is only supported on NVIDIA hardware for now
653
+ enable_cpu_affinity = False
654
+ if distributed_type in (DistributedType.NO, DistributedType.MULTI_GPU) and not use_cpu and not use_mps:
655
+ enable_cpu_affinity = _ask_field(
656
+ "Would you like to enable numa efficiency? (Currently only supported on NVIDIA hardware). [yes/NO]: ",
657
+ _convert_yes_no_to_bool,
658
+ default=False,
659
+ error_message="Please enter yes or no.",
660
+ )
661
+
662
+ fp8_config = None
663
+ if distributed_type == DistributedType.XLA:
664
+ mixed_precision = "no"
665
+ main_training_function = _ask_field(
666
+ "What is the name of the function in your script that should be launched in all parallel scripts? [main]: ",
667
+ default="main",
668
+ )
669
+ tpu_use_cluster = _ask_field(
670
+ "Are you using a TPU cluster? [yes/NO]: ",
671
+ _convert_yes_no_to_bool,
672
+ default=False,
673
+ error_message="Please enter yes or no.",
674
+ )
675
+ if tpu_use_cluster:
676
+ tpu_name = _ask_field(
677
+ "What is the name of your TPU cluster? ",
678
+ default=None,
679
+ error_message="Please enter the name of your TPU cluster.",
680
+ )
681
+ tpu_zone = _ask_field(
682
+ "What is the zone of your TPU cluster? ",
683
+ default=None,
684
+ error_message="Please enter the zone of your TPU cluster.",
685
+ )
686
+ tpu_use_sudo = _ask_field(
687
+ "To run a python script in a TPU pod, should `sudo` be used? [yes/NO]: ",
688
+ default=False,
689
+ error_message="Please enter yes or no.",
690
+ )
691
+ run_commands = _ask_field(
692
+ "Do you have code you wish to run on startup in each pod? [yes/NO]: ",
693
+ _convert_yes_no_to_bool,
694
+ default=False,
695
+ error_message="Please enter yes or no.",
696
+ )
697
+ if run_commands:
698
+ use_command_file = _ask_field(
699
+ "Is this code located in a bash script? [yes/NO]: ",
700
+ _convert_yes_no_to_bool,
701
+ default=False,
702
+ error_message="Please enter yes or no.",
703
+ )
704
+ if use_command_file:
705
+ tpu_command_file = _ask_field(
706
+ "What is the path to your bash script? ",
707
+ default=None,
708
+ error_message="Please enter the path to your bash script.",
709
+ )
710
+ tpu_command_file = os.path.abspath(tpu_command_file)
711
+ else:
712
+ print("Please enter each command separately you wish to run on startup in each pod.")
713
+ tpu_commands = []
714
+ another_command = True
715
+ while another_command:
716
+ tpu_commands.append(
717
+ _ask_field(
718
+ "Please enter a single command to be ran ",
719
+ default=None,
720
+ error_message="Please enter the commands you wish to run on startup in each pod as a single string.",
721
+ )
722
+ )
723
+ another_command = _ask_field(
724
+ "Do you wish to add another command? [yes/NO]: ",
725
+ _convert_yes_no_to_bool,
726
+ default=False,
727
+ error_message="Please enter yes or no.",
728
+ )
729
+ tpu_vm = _ask_field(
730
+ "If not using an instance group, what are the names of the Compute VM instances to be used, separated by a comma: ",
731
+ default="",
732
+ ).split(",")
733
+ tpu_env = _ask_field(
734
+ "What environment variables do you wish to set in each pod, separated by a comma: ",
735
+ default="",
736
+ ).split(",")
737
+
738
+ else:
739
+ main_training_function = "main"
740
+ if distributed_type == DistributedType.DEEPSPEED and use_deepspeed_config:
741
+ mixed_precision = None
742
+ else:
743
+ mixed_precision = _ask_options(
744
+ "Do you wish to use mixed precision?",
745
+ ["no", "fp16", "bf16", "fp8"],
746
+ _convert_mixed_precision,
747
+ )
748
+ if mixed_precision == "fp8":
749
+ if not is_fp8_available():
750
+ raise ValueError("FP8 (either Transformer Engine or MSAMP) is not installed on this machine.")
751
+ fp8_config = {}
752
+ fp8_config["backend"] = _ask_options(
753
+ "Which FP8 backend do you want to use?",
754
+ ["te", "msamp"],
755
+ _convert_fp8_backend,
756
+ )
757
+ if fp8_config["backend"] == "TE":
758
+ if not is_transformer_engine_available():
759
+ raise ValueError("TransformersEngine was selected, but it is not installed on this machine.")
760
+ fp8_config["use_autocast_during_eval"] = _ask_field(
761
+ "Do you want to use FP8 autocast during eval mode? Generally better metrics are found when this is disabled [yes/NO]: ",
762
+ _convert_yes_no_to_bool,
763
+ default=False,
764
+ )
765
+ fp8_config["margin"] = _ask_field(
766
+ "What margin should be used for gradient scaling? [0]: ",
767
+ int,
768
+ default=0,
769
+ )
770
+ fp8_config["interval"] = _ask_field(
771
+ "What interval should be used for for how often the scaling factor is recomputed? [1]: ",
772
+ int,
773
+ default=1,
774
+ )
775
+ fp8_config["fp8_format"] = _ask_options(
776
+ "Which weight format should be used?",
777
+ ["HYBRID", "E4M3"],
778
+ lambda x: "HYBRID" if x == 0 else "E4M3",
779
+ default=0,
780
+ )
781
+ fp8_config["amax_history_length"] = _ask_field(
782
+ "What length of history should be used for the amax scaling factor computation? [1024]: ",
783
+ int,
784
+ default=1024,
785
+ )
786
+ fp8_config["amax_compute_algorithm"] = _ask_options(
787
+ "Which algorithm should be used for the amax scaling factor computation?",
788
+ ["max", "most_recent"],
789
+ lambda x: "max" if x == 0 else "most_recent",
790
+ default=0,
791
+ )
792
+ fp8_config["override_linear_precision"] = _ask_field(
793
+ "Do you want to to execute `fprop`, `dgrad`, and `wgrad` GEMMS in higher precision? [yes/NO]: ",
794
+ _convert_yes_no_to_bool,
795
+ default=False,
796
+ )
797
+ if fp8_config["override_linear_precision"]:
798
+ fprop = _ask_field(
799
+ "Should `fprop` be executed in higher precision? [yes/NO]: ",
800
+ _convert_yes_no_to_bool,
801
+ default=False,
802
+ )
803
+ dgrad = _ask_field(
804
+ "Should `dgrad` be executed in higher precision? [yes/NO]: ",
805
+ _convert_yes_no_to_bool,
806
+ default=False,
807
+ )
808
+ wgrad = _ask_field(
809
+ "Should `wgrad` be executed in higher precision? [yes/NO]: ",
810
+ _convert_yes_no_to_bool,
811
+ default=False,
812
+ )
813
+ fp8_config["override_linear_precision"] = (fprop, dgrad, wgrad)
814
+ else:
815
+ fp8_config["override_linear_precision"] = (False, False, False)
816
+
817
+ elif fp8_config["backend"] == "MSAMP":
818
+ if not is_msamp_available():
819
+ raise ValueError("MSAMP was selected, but it is not installed on this machine.")
820
+ fp8_config["optimization_level"] = _ask_options(
821
+ "Which optimization level should be used?",
822
+ ["O1", "O2"],
823
+ lambda x: "O1" if x == 0 else "O2",
824
+ default=1,
825
+ )
826
+
827
+ if use_dynamo and mixed_precision == "no" and not use_cpu:
828
+ print(
829
+ "Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts."
830
+ )
831
+
832
+ if distributed_type == DistributedType.XLA and mixed_precision == "bf16":
833
+ tpu_downcast_bf16 = _ask_field(
834
+ "Should `torch.float` be cast as `bfloat16` and `torch.double` remain `float32` on TPUs?", default="no"
835
+ )
836
+
837
+ return ClusterConfig(
838
+ compute_environment=ComputeEnvironment.LOCAL_MACHINE,
839
+ distributed_type=distributed_type,
840
+ num_processes=num_processes,
841
+ gpu_ids=gpu_ids,
842
+ mixed_precision=mixed_precision,
843
+ downcast_bf16=tpu_downcast_bf16,
844
+ machine_rank=machine_rank,
845
+ num_machines=num_machines,
846
+ main_process_ip=main_process_ip,
847
+ main_process_port=main_process_port,
848
+ main_training_function=main_training_function,
849
+ fp8_config=fp8_config,
850
+ deepspeed_config=deepspeed_config,
851
+ fsdp_config=fsdp_config,
852
+ megatron_lm_config=megatron_lm_config,
853
+ ipex_config=ipex_config,
854
+ mpirun_config=mpirun_config,
855
+ use_cpu=use_cpu,
856
+ rdzv_backend=rdzv_backend,
857
+ same_network=same_network,
858
+ commands=tpu_commands,
859
+ command_file=tpu_command_file,
860
+ tpu_env=tpu_env,
861
+ tpu_name=tpu_name,
862
+ tpu_vm=tpu_vm,
863
+ tpu_zone=tpu_zone,
864
+ tpu_use_sudo=tpu_use_sudo,
865
+ tpu_use_cluster=tpu_use_cluster,
866
+ dynamo_config=dynamo_config,
867
+ debug=debug,
868
+ enable_cpu_affinity=enable_cpu_affinity,
869
+ )
lib/python3.12/site-packages/accelerate/commands/config/config.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright 2021 The HuggingFace Team. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ import argparse
18
+ import os
19
+
20
+ from accelerate.utils import ComputeEnvironment
21
+
22
+ from .cluster import get_cluster_input
23
+ from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file # noqa: F401
24
+ from .config_utils import _ask_field, _ask_options, _convert_compute_environment # noqa: F401
25
+ from .sagemaker import get_sagemaker_input
26
+
27
+
28
+ description = "Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine"
29
+
30
+
31
+ def get_user_input():
32
+ compute_environment = _ask_options(
33
+ "In which compute environment are you running?",
34
+ ["This machine", "AWS (Amazon SageMaker)"],
35
+ _convert_compute_environment,
36
+ )
37
+ if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
38
+ config = get_sagemaker_input()
39
+ else:
40
+ config = get_cluster_input()
41
+ return config
42
+
43
+
44
+ def config_command_parser(subparsers=None):
45
+ if subparsers is not None:
46
+ parser = subparsers.add_parser("config", description=description)
47
+ else:
48
+ parser = argparse.ArgumentParser("Accelerate config command", description=description)
49
+
50
+ parser.add_argument(
51
+ "--config_file",
52
+ default=None,
53
+ help=(
54
+ "The path to use to store the config file. Will default to a file named default_config.yaml in the cache "
55
+ "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have "
56
+ "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed "
57
+ "with 'huggingface'."
58
+ ),
59
+ )
60
+
61
+ if subparsers is not None:
62
+ parser.set_defaults(func=config_command)
63
+ return parser
64
+
65
+
66
+ def config_command(args):
67
+ config = get_user_input()
68
+ if args.config_file is not None:
69
+ config_file = args.config_file
70
+ else:
71
+ if not os.path.isdir(cache_dir):
72
+ os.makedirs(cache_dir)
73
+ config_file = default_yaml_config_file
74
+
75
+ if config_file.endswith(".json"):
76
+ config.to_json_file(config_file)
77
+ else:
78
+ config.to_yaml_file(config_file)
79
+ print(f"accelerate configuration saved at {config_file}")
80
+
81
+
82
+ def main():
83
+ parser = config_command_parser()
84
+ args = parser.parse_args()
85
+ config_command(args)
86
+
87
+
88
+ if __name__ == "__main__":
89
+ main()
lib/python3.12/site-packages/accelerate/commands/config/config_args.py ADDED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright 2021 The HuggingFace Team. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ import json
18
+ import os
19
+ from dataclasses import dataclass
20
+ from enum import Enum
21
+ from typing import Optional, Union
22
+
23
+ import yaml
24
+
25
+ from ...utils import ComputeEnvironment, DistributedType, SageMakerDistributedType
26
+ from ...utils.constants import SAGEMAKER_PYTHON_VERSION, SAGEMAKER_PYTORCH_VERSION, SAGEMAKER_TRANSFORMERS_VERSION
27
+
28
+
29
+ hf_cache_home = os.path.expanduser(
30
+ os.environ.get("HF_HOME", os.path.join(os.environ.get("XDG_CACHE_HOME", "~/.cache"), "huggingface"))
31
+ )
32
+ cache_dir = os.path.join(hf_cache_home, "accelerate")
33
+ default_json_config_file = os.path.join(cache_dir, "default_config.yaml")
34
+ default_yaml_config_file = os.path.join(cache_dir, "default_config.yaml")
35
+
36
+ # For backward compatibility: the default config is the json one if it's the only existing file.
37
+ if os.path.isfile(default_yaml_config_file) or not os.path.isfile(default_json_config_file):
38
+ default_config_file = default_yaml_config_file
39
+ else:
40
+ default_config_file = default_json_config_file
41
+
42
+
43
+ def load_config_from_file(config_file):
44
+ if config_file is not None:
45
+ if not os.path.isfile(config_file):
46
+ raise FileNotFoundError(
47
+ f"The passed configuration file `{config_file}` does not exist. "
48
+ "Please pass an existing file to `accelerate launch`, or use the default one "
49
+ "created through `accelerate config` and run `accelerate launch` "
50
+ "without the `--config_file` argument."
51
+ )
52
+ else:
53
+ config_file = default_config_file
54
+ with open(config_file, encoding="utf-8") as f:
55
+ if config_file.endswith(".json"):
56
+ if (
57
+ json.load(f).get("compute_environment", ComputeEnvironment.LOCAL_MACHINE)
58
+ == ComputeEnvironment.LOCAL_MACHINE
59
+ ):
60
+ config_class = ClusterConfig
61
+ else:
62
+ config_class = SageMakerConfig
63
+ return config_class.from_json_file(json_file=config_file)
64
+ else:
65
+ if (
66
+ yaml.safe_load(f).get("compute_environment", ComputeEnvironment.LOCAL_MACHINE)
67
+ == ComputeEnvironment.LOCAL_MACHINE
68
+ ):
69
+ config_class = ClusterConfig
70
+ else:
71
+ config_class = SageMakerConfig
72
+ return config_class.from_yaml_file(yaml_file=config_file)
73
+
74
+
75
+ @dataclass
76
+ class BaseConfig:
77
+ compute_environment: ComputeEnvironment
78
+ distributed_type: Union[DistributedType, SageMakerDistributedType]
79
+ mixed_precision: str
80
+ use_cpu: bool
81
+ debug: bool
82
+
83
+ def to_dict(self):
84
+ result = self.__dict__
85
+ # For serialization, it's best to convert Enums to strings (or their underlying value type).
86
+
87
+ def _convert_enums(value):
88
+ if isinstance(value, Enum):
89
+ return value.value
90
+ if isinstance(value, dict):
91
+ if not bool(value):
92
+ return None
93
+ for key1, value1 in value.items():
94
+ value[key1] = _convert_enums(value1)
95
+ return value
96
+
97
+ for key, value in result.items():
98
+ result[key] = _convert_enums(value)
99
+ result = {k: v for k, v in result.items() if v is not None}
100
+ return result
101
+
102
+ @staticmethod
103
+ def process_config(config_dict):
104
+ """
105
+ Processes `config_dict` and sets default values for any missing keys
106
+ """
107
+ if "compute_environment" not in config_dict:
108
+ config_dict["compute_environment"] = ComputeEnvironment.LOCAL_MACHINE
109
+ if "distributed_type" not in config_dict:
110
+ raise ValueError("A `distributed_type` must be specified in the config file.")
111
+ if "num_processes" not in config_dict and config_dict["distributed_type"] == DistributedType.NO:
112
+ config_dict["num_processes"] = 1
113
+ if "mixed_precision" not in config_dict:
114
+ config_dict["mixed_precision"] = "fp16" if ("fp16" in config_dict and config_dict["fp16"]) else None
115
+ if "fp16" in config_dict: # Convert the config to the new format.
116
+ del config_dict["fp16"]
117
+ if "dynamo_backend" in config_dict: # Convert the config to the new format.
118
+ dynamo_backend = config_dict.pop("dynamo_backend")
119
+ config_dict["dynamo_config"] = {} if dynamo_backend == "NO" else {"dynamo_backend": dynamo_backend}
120
+ if "use_cpu" not in config_dict:
121
+ config_dict["use_cpu"] = False
122
+ if "debug" not in config_dict:
123
+ config_dict["debug"] = False
124
+ if "enable_cpu_affinity" not in config_dict:
125
+ config_dict["enable_cpu_affinity"] = False
126
+ return config_dict
127
+
128
+ @classmethod
129
+ def from_json_file(cls, json_file=None):
130
+ json_file = default_json_config_file if json_file is None else json_file
131
+ with open(json_file, encoding="utf-8") as f:
132
+ config_dict = json.load(f)
133
+ config_dict = cls.process_config(config_dict)
134
+ extra_keys = sorted(set(config_dict.keys()) - set(cls.__dataclass_fields__.keys()))
135
+ if len(extra_keys) > 0:
136
+ raise ValueError(
137
+ f"The config file at {json_file} had unknown keys ({extra_keys}), please try upgrading your `accelerate`"
138
+ " version or fix (and potentially remove) these keys from your config file."
139
+ )
140
+
141
+ return cls(**config_dict)
142
+
143
+ def to_json_file(self, json_file):
144
+ with open(json_file, "w", encoding="utf-8") as f:
145
+ content = json.dumps(self.to_dict(), indent=2, sort_keys=True) + "\n"
146
+ f.write(content)
147
+
148
+ @classmethod
149
+ def from_yaml_file(cls, yaml_file=None):
150
+ yaml_file = default_yaml_config_file if yaml_file is None else yaml_file
151
+ with open(yaml_file, encoding="utf-8") as f:
152
+ config_dict = yaml.safe_load(f)
153
+ config_dict = cls.process_config(config_dict)
154
+ extra_keys = sorted(set(config_dict.keys()) - set(cls.__dataclass_fields__.keys()))
155
+ if len(extra_keys) > 0:
156
+ raise ValueError(
157
+ f"The config file at {yaml_file} had unknown keys ({extra_keys}), please try upgrading your `accelerate`"
158
+ " version or fix (and potentially remove) these keys from your config file."
159
+ )
160
+ return cls(**config_dict)
161
+
162
+ def to_yaml_file(self, yaml_file):
163
+ with open(yaml_file, "w", encoding="utf-8") as f:
164
+ yaml.safe_dump(self.to_dict(), f)
165
+
166
+ def __post_init__(self):
167
+ if isinstance(self.compute_environment, str):
168
+ self.compute_environment = ComputeEnvironment(self.compute_environment)
169
+ if isinstance(self.distributed_type, str):
170
+ if self.compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
171
+ self.distributed_type = SageMakerDistributedType(self.distributed_type)
172
+ else:
173
+ self.distributed_type = DistributedType(self.distributed_type)
174
+ if getattr(self, "dynamo_config", None) is None:
175
+ self.dynamo_config = {}
176
+
177
+
178
+ @dataclass
179
+ class ClusterConfig(BaseConfig):
180
+ num_processes: int = -1 # For instance if we use SLURM and the user manually passes it in
181
+ machine_rank: int = 0
182
+ num_machines: int = 1
183
+ gpu_ids: Optional[str] = None
184
+ main_process_ip: Optional[str] = None
185
+ main_process_port: Optional[int] = None
186
+ rdzv_backend: Optional[str] = "static"
187
+ same_network: Optional[bool] = False
188
+ main_training_function: str = "main"
189
+ enable_cpu_affinity: bool = False
190
+
191
+ # args for FP8 training
192
+ fp8_config: dict = None
193
+ # args for deepspeed_plugin
194
+ deepspeed_config: dict = None
195
+ # args for fsdp
196
+ fsdp_config: dict = None
197
+ # args for megatron_lm
198
+ megatron_lm_config: dict = None
199
+ # args for ipex
200
+ ipex_config: dict = None
201
+ # args for mpirun
202
+ mpirun_config: dict = None
203
+ # args for TPU
204
+ downcast_bf16: bool = False
205
+
206
+ # args for TPU pods
207
+ tpu_name: str = None
208
+ tpu_zone: str = None
209
+ tpu_use_cluster: bool = False
210
+ tpu_use_sudo: bool = False
211
+ command_file: str = None
212
+ commands: list[str] = None
213
+ tpu_vm: list[str] = None
214
+ tpu_env: list[str] = None
215
+
216
+ # args for dynamo
217
+ dynamo_config: dict = None
218
+
219
+ def __post_init__(self):
220
+ if self.deepspeed_config is None:
221
+ self.deepspeed_config = {}
222
+ if self.fsdp_config is None:
223
+ self.fsdp_config = {}
224
+ if self.megatron_lm_config is None:
225
+ self.megatron_lm_config = {}
226
+ if self.ipex_config is None:
227
+ self.ipex_config = {}
228
+ if self.mpirun_config is None:
229
+ self.mpirun_config = {}
230
+ if self.fp8_config is None:
231
+ self.fp8_config = {}
232
+ return super().__post_init__()
233
+
234
+
235
+ @dataclass
236
+ class SageMakerConfig(BaseConfig):
237
+ ec2_instance_type: str
238
+ iam_role_name: str
239
+ image_uri: Optional[str] = None
240
+ profile: Optional[str] = None
241
+ region: str = "us-east-1"
242
+ num_machines: int = 1
243
+ gpu_ids: str = "all"
244
+ base_job_name: str = f"accelerate-sagemaker-{num_machines}"
245
+ pytorch_version: str = SAGEMAKER_PYTORCH_VERSION
246
+ transformers_version: str = SAGEMAKER_TRANSFORMERS_VERSION
247
+ py_version: str = SAGEMAKER_PYTHON_VERSION
248
+ sagemaker_inputs_file: str = None
249
+ sagemaker_metrics_file: str = None
250
+ additional_args: dict = None
251
+ dynamo_config: dict = None
252
+ enable_cpu_affinity: bool = False
lib/python3.12/site-packages/accelerate/commands/config/config_utils.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright 2021 The HuggingFace Team. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ import argparse
18
+
19
+ from ...utils.dataclasses import (
20
+ ComputeEnvironment,
21
+ DistributedType,
22
+ DynamoBackend,
23
+ FP8BackendType,
24
+ PrecisionType,
25
+ SageMakerDistributedType,
26
+ )
27
+ from ..menu import BulletMenu
28
+
29
+
30
+ DYNAMO_BACKENDS = [
31
+ "EAGER",
32
+ "AOT_EAGER",
33
+ "INDUCTOR",
34
+ "AOT_TS_NVFUSER",
35
+ "NVPRIMS_NVFUSER",
36
+ "CUDAGRAPHS",
37
+ "OFI",
38
+ "FX2TRT",
39
+ "ONNXRT",
40
+ "TENSORRT",
41
+ "AOT_TORCHXLA_TRACE_ONCE",
42
+ "TORHCHXLA_TRACE_ONCE",
43
+ "IPEX",
44
+ "TVM",
45
+ ]
46
+
47
+
48
+ def _ask_field(input_text, convert_value=None, default=None, error_message=None):
49
+ ask_again = True
50
+ while ask_again:
51
+ result = input(input_text)
52
+ try:
53
+ if default is not None and len(result) == 0:
54
+ return default
55
+ return convert_value(result) if convert_value is not None else result
56
+ except Exception:
57
+ if error_message is not None:
58
+ print(error_message)
59
+
60
+
61
+ def _ask_options(input_text, options=[], convert_value=None, default=0):
62
+ menu = BulletMenu(input_text, options)
63
+ result = menu.run(default_choice=default)
64
+ return convert_value(result) if convert_value is not None else result
65
+
66
+
67
+ def _convert_compute_environment(value):
68
+ value = int(value)
69
+ return ComputeEnvironment(["LOCAL_MACHINE", "AMAZON_SAGEMAKER"][value])
70
+
71
+
72
+ def _convert_distributed_mode(value):
73
+ value = int(value)
74
+ return DistributedType(
75
+ [
76
+ "NO",
77
+ "MULTI_CPU",
78
+ "MULTI_XPU",
79
+ "MULTI_HPU",
80
+ "MULTI_GPU",
81
+ "MULTI_NPU",
82
+ "MULTI_MLU",
83
+ "MULTI_SDAA",
84
+ "MULTI_MUSA",
85
+ "XLA",
86
+ ][value]
87
+ )
88
+
89
+
90
+ def _convert_dynamo_backend(value):
91
+ value = int(value)
92
+ return DynamoBackend(DYNAMO_BACKENDS[value]).value
93
+
94
+
95
+ def _convert_mixed_precision(value):
96
+ value = int(value)
97
+ return PrecisionType(["no", "fp16", "bf16", "fp8"][value])
98
+
99
+
100
+ def _convert_sagemaker_distributed_mode(value):
101
+ value = int(value)
102
+ return SageMakerDistributedType(["NO", "DATA_PARALLEL", "MODEL_PARALLEL"][value])
103
+
104
+
105
+ def _convert_fp8_backend(value):
106
+ value = int(value)
107
+ return FP8BackendType(["TE", "MSAMP"][value])
108
+
109
+
110
+ def _convert_yes_no_to_bool(value):
111
+ return {"yes": True, "no": False}[value.lower()]
112
+
113
+
114
+ class SubcommandHelpFormatter(argparse.RawDescriptionHelpFormatter):
115
+ """
116
+ A custom formatter that will remove the usage line from the help message for subcommands.
117
+ """
118
+
119
+ def _format_usage(self, usage, actions, groups, prefix):
120
+ usage = super()._format_usage(usage, actions, groups, prefix)
121
+ usage = usage.replace("<command> [<args>] ", "")
122
+ return usage
lib/python3.12/site-packages/accelerate/commands/config/default.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright 2021 The HuggingFace Team. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ from pathlib import Path
18
+
19
+ import torch
20
+
21
+ from ...utils import (
22
+ is_hpu_available,
23
+ is_mlu_available,
24
+ is_musa_available,
25
+ is_npu_available,
26
+ is_sdaa_available,
27
+ is_xpu_available,
28
+ )
29
+ from .config_args import ClusterConfig, default_json_config_file
30
+ from .config_utils import SubcommandHelpFormatter
31
+
32
+
33
+ description = "Create a default config file for Accelerate with only a few flags set."
34
+
35
+
36
+ def write_basic_config(mixed_precision="no", save_location: str = default_json_config_file):
37
+ """
38
+ Creates and saves a basic cluster config to be used on a local machine with potentially multiple GPUs. Will also
39
+ set CPU if it is a CPU-only machine.
40
+
41
+ Args:
42
+ mixed_precision (`str`, *optional*, defaults to "no"):
43
+ Mixed Precision to use. Should be one of "no", "fp16", or "bf16"
44
+ save_location (`str`, *optional*, defaults to `default_json_config_file`):
45
+ Optional custom save location. Should be passed to `--config_file` when using `accelerate launch`. Default
46
+ location is inside the huggingface cache folder (`~/.cache/huggingface`) but can be overridden by setting
47
+ the `HF_HOME` environmental variable, followed by `accelerate/default_config.yaml`.
48
+ """
49
+ path = Path(save_location)
50
+ path.parent.mkdir(parents=True, exist_ok=True)
51
+ if path.exists():
52
+ print(
53
+ f"Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`."
54
+ )
55
+ return False
56
+ mixed_precision = mixed_precision.lower()
57
+ if mixed_precision not in ["no", "fp16", "bf16", "fp8"]:
58
+ raise ValueError(
59
+ f"`mixed_precision` should be one of 'no', 'fp16', 'bf16', or 'fp8'. Received {mixed_precision}"
60
+ )
61
+ config = {
62
+ "compute_environment": "LOCAL_MACHINE",
63
+ "mixed_precision": mixed_precision,
64
+ }
65
+ if is_mlu_available():
66
+ num_mlus = torch.mlu.device_count()
67
+ config["num_processes"] = num_mlus
68
+ config["use_cpu"] = False
69
+ if num_mlus > 1:
70
+ config["distributed_type"] = "MULTI_MLU"
71
+ else:
72
+ config["distributed_type"] = "NO"
73
+ if is_sdaa_available():
74
+ num_sdaas = torch.sdaa.device_count()
75
+ config["num_processes"] = num_sdaas
76
+ config["use_cpu"] = False
77
+ if num_sdaas > 1:
78
+ config["distributed_type"] = "MULTI_SDAA"
79
+ else:
80
+ config["distributed_type"] = "NO"
81
+ elif is_musa_available():
82
+ num_musas = torch.musa.device_count()
83
+ config["num_processes"] = num_musas
84
+ config["use_cpu"] = False
85
+ if num_musas > 1:
86
+ config["distributed_type"] = "MULTI_MUSA"
87
+ else:
88
+ config["distributed_type"] = "NO"
89
+ elif is_hpu_available():
90
+ num_hpus = torch.hpu.device_count()
91
+ config["num_processes"] = num_hpus
92
+ config["use_cpu"] = False
93
+ if num_hpus > 1:
94
+ config["distributed_type"] = "MULTI_HPU"
95
+ else:
96
+ config["distributed_type"] = "NO"
97
+ elif torch.cuda.is_available():
98
+ num_gpus = torch.cuda.device_count()
99
+ config["num_processes"] = num_gpus
100
+ config["use_cpu"] = False
101
+ if num_gpus > 1:
102
+ config["distributed_type"] = "MULTI_GPU"
103
+ else:
104
+ config["distributed_type"] = "NO"
105
+ elif is_xpu_available():
106
+ num_xpus = torch.xpu.device_count()
107
+ config["num_processes"] = num_xpus
108
+ config["use_cpu"] = False
109
+ if num_xpus > 1:
110
+ config["distributed_type"] = "MULTI_XPU"
111
+ else:
112
+ config["distributed_type"] = "NO"
113
+ elif is_npu_available():
114
+ num_npus = torch.npu.device_count()
115
+ config["num_processes"] = num_npus
116
+ config["use_cpu"] = False
117
+ if num_npus > 1:
118
+ config["distributed_type"] = "MULTI_NPU"
119
+ else:
120
+ config["distributed_type"] = "NO"
121
+ else:
122
+ num_xpus = 0
123
+ config["use_cpu"] = True
124
+ config["num_processes"] = 1
125
+ config["distributed_type"] = "NO"
126
+ config["debug"] = False
127
+ config["enable_cpu_affinity"] = False
128
+ config = ClusterConfig(**config)
129
+ config.to_json_file(path)
130
+ return path
131
+
132
+
133
+ def default_command_parser(parser, parents):
134
+ parser = parser.add_parser("default", parents=parents, help=description, formatter_class=SubcommandHelpFormatter)
135
+ parser.add_argument(
136
+ "--config_file",
137
+ default=default_json_config_file,
138
+ help=(
139
+ "The path to use to store the config file. Will default to a file named default_config.yaml in the cache "
140
+ "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have "
141
+ "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed "
142
+ "with 'huggingface'."
143
+ ),
144
+ dest="save_location",
145
+ )
146
+
147
+ parser.add_argument(
148
+ "--mixed_precision",
149
+ choices=["no", "fp16", "bf16"],
150
+ type=str,
151
+ help="Whether or not to use mixed precision training. "
152
+ "Choose between FP16 and BF16 (bfloat16) training. "
153
+ "BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.",
154
+ default="no",
155
+ )
156
+ parser.set_defaults(func=default_config_command)
157
+ return parser
158
+
159
+
160
+ def default_config_command(args):
161
+ config_file = write_basic_config(args.mixed_precision, args.save_location)
162
+ if config_file:
163
+ print(f"accelerate configuration saved at {config_file}")
lib/python3.12/site-packages/accelerate/commands/config/sagemaker.py ADDED
@@ -0,0 +1,274 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright 2021 The HuggingFace Team. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ import json
17
+ import os
18
+
19
+ from ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES
20
+ from ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType
21
+ from ...utils.imports import is_boto3_available
22
+ from .config_args import SageMakerConfig
23
+ from .config_utils import (
24
+ DYNAMO_BACKENDS,
25
+ _ask_field,
26
+ _ask_options,
27
+ _convert_dynamo_backend,
28
+ _convert_mixed_precision,
29
+ _convert_sagemaker_distributed_mode,
30
+ _convert_yes_no_to_bool,
31
+ )
32
+
33
+
34
+ if is_boto3_available():
35
+ import boto3 # noqa: F401
36
+
37
+
38
+ def _create_iam_role_for_sagemaker(role_name):
39
+ iam_client = boto3.client("iam")
40
+
41
+ sagemaker_trust_policy = {
42
+ "Version": "2012-10-17",
43
+ "Statement": [
44
+ {"Effect": "Allow", "Principal": {"Service": "sagemaker.amazonaws.com"}, "Action": "sts:AssumeRole"}
45
+ ],
46
+ }
47
+ try:
48
+ # create the role, associated with the chosen trust policy
49
+ iam_client.create_role(
50
+ RoleName=role_name, AssumeRolePolicyDocument=json.dumps(sagemaker_trust_policy, indent=2)
51
+ )
52
+ policy_document = {
53
+ "Version": "2012-10-17",
54
+ "Statement": [
55
+ {
56
+ "Effect": "Allow",
57
+ "Action": [
58
+ "sagemaker:*",
59
+ "ecr:GetDownloadUrlForLayer",
60
+ "ecr:BatchGetImage",
61
+ "ecr:BatchCheckLayerAvailability",
62
+ "ecr:GetAuthorizationToken",
63
+ "cloudwatch:PutMetricData",
64
+ "cloudwatch:GetMetricData",
65
+ "cloudwatch:GetMetricStatistics",
66
+ "cloudwatch:ListMetrics",
67
+ "logs:CreateLogGroup",
68
+ "logs:CreateLogStream",
69
+ "logs:DescribeLogStreams",
70
+ "logs:PutLogEvents",
71
+ "logs:GetLogEvents",
72
+ "s3:CreateBucket",
73
+ "s3:ListBucket",
74
+ "s3:GetBucketLocation",
75
+ "s3:GetObject",
76
+ "s3:PutObject",
77
+ ],
78
+ "Resource": "*",
79
+ }
80
+ ],
81
+ }
82
+ # attach policy to role
83
+ iam_client.put_role_policy(
84
+ RoleName=role_name,
85
+ PolicyName=f"{role_name}_policy_permission",
86
+ PolicyDocument=json.dumps(policy_document, indent=2),
87
+ )
88
+ except iam_client.exceptions.EntityAlreadyExistsException:
89
+ print(f"role {role_name} already exists. Using existing one")
90
+
91
+
92
+ def _get_iam_role_arn(role_name):
93
+ iam_client = boto3.client("iam")
94
+ return iam_client.get_role(RoleName=role_name)["Role"]["Arn"]
95
+
96
+
97
+ def get_sagemaker_input():
98
+ credentials_configuration = _ask_options(
99
+ "How do you want to authorize?",
100
+ ["AWS Profile", "Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) "],
101
+ int,
102
+ )
103
+ aws_profile = None
104
+ if credentials_configuration == 0:
105
+ aws_profile = _ask_field("Enter your AWS Profile name: [default] ", default="default")
106
+ os.environ["AWS_PROFILE"] = aws_profile
107
+ else:
108
+ print(
109
+ "Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,"
110
+ "`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`"
111
+ )
112
+ aws_access_key_id = _ask_field("AWS Access Key ID: ")
113
+ os.environ["AWS_ACCESS_KEY_ID"] = aws_access_key_id
114
+
115
+ aws_secret_access_key = _ask_field("AWS Secret Access Key: ")
116
+ os.environ["AWS_SECRET_ACCESS_KEY"] = aws_secret_access_key
117
+
118
+ aws_region = _ask_field("Enter your AWS Region: [us-east-1]", default="us-east-1")
119
+ os.environ["AWS_DEFAULT_REGION"] = aws_region
120
+
121
+ role_management = _ask_options(
122
+ "Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?",
123
+ ["Provide IAM Role name", "Create new IAM role using credentials"],
124
+ int,
125
+ )
126
+ if role_management == 0:
127
+ iam_role_name = _ask_field("Enter your IAM role name: ")
128
+ else:
129
+ iam_role_name = "accelerate_sagemaker_execution_role"
130
+ print(f'Accelerate will create an iam role "{iam_role_name}" using the provided credentials')
131
+ _create_iam_role_for_sagemaker(iam_role_name)
132
+
133
+ is_custom_docker_image = _ask_field(
134
+ "Do you want to use custom Docker image? [yes/NO]: ",
135
+ _convert_yes_no_to_bool,
136
+ default=False,
137
+ error_message="Please enter yes or no.",
138
+ )
139
+ docker_image = None
140
+ if is_custom_docker_image:
141
+ docker_image = _ask_field("Enter your Docker image: ", lambda x: str(x).lower())
142
+
143
+ is_sagemaker_inputs_enabled = _ask_field(
144
+ "Do you want to provide SageMaker input channels with data locations? [yes/NO]: ",
145
+ _convert_yes_no_to_bool,
146
+ default=False,
147
+ error_message="Please enter yes or no.",
148
+ )
149
+ sagemaker_inputs_file = None
150
+ if is_sagemaker_inputs_enabled:
151
+ sagemaker_inputs_file = _ask_field(
152
+ "Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): ",
153
+ lambda x: str(x).lower(),
154
+ )
155
+
156
+ is_sagemaker_metrics_enabled = _ask_field(
157
+ "Do you want to enable SageMaker metrics? [yes/NO]: ",
158
+ _convert_yes_no_to_bool,
159
+ default=False,
160
+ error_message="Please enter yes or no.",
161
+ )
162
+ sagemaker_metrics_file = None
163
+ if is_sagemaker_metrics_enabled:
164
+ sagemaker_metrics_file = _ask_field(
165
+ "Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): ",
166
+ lambda x: str(x).lower(),
167
+ )
168
+
169
+ distributed_type = _ask_options(
170
+ "What is the distributed mode?",
171
+ ["No distributed training", "Data parallelism"],
172
+ _convert_sagemaker_distributed_mode,
173
+ )
174
+ dynamo_config = {}
175
+ use_dynamo = _ask_field(
176
+ "Do you wish to optimize your script with torch dynamo?[yes/NO]:",
177
+ _convert_yes_no_to_bool,
178
+ default=False,
179
+ error_message="Please enter yes or no.",
180
+ )
181
+ if use_dynamo:
182
+ prefix = "dynamo_"
183
+ dynamo_config[prefix + "backend"] = _ask_options(
184
+ "Which dynamo backend would you like to use?",
185
+ [x.lower() for x in DYNAMO_BACKENDS],
186
+ _convert_dynamo_backend,
187
+ default=2,
188
+ )
189
+ use_custom_options = _ask_field(
190
+ "Do you want to customize the defaults sent to torch.compile? [yes/NO]: ",
191
+ _convert_yes_no_to_bool,
192
+ default=False,
193
+ error_message="Please enter yes or no.",
194
+ )
195
+
196
+ if use_custom_options:
197
+ dynamo_config[prefix + "mode"] = _ask_options(
198
+ "Which mode do you want to use?",
199
+ TORCH_DYNAMO_MODES,
200
+ lambda x: TORCH_DYNAMO_MODES[int(x)],
201
+ default="default",
202
+ )
203
+ dynamo_config[prefix + "use_fullgraph"] = _ask_field(
204
+ "Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: ",
205
+ _convert_yes_no_to_bool,
206
+ default=False,
207
+ error_message="Please enter yes or no.",
208
+ )
209
+ dynamo_config[prefix + "use_dynamic"] = _ask_field(
210
+ "Do you want to enable dynamic shape tracing? [yes/NO]: ",
211
+ _convert_yes_no_to_bool,
212
+ default=False,
213
+ error_message="Please enter yes or no.",
214
+ )
215
+ dynamo_config[prefix + "use_regional_compilation"] = _ask_field(
216
+ "Do you want to enable regional compilation? [yes/NO]: ",
217
+ _convert_yes_no_to_bool,
218
+ default=False,
219
+ error_message="Please enter yes or no.",
220
+ )
221
+
222
+ ec2_instance_query = "Which EC2 instance type you want to use for your training?"
223
+ if distributed_type != SageMakerDistributedType.NO:
224
+ ec2_instance_type = _ask_options(
225
+ ec2_instance_query, SAGEMAKER_PARALLEL_EC2_INSTANCES, lambda x: SAGEMAKER_PARALLEL_EC2_INSTANCES[int(x)]
226
+ )
227
+ else:
228
+ ec2_instance_query += "? [ml.p3.2xlarge]:"
229
+ ec2_instance_type = _ask_field(ec2_instance_query, lambda x: str(x).lower(), default="ml.p3.2xlarge")
230
+
231
+ debug = False
232
+ if distributed_type != SageMakerDistributedType.NO:
233
+ debug = _ask_field(
234
+ "Should distributed operations be checked while running for errors? This can avoid timeout issues but will be slower. [yes/NO]: ",
235
+ _convert_yes_no_to_bool,
236
+ default=False,
237
+ error_message="Please enter yes or no.",
238
+ )
239
+
240
+ num_machines = 1
241
+ if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL):
242
+ num_machines = _ask_field(
243
+ "How many machines do you want use? [1]: ",
244
+ int,
245
+ default=1,
246
+ )
247
+
248
+ mixed_precision = _ask_options(
249
+ "Do you wish to use FP16 or BF16 (mixed precision)?",
250
+ ["no", "fp16", "bf16", "fp8"],
251
+ _convert_mixed_precision,
252
+ )
253
+
254
+ if use_dynamo and mixed_precision == "no":
255
+ print(
256
+ "Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts."
257
+ )
258
+
259
+ return SageMakerConfig(
260
+ image_uri=docker_image,
261
+ compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER,
262
+ distributed_type=distributed_type,
263
+ use_cpu=False,
264
+ dynamo_config=dynamo_config,
265
+ ec2_instance_type=ec2_instance_type,
266
+ profile=aws_profile,
267
+ region=aws_region,
268
+ iam_role_name=iam_role_name,
269
+ mixed_precision=mixed_precision,
270
+ num_machines=num_machines,
271
+ sagemaker_inputs_file=sagemaker_inputs_file,
272
+ sagemaker_metrics_file=sagemaker_metrics_file,
273
+ debug=debug,
274
+ )
lib/python3.12/site-packages/accelerate/commands/config/update.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright 2022 The HuggingFace Team. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ from pathlib import Path
18
+
19
+ from .config_args import default_config_file, load_config_from_file
20
+ from .config_utils import SubcommandHelpFormatter
21
+
22
+
23
+ description = "Update an existing config file with the latest defaults while maintaining the old configuration."
24
+
25
+
26
+ def update_config(args):
27
+ """
28
+ Update an existing config file with the latest defaults while maintaining the old configuration.
29
+ """
30
+ config_file = args.config_file
31
+ if config_file is None and Path(default_config_file).exists():
32
+ config_file = default_config_file
33
+ elif not Path(config_file).exists():
34
+ raise ValueError(f"The passed config file located at {config_file} doesn't exist.")
35
+ config = load_config_from_file(config_file)
36
+
37
+ if config_file.endswith(".json"):
38
+ config.to_json_file(config_file)
39
+ else:
40
+ config.to_yaml_file(config_file)
41
+ return config_file
42
+
43
+
44
+ def update_command_parser(parser, parents):
45
+ parser = parser.add_parser("update", parents=parents, help=description, formatter_class=SubcommandHelpFormatter)
46
+ parser.add_argument(
47
+ "--config_file",
48
+ default=None,
49
+ help=(
50
+ "The path to the config file to update. Will default to a file named default_config.yaml in the cache "
51
+ "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have "
52
+ "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed "
53
+ "with 'huggingface'."
54
+ ),
55
+ )
56
+
57
+ parser.set_defaults(func=update_config_command)
58
+ return parser
59
+
60
+
61
+ def update_config_command(args):
62
+ config_file = update_config(args)
63
+ print(f"Sucessfully updated the configuration file at {config_file}.")
lib/python3.12/site-packages/accelerate/commands/env.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright 2022 The HuggingFace Team. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ import argparse
18
+ import os
19
+ import platform
20
+ import subprocess
21
+
22
+ import numpy as np
23
+ import psutil
24
+ import torch
25
+
26
+ from accelerate import __version__ as version
27
+ from accelerate.commands.config import default_config_file, load_config_from_file
28
+
29
+ from ..utils import is_mlu_available, is_musa_available, is_npu_available, is_sdaa_available, is_xpu_available
30
+
31
+
32
+ def env_command_parser(subparsers=None):
33
+ if subparsers is not None:
34
+ parser = subparsers.add_parser("env")
35
+ else:
36
+ parser = argparse.ArgumentParser("Accelerate env command")
37
+
38
+ parser.add_argument(
39
+ "--config_file", default=None, help="The config file to use for the default values in the launching script."
40
+ )
41
+
42
+ if subparsers is not None:
43
+ parser.set_defaults(func=env_command)
44
+ return parser
45
+
46
+
47
+ def env_command(args):
48
+ pt_version = torch.__version__
49
+ pt_cuda_available = torch.cuda.is_available()
50
+ pt_xpu_available = is_xpu_available()
51
+ pt_mlu_available = is_mlu_available()
52
+ pt_sdaa_available = is_sdaa_available()
53
+ pt_musa_available = is_musa_available()
54
+ pt_npu_available = is_npu_available()
55
+
56
+ accelerator = "N/A"
57
+ if pt_cuda_available:
58
+ accelerator = "CUDA"
59
+ elif pt_xpu_available:
60
+ accelerator = "XPU"
61
+ elif pt_mlu_available:
62
+ accelerator = "MLU"
63
+ elif pt_sdaa_available:
64
+ accelerator = "SDAA"
65
+ elif pt_musa_available:
66
+ accelerator = "MUSA"
67
+ elif pt_npu_available:
68
+ accelerator = "NPU"
69
+
70
+ accelerate_config = "Not found"
71
+ # Get the default from the config file.
72
+ if args.config_file is not None or os.path.isfile(default_config_file):
73
+ accelerate_config = load_config_from_file(args.config_file).to_dict()
74
+
75
+ # if we can run which, get it
76
+ command = None
77
+ bash_location = "Not found"
78
+ if os.name == "nt":
79
+ command = ["where", "accelerate"]
80
+ elif os.name == "posix":
81
+ command = ["which", "accelerate"]
82
+ if command is not None:
83
+ bash_location = subprocess.check_output(command, text=True, stderr=subprocess.STDOUT).strip()
84
+ info = {
85
+ "`Accelerate` version": version,
86
+ "Platform": platform.platform(),
87
+ "`accelerate` bash location": bash_location,
88
+ "Python version": platform.python_version(),
89
+ "Numpy version": np.__version__,
90
+ "PyTorch version": f"{pt_version}",
91
+ "PyTorch accelerator": accelerator,
92
+ "System RAM": f"{psutil.virtual_memory().total / 1024**3:.2f} GB",
93
+ }
94
+ if pt_cuda_available:
95
+ info["GPU type"] = torch.cuda.get_device_name()
96
+ elif pt_xpu_available:
97
+ info["XPU type"] = torch.xpu.get_device_name()
98
+ elif pt_mlu_available:
99
+ info["MLU type"] = torch.mlu.get_device_name()
100
+ elif pt_sdaa_available:
101
+ info["SDAA type"] = torch.sdaa.get_device_name()
102
+ elif pt_musa_available:
103
+ info["MUSA type"] = torch.musa.get_device_name()
104
+ elif pt_npu_available:
105
+ info["CANN version"] = torch.version.cann
106
+
107
+ print("\nCopy-and-paste the text below in your GitHub issue\n")
108
+ print("\n".join([f"- {prop}: {val}" for prop, val in info.items()]))
109
+
110
+ print("- `Accelerate` default config:" if args.config_file is None else "- `Accelerate` config passed:")
111
+ accelerate_config_str = (
112
+ "\n".join([f"\t- {prop}: {val}" for prop, val in accelerate_config.items()])
113
+ if isinstance(accelerate_config, dict)
114
+ else f"\t{accelerate_config}"
115
+ )
116
+ print(accelerate_config_str)
117
+
118
+ info["`Accelerate` configs"] = accelerate_config
119
+
120
+ return info
121
+
122
+
123
+ def main() -> int:
124
+ parser = env_command_parser()
125
+ args = parser.parse_args()
126
+ env_command(args)
127
+ return 0
128
+
129
+
130
+ if __name__ == "__main__":
131
+ raise SystemExit(main())
lib/python3.12/site-packages/accelerate/commands/estimate.py ADDED
@@ -0,0 +1,312 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright 2023 The HuggingFace Team. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ import torch
17
+ from huggingface_hub import model_info
18
+ from huggingface_hub.utils import GatedRepoError, RepositoryNotFoundError
19
+
20
+ from accelerate import init_empty_weights
21
+ from accelerate.commands.utils import CustomArgumentParser
22
+ from accelerate.utils import (
23
+ calculate_maximum_sizes,
24
+ convert_bytes,
25
+ is_timm_available,
26
+ is_transformers_available,
27
+ )
28
+
29
+
30
+ if is_transformers_available():
31
+ import transformers
32
+ from transformers import AutoConfig, AutoModel
33
+
34
+ if is_timm_available():
35
+ import timm
36
+
37
+
38
+ def verify_on_hub(repo: str, token: str = None):
39
+ "Verifies that the model is on the hub and returns the model info."
40
+ try:
41
+ return model_info(repo, token=token)
42
+ except (OSError, GatedRepoError):
43
+ return "gated"
44
+ except RepositoryNotFoundError:
45
+ return "repo"
46
+
47
+
48
+ def check_has_model(error):
49
+ """
50
+ Checks what library spawned `error` when a model is not found
51
+ """
52
+ if is_timm_available() and isinstance(error, RuntimeError) and "Unknown model" in error.args[0]:
53
+ return "timm"
54
+ elif (
55
+ is_transformers_available()
56
+ and isinstance(error, OSError)
57
+ and "does not appear to have a file named" in error.args[0]
58
+ ):
59
+ return "transformers"
60
+ else:
61
+ return "unknown"
62
+
63
+
64
+ def create_empty_model(model_name: str, library_name: str, trust_remote_code: bool = False, access_token: str = None):
65
+ """
66
+ Creates an empty model in full precision from its parent library on the `Hub` to calculate the overall memory
67
+ consumption.
68
+
69
+ Args:
70
+ model_name (`str`):
71
+ The model name on the Hub
72
+ library_name (`str`):
73
+ The library the model has an integration with, such as `transformers`. Will be used if `model_name` has no
74
+ metadata on the Hub to determine the library.
75
+ trust_remote_code (`bool`, `optional`, defaults to `False`):
76
+ Whether or not to allow for custom models defined on the Hub in their own modeling files. This option
77
+ should only be set to `True` for repositories you trust and in which you have read the code, as it will
78
+ execute code present on the Hub on your local machine.
79
+ access_token (`str`, `optional`, defaults to `None`):
80
+ The access token to use to access private or gated models on the Hub. (for use on the Gradio app)
81
+
82
+ Returns:
83
+ `torch.nn.Module`: The torch model that has been initialized on the `meta` device.
84
+
85
+ """
86
+ model_info = verify_on_hub(model_name, access_token)
87
+ # Simplified errors
88
+ if model_info == "gated":
89
+ raise GatedRepoError(
90
+ f"Repo for model `{model_name}` is gated. You must be authenticated to access it. Please run `huggingface-cli login`."
91
+ )
92
+ elif model_info == "repo":
93
+ raise RepositoryNotFoundError(
94
+ f"Repo for model `{model_name}` does not exist on the Hub. If you are trying to access a private repo,"
95
+ " make sure you are authenticated via `huggingface-cli login` and have access."
96
+ )
97
+ if library_name is None:
98
+ library_name = getattr(model_info, "library_name", False)
99
+ if not library_name:
100
+ raise ValueError(
101
+ f"Model `{model_name}` does not have any library metadata on the Hub, please manually pass in a `--library_name` to use (such as `transformers`)"
102
+ )
103
+ if library_name == "transformers":
104
+ if not is_transformers_available():
105
+ raise ImportError(
106
+ f"To check `{model_name}`, `transformers` must be installed. Please install it via `pip install transformers`"
107
+ )
108
+ print(f"Loading pretrained config for `{model_name}` from `transformers`...")
109
+ if model_info.config is None:
110
+ raise RuntimeError(f"Tried to load `{model_name}` with `transformers` but it does not have any metadata.")
111
+
112
+ auto_map = model_info.config.get("auto_map", False)
113
+ config = AutoConfig.from_pretrained(model_name, trust_remote_code=trust_remote_code, token=access_token)
114
+ with init_empty_weights():
115
+ # remote code could specify a specific `AutoModel` class in the `auto_map`
116
+ constructor = AutoModel
117
+ if isinstance(auto_map, dict):
118
+ value = None
119
+ for key in auto_map.keys():
120
+ if key.startswith("AutoModelFor"):
121
+ value = key
122
+ break
123
+ if value is not None:
124
+ constructor = getattr(transformers, value)
125
+ # we need to pass the dtype, otherwise it is going to use the torch_dtype that is saved in the config
126
+ model = constructor.from_config(config, torch_dtype=torch.float32, trust_remote_code=trust_remote_code)
127
+ elif library_name == "timm":
128
+ if not is_timm_available():
129
+ raise ImportError(
130
+ f"To check `{model_name}`, `timm` must be installed. Please install it via `pip install timm`"
131
+ )
132
+ print(f"Loading pretrained config for `{model_name}` from `timm`...")
133
+ with init_empty_weights():
134
+ model = timm.create_model(model_name, pretrained=False)
135
+ else:
136
+ raise ValueError(
137
+ f"Library `{library_name}` is not supported yet, please open an issue on GitHub for us to add support."
138
+ )
139
+ return model
140
+
141
+
142
+ def create_ascii_table(headers: list, rows: list, title: str):
143
+ "Creates a pretty table from a list of rows, minimal version of `tabulate`."
144
+ sep_char, in_between = "│", "─"
145
+ column_widths = []
146
+ for i in range(len(headers)):
147
+ column_values = [row[i] for row in rows] + [headers[i]]
148
+ max_column_width = max(len(value) for value in column_values)
149
+ column_widths.append(max_column_width)
150
+
151
+ formats = [f"%{column_widths[i]}s" for i in range(len(rows[0]))]
152
+
153
+ pattern = f"{sep_char}{sep_char.join(formats)}{sep_char}"
154
+ diff = 0
155
+
156
+ def make_row(left_char, middle_char, right_char):
157
+ return f"{left_char}{middle_char.join([in_between * n for n in column_widths])}{in_between * diff}{right_char}"
158
+
159
+ separator = make_row("├", "┼", "┤")
160
+ if len(title) > sum(column_widths):
161
+ diff = abs(len(title) - len(separator))
162
+ column_widths[-1] += diff
163
+
164
+ # Update with diff
165
+ separator = make_row("├", "┼", "┤")
166
+ initial_rows = [
167
+ make_row("┌", in_between, "┐"),
168
+ f"{sep_char}{title.center(len(separator) - 2)}{sep_char}",
169
+ make_row("├", "┬", "┤"),
170
+ ]
171
+ table = "\n".join(initial_rows) + "\n"
172
+ column_widths[-1] += diff
173
+ centered_line = [text.center(column_widths[i]) for i, text in enumerate(headers)]
174
+ table += f"{pattern % tuple(centered_line)}\n{separator}\n"
175
+ for i, line in enumerate(rows):
176
+ centered_line = [t.center(column_widths[i]) for i, t in enumerate(line)]
177
+ table += f"{pattern % tuple(centered_line)}\n"
178
+ table += f"└{'┴'.join([in_between * n for n in column_widths])}┘"
179
+
180
+ return table
181
+
182
+
183
+ def estimate_command_parser(subparsers=None):
184
+ if subparsers is not None:
185
+ parser = subparsers.add_parser("estimate-memory")
186
+ else:
187
+ parser = CustomArgumentParser(description="Model size estimator for fitting a model onto CUDA memory.")
188
+
189
+ parser.add_argument("model_name", type=str, help="The model name on the Hugging Face Hub.")
190
+ parser.add_argument(
191
+ "--library_name",
192
+ type=str,
193
+ help="The library the model has an integration with, such as `transformers`, needed only if this information is not stored on the Hub.",
194
+ choices=["timm", "transformers"],
195
+ )
196
+ parser.add_argument(
197
+ "--dtypes",
198
+ type=str,
199
+ nargs="+",
200
+ default=["float32", "float16", "int8", "int4"],
201
+ help="The dtypes to use for the model, must be one (or many) of `float32`, `float16`, `int8`, and `int4`",
202
+ choices=["float32", "float16", "int8", "int4"],
203
+ )
204
+ parser.add_argument(
205
+ "--trust_remote_code",
206
+ action="store_true",
207
+ help="""Whether or not to allow for custom models defined on the Hub in their own modeling files. This flag
208
+ should only be used for repositories you trust and in which you have read the code, as it will execute
209
+ code present on the Hub on your local machine.""",
210
+ default=False,
211
+ )
212
+
213
+ if subparsers is not None:
214
+ parser.set_defaults(func=estimate_command)
215
+ return parser
216
+
217
+
218
+ def estimate_training_usage(bytes: int, mixed_precision: str, msamp_config: str = None) -> dict:
219
+ """
220
+ Given an amount of `bytes` and `mixed_precision`, calculates how much training memory is needed for a batch size of
221
+ 1.
222
+
223
+ Args:
224
+ bytes (`int`):
225
+ The size of the model being trained.
226
+ mixed_precision (`str`):
227
+ The mixed precision that would be ran.
228
+ msamp_config (`str`):
229
+ The msamp config to estimate the training memory for if `mixed_precision` is set to `"fp8"`.
230
+ """
231
+ memory_sizes = {"model": -1, "optimizer": -1, "gradients": -1, "step": -1}
232
+ fp32_size = bytes
233
+ fp16_size = bytes // 2
234
+
235
+ if mixed_precision == "float32":
236
+ memory_sizes["model"] = fp32_size
237
+ memory_sizes["gradients"] = fp32_size
238
+ memory_sizes["optimizer"] = fp32_size * 2
239
+ memory_sizes["step"] = fp32_size * 4
240
+ elif mixed_precision in ("float16", "bfloat16") or (mixed_precision == "fp8" and msamp_config is None):
241
+ # With native `TransformersEngine`, there is no memory savings with FP8
242
+ # With mixed precision training, the model has weights stored
243
+ # in FP16 and FP32
244
+ memory_sizes["model"] = fp32_size
245
+ # 1.5 from weight gradient + computation (GEMM)
246
+ memory_sizes["gradients"] = fp32_size + fp16_size
247
+ # 2x from optimizer states
248
+ memory_sizes["optimizer"] = fp32_size * 2 # Optimizer states
249
+ memory_sizes["step"] = memory_sizes["optimizer"]
250
+ return memory_sizes
251
+
252
+
253
+ def gather_data(args):
254
+ "Creates an empty model and gathers the data for the sizes"
255
+ try:
256
+ model = create_empty_model(
257
+ args.model_name, library_name=args.library_name, trust_remote_code=args.trust_remote_code
258
+ )
259
+ except (RuntimeError, OSError) as e:
260
+ library = check_has_model(e)
261
+ if library != "unknown":
262
+ raise RuntimeError(
263
+ f"Tried to load `{args.model_name}` with `{library}` but a possible model to load was not found inside the repo."
264
+ )
265
+ raise e
266
+
267
+ total_size, largest_layer = calculate_maximum_sizes(model)
268
+
269
+ data = []
270
+
271
+ for dtype in args.dtypes:
272
+ dtype_total_size = total_size
273
+ dtype_largest_layer = largest_layer[0]
274
+ dtype_training_size = estimate_training_usage(dtype_total_size, dtype)
275
+ if dtype == "float16":
276
+ dtype_total_size /= 2
277
+ dtype_largest_layer /= 2
278
+ elif dtype == "int8":
279
+ dtype_total_size /= 4
280
+ dtype_largest_layer /= 4
281
+ elif dtype == "int4":
282
+ dtype_total_size /= 8
283
+ dtype_largest_layer /= 8
284
+ data.append([dtype, dtype_largest_layer, dtype_total_size, dtype_training_size])
285
+ return data
286
+
287
+
288
+ def estimate_command(args):
289
+ data = gather_data(args)
290
+ for row in data:
291
+ for i, item in enumerate(row):
292
+ if isinstance(item, (int, float)):
293
+ row[i] = convert_bytes(item)
294
+ elif isinstance(item, dict):
295
+ training_usage = max(item.values())
296
+ row[i] = convert_bytes(training_usage) if training_usage != -1 else "N/A"
297
+
298
+ headers = ["dtype", "Largest Layer", "Total Size", "Training using Adam"]
299
+
300
+ title = f"Memory Usage for loading `{args.model_name}`"
301
+ table = create_ascii_table(headers, data, title)
302
+ print(table)
303
+
304
+
305
+ def main():
306
+ parser = estimate_command_parser()
307
+ args = parser.parse_args()
308
+ estimate_command(args)
309
+
310
+
311
+ if __name__ == "__main__":
312
+ main()
lib/python3.12/site-packages/accelerate/commands/launch.py ADDED
@@ -0,0 +1,1208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright 2021 The HuggingFace Team. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ import argparse
18
+ import importlib
19
+ import logging
20
+ import os
21
+ import subprocess
22
+ import sys
23
+ from pathlib import Path
24
+
25
+ import psutil
26
+ import torch
27
+
28
+ from accelerate.commands.config import default_config_file, load_config_from_file
29
+ from accelerate.commands.config.config_args import SageMakerConfig
30
+ from accelerate.commands.config.config_utils import DYNAMO_BACKENDS
31
+ from accelerate.commands.utils import CustomArgumentParser
32
+ from accelerate.state import get_int_from_env
33
+ from accelerate.utils import (
34
+ ComputeEnvironment,
35
+ DistributedType,
36
+ PrepareForLaunch,
37
+ _filter_args,
38
+ check_cuda_p2p_ib_support,
39
+ convert_dict_to_env_variables,
40
+ is_bf16_available,
41
+ is_deepspeed_available,
42
+ is_hpu_available,
43
+ is_mlu_available,
44
+ is_musa_available,
45
+ is_npu_available,
46
+ is_rich_available,
47
+ is_sagemaker_available,
48
+ is_sdaa_available,
49
+ is_torch_xla_available,
50
+ is_xpu_available,
51
+ patch_environment,
52
+ prepare_deepspeed_cmd_env,
53
+ prepare_multi_gpu_env,
54
+ prepare_sagemager_args_inputs,
55
+ prepare_simple_launcher_cmd_env,
56
+ prepare_tpu,
57
+ str_to_bool,
58
+ )
59
+ from accelerate.utils.constants import DEEPSPEED_MULTINODE_LAUNCHERS, TORCH_DYNAMO_MODES
60
+
61
+
62
+ if is_rich_available():
63
+ from rich import get_console
64
+ from rich.logging import RichHandler
65
+
66
+ FORMAT = "%(message)s"
67
+ logging.basicConfig(format=FORMAT, datefmt="[%X]", handlers=[RichHandler()])
68
+
69
+
70
+ logger = logging.getLogger(__name__)
71
+
72
+
73
+ options_to_group = {
74
+ "multi_gpu": "Distributed GPUs",
75
+ "tpu": "TPU",
76
+ "use_deepspeed": "DeepSpeed Arguments",
77
+ "use_fsdp": "FSDP Arguments",
78
+ "use_megatron_lm": "Megatron-LM Arguments",
79
+ "fp8_backend": "FP8 Arguments",
80
+ }
81
+
82
+
83
+ def clean_option(option):
84
+ "Finds all cases of - after the first two characters and changes them to _"
85
+ if "fp8_backend" in option:
86
+ option = "--fp8_backend"
87
+ if option.startswith("--"):
88
+ return option[2:].replace("-", "_")
89
+
90
+
91
+ class CustomHelpFormatter(argparse.HelpFormatter):
92
+ """
93
+ This is a custom help formatter that will hide all arguments that are not used in the command line when the help is
94
+ called. This is useful for the case where the user is using a specific platform and only wants to see the arguments
95
+ for that platform.
96
+ """
97
+
98
+ def __init__(self, *args, **kwargs):
99
+ super().__init__(*args, **kwargs)
100
+ self.titles = [
101
+ "Hardware Selection Arguments",
102
+ "Resource Selection Arguments",
103
+ "Training Paradigm Arguments",
104
+ "positional arguments",
105
+ "optional arguments",
106
+ ]
107
+
108
+ def add_argument(self, action: argparse.Action):
109
+ if "accelerate" in sys.argv[0] and "launch" in sys.argv[1:]:
110
+ args = sys.argv[2:]
111
+ else:
112
+ args = sys.argv[1:]
113
+
114
+ if len(args) > 1:
115
+ args = list(map(clean_option, args))
116
+ used_platforms = [arg for arg in args if arg in options_to_group.keys()]
117
+ used_titles = [options_to_group[o] for o in used_platforms]
118
+ if action.container.title not in self.titles + used_titles:
119
+ action.help = argparse.SUPPRESS
120
+ elif action.container.title == "Hardware Selection Arguments":
121
+ if set(action.option_strings).isdisjoint(set(args)):
122
+ action.help = argparse.SUPPRESS
123
+ else:
124
+ action.help = action.help + " (currently selected)"
125
+ elif action.container.title == "Training Paradigm Arguments":
126
+ if set(action.option_strings).isdisjoint(set(args)):
127
+ action.help = argparse.SUPPRESS
128
+ else:
129
+ action.help = action.help + " (currently selected)"
130
+
131
+ action.option_strings = [s for s in action.option_strings if "-" not in s[2:]]
132
+ super().add_argument(action)
133
+
134
+ def end_section(self):
135
+ if len(self._current_section.items) < 2:
136
+ self._current_section.items = []
137
+ self._current_section.heading = ""
138
+ super().end_section()
139
+
140
+
141
+ def launch_command_parser(subparsers=None):
142
+ description = "Launch a python script in a distributed scenario. Arguments can be passed in with either hyphens (`--num-processes=2`) or underscores (`--num_processes=2`)"
143
+ if subparsers is not None:
144
+ parser = subparsers.add_parser(
145
+ "launch", description=description, add_help=False, allow_abbrev=False, formatter_class=CustomHelpFormatter
146
+ )
147
+ else:
148
+ parser = CustomArgumentParser(
149
+ "Accelerate launch command",
150
+ description=description,
151
+ add_help=False,
152
+ allow_abbrev=False,
153
+ formatter_class=CustomHelpFormatter,
154
+ )
155
+
156
+ parser.add_argument("-h", "--help", action="help", help="Show this help message and exit.")
157
+
158
+ parser.add_argument(
159
+ "--config_file",
160
+ default=None,
161
+ help="The config file to use for the default values in the launching script.",
162
+ )
163
+ parser.add_argument(
164
+ "--quiet",
165
+ "-q",
166
+ action="store_true",
167
+ help="Silence subprocess errors from the launch stack trace and only show the relevant tracebacks. (Only applicable to DeepSpeed and single-process configurations)",
168
+ )
169
+ # Hardware selection arguments
170
+ hardware_args = parser.add_argument_group(
171
+ "Hardware Selection Arguments", "Arguments for selecting the hardware to be used."
172
+ )
173
+ hardware_args.add_argument(
174
+ "--cpu", default=False, action="store_true", help="Whether or not to force the training on the CPU."
175
+ )
176
+ hardware_args.add_argument(
177
+ "--multi_gpu",
178
+ default=False,
179
+ action="store_true",
180
+ help="Whether or not this should launch a distributed GPU training.",
181
+ )
182
+ hardware_args.add_argument(
183
+ "--tpu", default=False, action="store_true", help="Whether or not this should launch a TPU training."
184
+ )
185
+ hardware_args.add_argument(
186
+ "--ipex",
187
+ default=False,
188
+ action="store_true",
189
+ help="Whether or not this should launch a Intel PyTorch Extension (IPEX) training.",
190
+ )
191
+
192
+ # Resource selection arguments
193
+ resource_args = parser.add_argument_group(
194
+ "Resource Selection Arguments", "Arguments for fine-tuning how available hardware should be used."
195
+ )
196
+ resource_args.add_argument(
197
+ "--mixed_precision",
198
+ type=str,
199
+ choices=["no", "fp16", "bf16", "fp8"],
200
+ help="Whether or not to use mixed precision training. "
201
+ "Choose between FP16 and BF16 (bfloat16) training. "
202
+ "BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.",
203
+ )
204
+ resource_args.add_argument(
205
+ "--num_processes", type=int, default=None, help="The total number of processes to be launched in parallel."
206
+ )
207
+ resource_args.add_argument(
208
+ "--num_machines", type=int, default=None, help="The total number of machines used in this training."
209
+ )
210
+ resource_args.add_argument(
211
+ "--num_cpu_threads_per_process",
212
+ type=int,
213
+ default=None,
214
+ help="The number of CPU threads per process. Can be tuned for optimal performance.",
215
+ )
216
+ resource_args.add_argument(
217
+ "--enable_cpu_affinity",
218
+ default=False,
219
+ action="store_true",
220
+ help="Whether or not CPU affinity and balancing should be enabled. Currently only supported on NVIDIA hardware.",
221
+ )
222
+ # Dynamo arguments
223
+ resource_args.add_argument(
224
+ "--dynamo_backend",
225
+ type=str,
226
+ choices=["no"] + [b.lower() for b in DYNAMO_BACKENDS],
227
+ help="Choose a backend to optimize your training with dynamo, see more at "
228
+ "https://github.com/pytorch/torchdynamo.",
229
+ )
230
+ resource_args.add_argument(
231
+ "--dynamo_mode",
232
+ type=str,
233
+ default="default",
234
+ choices=TORCH_DYNAMO_MODES,
235
+ help="Choose a mode to optimize your training with dynamo.",
236
+ )
237
+ resource_args.add_argument(
238
+ "--dynamo_use_fullgraph",
239
+ default=False,
240
+ action="store_true",
241
+ help="Whether to use full graph mode for dynamo or it is ok to break model into several subgraphs",
242
+ )
243
+ resource_args.add_argument(
244
+ "--dynamo_use_dynamic",
245
+ default=False,
246
+ action="store_true",
247
+ help="Whether to enable dynamic shape tracing.",
248
+ )
249
+ resource_args.add_argument(
250
+ "--dynamo_use_regional_compilation",
251
+ default=False,
252
+ action="store_true",
253
+ help="Whether to enable regional compilation.",
254
+ )
255
+
256
+ # Training Paradigm arguments
257
+ paradigm_args = parser.add_argument_group(
258
+ "Training Paradigm Arguments", "Arguments for selecting which training paradigm to be used."
259
+ )
260
+ paradigm_args.add_argument(
261
+ "--use_deepspeed",
262
+ default=False,
263
+ action="store_true",
264
+ help="Whether to use deepspeed.",
265
+ )
266
+ paradigm_args.add_argument(
267
+ "--use_fsdp",
268
+ default=False,
269
+ action="store_true",
270
+ help="Whether to use fsdp.",
271
+ )
272
+ paradigm_args.add_argument(
273
+ "--use_megatron_lm",
274
+ default=False,
275
+ action="store_true",
276
+ help="Whether to use Megatron-LM.",
277
+ )
278
+
279
+ paradigm_args.add_argument(
280
+ "--use_xpu",
281
+ default=None,
282
+ action="store_true",
283
+ help="Whether to use IPEX plugin to speed up training on XPU specifically. This argument is deprecated and ignored, will be removed in Accelerate v1.20.",
284
+ )
285
+
286
+ # distributed GPU training arguments
287
+ distributed_args = parser.add_argument_group("Distributed GPUs", "Arguments related to distributed GPU training.")
288
+ distributed_args.add_argument(
289
+ "--gpu_ids",
290
+ default=None,
291
+ help="What GPUs (by id) should be used for training on this machine as a comma-separated list",
292
+ )
293
+ distributed_args.add_argument(
294
+ "--same_network",
295
+ default=False,
296
+ action="store_true",
297
+ help="Whether all machines used for multinode training exist on the same local network.",
298
+ )
299
+ distributed_args.add_argument(
300
+ "--machine_rank", type=int, default=None, help="The rank of the machine on which this script is launched."
301
+ )
302
+ distributed_args.add_argument(
303
+ "--main_process_ip", type=str, default=None, help="The IP address of the machine of rank 0."
304
+ )
305
+ distributed_args.add_argument(
306
+ "--main_process_port",
307
+ type=int,
308
+ default=None,
309
+ help="The port to use to communicate with the machine of rank 0.",
310
+ )
311
+ distributed_args.add_argument(
312
+ "-t",
313
+ "--tee",
314
+ default="0",
315
+ type=str,
316
+ help="Tee std streams into a log file and also to console.",
317
+ )
318
+ distributed_args.add_argument(
319
+ "--log_dir",
320
+ type=str,
321
+ default=None,
322
+ help=(
323
+ "Base directory to use for log files when using torchrun/torch.distributed.run as launcher. "
324
+ "Use with --tee to redirect std streams info log files."
325
+ ),
326
+ )
327
+ distributed_args.add_argument(
328
+ "--role",
329
+ type=str,
330
+ default="default",
331
+ help="User-defined role for the workers.",
332
+ )
333
+ # Rendezvous related arguments
334
+ distributed_args.add_argument(
335
+ "--rdzv_backend",
336
+ type=str,
337
+ default="static",
338
+ help="The rendezvous method to use, such as 'static' (the default) or 'c10d'",
339
+ )
340
+ distributed_args.add_argument(
341
+ "--rdzv_conf",
342
+ type=str,
343
+ default="",
344
+ help="Additional rendezvous configuration (<key1>=<value1>,<key2>=<value2>,...).",
345
+ )
346
+ distributed_args.add_argument(
347
+ "--max_restarts",
348
+ type=int,
349
+ default=0,
350
+ help="Maximum number of worker group restarts before failing.",
351
+ )
352
+ distributed_args.add_argument(
353
+ "--monitor_interval",
354
+ type=float,
355
+ default=0.1,
356
+ help="Interval, in seconds, to monitor the state of workers.",
357
+ )
358
+ parser.add_argument(
359
+ "-m",
360
+ "--module",
361
+ action="store_true",
362
+ help="Change each process to interpret the launch script as a Python module, executing with the same behavior as 'python -m'.",
363
+ )
364
+ parser.add_argument(
365
+ "--no_python",
366
+ action="store_true",
367
+ help="Skip prepending the training script with 'python' - just execute it directly. Useful when the script is not a Python script.",
368
+ )
369
+
370
+ # TPU arguments
371
+ tpu_args = parser.add_argument_group("TPU", "Arguments related to TPU.")
372
+ tpu_args.add_argument(
373
+ "--tpu_cluster",
374
+ action="store_true",
375
+ dest="tpu_use_cluster",
376
+ help="Whether to use a GCP TPU pod for training.",
377
+ )
378
+ tpu_args.add_argument(
379
+ "--no_tpu_cluster",
380
+ action="store_false",
381
+ dest="tpu_use_cluster",
382
+ help="Should not be passed explicitly, this is for internal use only.",
383
+ )
384
+ tpu_args.add_argument(
385
+ "--tpu_use_sudo",
386
+ action="store_true",
387
+ help="Whether to use `sudo` when running the TPU training script in each pod.",
388
+ )
389
+ tpu_args.add_argument(
390
+ "--vm",
391
+ type=str,
392
+ action="append",
393
+ help=(
394
+ "List of single Compute VM instance names. "
395
+ "If not provided we assume usage of instance groups. For TPU pods."
396
+ ),
397
+ )
398
+ tpu_args.add_argument(
399
+ "--env",
400
+ type=str,
401
+ action="append",
402
+ help="List of environment variables to set on the Compute VM instances. For TPU pods.",
403
+ )
404
+ tpu_args.add_argument(
405
+ "--main_training_function",
406
+ type=str,
407
+ default=None,
408
+ help="The name of the main function to be executed in your script (only for TPU training).",
409
+ )
410
+ tpu_args.add_argument(
411
+ "--downcast_bf16",
412
+ action="store_true",
413
+ help="Whether when using bf16 precision on TPUs if both float and double tensors are cast to bfloat16 or if double tensors remain as float32.",
414
+ )
415
+
416
+ # DeepSpeed arguments
417
+ deepspeed_args = parser.add_argument_group("DeepSpeed Arguments", "Arguments related to DeepSpeed.")
418
+ deepspeed_args.add_argument(
419
+ "--deepspeed_config_file",
420
+ default=None,
421
+ type=str,
422
+ help="DeepSpeed config file.",
423
+ )
424
+ deepspeed_args.add_argument(
425
+ "--zero_stage",
426
+ default=None,
427
+ type=int,
428
+ help="DeepSpeed's ZeRO optimization stage (useful only when `use_deepspeed` flag is passed). "
429
+ "If unspecified, will default to `2`.",
430
+ )
431
+ deepspeed_args.add_argument(
432
+ "--offload_optimizer_device",
433
+ default=None,
434
+ type=str,
435
+ help="Decides where (none|cpu|nvme) to offload optimizer states (useful only when `use_deepspeed` flag is passed). "
436
+ "If unspecified, will default to 'none'.",
437
+ )
438
+ deepspeed_args.add_argument(
439
+ "--offload_param_device",
440
+ default=None,
441
+ type=str,
442
+ help="Decides where (none|cpu|nvme) to offload parameters (useful only when `use_deepspeed` flag is passed). "
443
+ "If unspecified, will default to 'none'.",
444
+ )
445
+ deepspeed_args.add_argument(
446
+ "--offload_optimizer_nvme_path",
447
+ default=None,
448
+ type=str,
449
+ help="Decides Nvme Path to offload optimizer states (useful only when `use_deepspeed` flag is passed). "
450
+ "If unspecified, will default to 'none'.",
451
+ )
452
+ deepspeed_args.add_argument(
453
+ "--offload_param_nvme_path",
454
+ default=None,
455
+ type=str,
456
+ help="Decides Nvme Path to offload parameters (useful only when `use_deepspeed` flag is passed). "
457
+ "If unspecified, will default to 'none'.",
458
+ )
459
+ deepspeed_args.add_argument(
460
+ "--gradient_accumulation_steps",
461
+ default=None,
462
+ type=int,
463
+ help="No of gradient_accumulation_steps used in your training script (useful only when `use_deepspeed` flag is passed). "
464
+ "If unspecified, will default to `1`.",
465
+ )
466
+ deepspeed_args.add_argument(
467
+ "--gradient_clipping",
468
+ default=None,
469
+ type=float,
470
+ help="gradient clipping value used in your training script (useful only when `use_deepspeed` flag is passed). "
471
+ "If unspecified, will default to `1.0`.",
472
+ )
473
+ deepspeed_args.add_argument(
474
+ "--zero3_init_flag",
475
+ default=None,
476
+ type=str,
477
+ help="Decides Whether (true|false) to enable `deepspeed.zero.Init` for constructing massive models. "
478
+ "Only applicable with DeepSpeed ZeRO Stage-3. If unspecified, will default to `true`.",
479
+ )
480
+ deepspeed_args.add_argument(
481
+ "--zero3_save_16bit_model",
482
+ default=None,
483
+ type=str,
484
+ help="Decides Whether (true|false) to save 16-bit model weights when using ZeRO Stage-3. "
485
+ "Only applicable with DeepSpeed ZeRO Stage-3. If unspecified, will default to `false`.",
486
+ )
487
+ deepspeed_args.add_argument(
488
+ "--deepspeed_hostfile",
489
+ default=None,
490
+ type=str,
491
+ help="DeepSpeed hostfile for configuring multi-node compute resources.",
492
+ )
493
+ deepspeed_args.add_argument(
494
+ "--deepspeed_exclusion_filter",
495
+ default=None,
496
+ type=str,
497
+ help="DeepSpeed exclusion filter string when using mutli-node setup.",
498
+ )
499
+ deepspeed_args.add_argument(
500
+ "--deepspeed_inclusion_filter",
501
+ default=None,
502
+ type=str,
503
+ help="DeepSpeed inclusion filter string when using mutli-node setup.",
504
+ )
505
+ deepspeed_args.add_argument(
506
+ "--deepspeed_multinode_launcher",
507
+ default=None,
508
+ type=str,
509
+ help="DeepSpeed multi-node launcher to use, e.g. `pdsh`, `standard`, `openmpi`, `mvapich`, `mpich`, `slurm`, `nossh` (requires DeepSpeed >= 0.14.5). If unspecified, will default to `pdsh`.",
510
+ )
511
+ deepspeed_args.add_argument(
512
+ "--deepspeed_moe_layer_cls_names",
513
+ default=None,
514
+ type=str,
515
+ help="comma-separated list of transformer MoE layer class names (case-sensitive) to wrap ,e.g, `MixtralSparseMoeBlock`, `Qwen2MoeSparseMoeBlock`, `JetMoEAttention,JetMoEBlock` ..."
516
+ " (useful only when `use_deepspeed` flag is passed).",
517
+ )
518
+
519
+ # fsdp arguments
520
+ fsdp_args = parser.add_argument_group("FSDP Arguments", "Arguments related to Fully Shared Data Parallelism.")
521
+ fsdp_args.add_argument(
522
+ "--fsdp_version",
523
+ type=str,
524
+ default="1",
525
+ choices=["1", "2"],
526
+ help="FSDP version to use. (useful only when `use_fsdp` flag is passed).",
527
+ )
528
+ fsdp_args.add_argument(
529
+ "--fsdp_offload_params",
530
+ default="false",
531
+ type=str,
532
+ help="Decides Whether (true|false) to offload parameters and gradients to CPU. (useful only when `use_fsdp` flag is passed).",
533
+ )
534
+ fsdp_args.add_argument(
535
+ "--fsdp_min_num_params",
536
+ type=int,
537
+ default=1e8,
538
+ help="FSDP's minimum number of parameters for Default Auto Wrapping. (useful only when `use_fsdp` flag is passed).",
539
+ )
540
+ # We enable this for backwards compatibility, throw a warning if this is set in `FullyShardedDataParallelPlugin`
541
+ fsdp_args.add_argument(
542
+ "--fsdp_sharding_strategy",
543
+ type=str,
544
+ default="FULL_SHARD",
545
+ help="FSDP's sharding strategy. (useful only when `use_fsdp` flag is passed and `fsdp_version=1`).",
546
+ )
547
+ fsdp_args.add_argument(
548
+ "--fsdp_reshard_after_forward",
549
+ type=str,
550
+ default="true",
551
+ help="FSDP's Reshard After Forward Strategy. (useful only when `use_fsdp` flag is passed). Supports either boolean (FSDP2) or `FULL_SHARD | SHARD_GRAD_OP | NO_RESHARD` (FSDP1).",
552
+ )
553
+ fsdp_args.add_argument(
554
+ "--fsdp_auto_wrap_policy",
555
+ type=str,
556
+ default=None,
557
+ help="FSDP's auto wrap policy. (useful only when `use_fsdp` flag is passed).",
558
+ )
559
+ fsdp_args.add_argument(
560
+ "--fsdp_transformer_layer_cls_to_wrap",
561
+ default=None,
562
+ type=str,
563
+ help="Transformer layer class name (case-sensitive) to wrap ,e.g, `BertLayer`, `GPTJBlock`, `T5Block` .... "
564
+ "(useful only when `use_fsdp` flag is passed).",
565
+ )
566
+ fsdp_args.add_argument(
567
+ "--fsdp_backward_prefetch",
568
+ default=None,
569
+ type=str,
570
+ help="FSDP's backward prefetch policy. (useful only when `use_fsdp` flag is passed).",
571
+ )
572
+ fsdp_args.add_argument(
573
+ "--fsdp_state_dict_type",
574
+ default=None,
575
+ type=str,
576
+ help="FSDP's state dict type. (useful only when `use_fsdp` flag is passed).",
577
+ )
578
+ fsdp_args.add_argument(
579
+ "--fsdp_forward_prefetch",
580
+ default="false",
581
+ type=str,
582
+ help="If True, then FSDP explicitly prefetches the next upcoming "
583
+ "all-gather while executing in the forward pass (useful only when `use_fsdp` flag is passed).",
584
+ )
585
+ fsdp_args.add_argument(
586
+ "--fsdp_use_orig_params",
587
+ default="true",
588
+ type=str,
589
+ help="If True, allows non-uniform `requires_grad` during init, which means support for interspersed frozen and trainable paramteres."
590
+ " (useful only when `use_fsdp` flag is passed).",
591
+ )
592
+ fsdp_args.add_argument(
593
+ "--fsdp_cpu_ram_efficient_loading",
594
+ default="true",
595
+ type=str,
596
+ help="If True, only the first process loads the pretrained model checkoint while all other processes have empty weights. "
597
+ "Only applicable for 🤗 Transformers. When using this, `--fsdp_sync_module_states` needs to True. "
598
+ "(useful only when `use_fsdp` flag is passed).",
599
+ )
600
+ fsdp_args.add_argument(
601
+ "--fsdp_sync_module_states",
602
+ default="true",
603
+ type=str,
604
+ help="If True, each individually wrapped FSDP unit will broadcast module parameters from rank 0."
605
+ " (useful only when `use_fsdp` flag is passed).",
606
+ )
607
+ fsdp_args.add_argument(
608
+ "--fsdp_activation_checkpointing",
609
+ default="false",
610
+ type=str,
611
+ help="Decides Whether (true|false) intermediate activations are freed during the forward pass, and a checkpoint is left as a placeholder. (useful only when `use_fsdp` flag is passed).",
612
+ )
613
+
614
+ # megatron_lm args
615
+ megatron_lm_args = parser.add_argument_group("Megatron-LM Arguments", "Arguments related to Megatron-LM.")
616
+ megatron_lm_args.add_argument(
617
+ "--megatron_lm_tp_degree",
618
+ type=int,
619
+ default=1,
620
+ help="Megatron-LM's Tensor Parallelism (TP) degree. (useful only when `use_megatron_lm` flag is passed).",
621
+ )
622
+ megatron_lm_args.add_argument(
623
+ "--megatron_lm_pp_degree",
624
+ type=int,
625
+ default=1,
626
+ help="Megatron-LM's Pipeline Parallelism (PP) degree. (useful only when `use_megatron_lm` flag is passed).",
627
+ )
628
+ megatron_lm_args.add_argument(
629
+ "--megatron_lm_num_micro_batches",
630
+ type=int,
631
+ default=None,
632
+ help="Megatron-LM's number of micro batches when PP degree > 1. (useful only when `use_megatron_lm` flag is passed).",
633
+ )
634
+ megatron_lm_args.add_argument(
635
+ "--megatron_lm_sequence_parallelism",
636
+ default=None,
637
+ type=str,
638
+ help="Decides Whether (true|false) to enable Sequence Parallelism when TP degree > 1. "
639
+ "(useful only when `use_megatron_lm` flag is passed).",
640
+ )
641
+ megatron_lm_args.add_argument(
642
+ "--megatron_lm_recompute_activations",
643
+ default=None,
644
+ type=str,
645
+ help="Decides Whether (true|false) to enable Selective Activation Recomputation. "
646
+ "(useful only when `use_megatron_lm` flag is passed).",
647
+ )
648
+ megatron_lm_args.add_argument(
649
+ "--megatron_lm_use_distributed_optimizer",
650
+ default=None,
651
+ type=str,
652
+ help="Decides Whether (true|false) to use distributed optimizer "
653
+ "which shards optimizer state and gradients across Data Pralellel (DP) ranks. "
654
+ "(useful only when `use_megatron_lm` flag is passed).",
655
+ )
656
+ megatron_lm_args.add_argument(
657
+ "--megatron_lm_gradient_clipping",
658
+ default=1.0,
659
+ type=float,
660
+ help="Megatron-LM's gradient clipping value based on global L2 Norm (0 to disable). "
661
+ "(useful only when `use_megatron_lm` flag is passed).",
662
+ )
663
+
664
+ # FP8 arguments
665
+ fp8_args = parser.add_argument_group(
666
+ "FP8 Arguments", "Arguments related to FP8 training (requires `--mixed_precision=fp8`)"
667
+ )
668
+ fp8_args.add_argument(
669
+ "--fp8_backend",
670
+ type=str,
671
+ choices=["te", "msamp"],
672
+ help="Choose a backend to train with FP8 (te: TransformerEngine, msamp: MS-AMP)",
673
+ )
674
+ fp8_args.add_argument(
675
+ "--fp8_use_autocast_during_eval",
676
+ default=False,
677
+ action="store_true",
678
+ help="Whether to use FP8 autocast during eval mode (useful only when `--fp8_backend=te` is passed). Generally better metrics are found when this is not passed.",
679
+ )
680
+ fp8_args.add_argument(
681
+ "--fp8_margin",
682
+ type=int,
683
+ default=0,
684
+ help="The margin to use for the gradient scaling (useful only when `--fp8_backend=te` is passed).",
685
+ )
686
+ fp8_args.add_argument(
687
+ "--fp8_interval",
688
+ type=int,
689
+ default=1,
690
+ help="The interval to use for how often the scaling factor is recomputed (useful only when `--fp8_backend=te` is passed).",
691
+ )
692
+ fp8_args.add_argument(
693
+ "--fp8_format",
694
+ type=str,
695
+ default="E4M3",
696
+ choices=["E4M3", "HYBRID"],
697
+ help="The format to use for the FP8 recipe (useful only when `--fp8_backend=te` is passed).",
698
+ )
699
+ fp8_args.add_argument(
700
+ "--fp8_amax_history_len",
701
+ type=int,
702
+ default=1024,
703
+ help="The length of the history to use for the scaling factor computation (useful only when `--fp8_backend=te` is passed).",
704
+ )
705
+ fp8_args.add_argument(
706
+ "--fp8_amax_compute_algo",
707
+ type=str,
708
+ default="most_recent",
709
+ choices=["max", "most_recent"],
710
+ help="The algorithm to use for the scaling factor computation. (useful only when `--fp8_backend=te` is passed).",
711
+ )
712
+ fp8_args.add_argument(
713
+ "--fp8_override_linear_precision",
714
+ type=lambda x: tuple(map(str_to_bool, x.split(","))),
715
+ default=(False, False, False),
716
+ help="Whether or not to execute `fprop`, `dgrad`, and `wgrad` GEMMS in higher precision. Should be passed in a comma-separated string of booleans (useful only when `--fp8_backend=te` is passed).",
717
+ )
718
+ fp8_args.add_argument(
719
+ "--fp8_opt_level",
720
+ type=str,
721
+ default="O2",
722
+ choices=["O1", "O2"],
723
+ help="What level of 8-bit collective communication should be used with MS-AMP (useful only when `--fp8_backend=msamp` is passed).",
724
+ )
725
+
726
+ # AWS arguments
727
+ aws_args = parser.add_argument_group("AWS Arguments", "Arguments related to AWS.")
728
+ aws_args.add_argument(
729
+ "--aws_access_key_id",
730
+ type=str,
731
+ default=None,
732
+ help="The AWS_ACCESS_KEY_ID used to launch the Amazon SageMaker training job",
733
+ )
734
+ aws_args.add_argument(
735
+ "--aws_secret_access_key",
736
+ type=str,
737
+ default=None,
738
+ help="The AWS_SECRET_ACCESS_KEY used to launch the Amazon SageMaker training job.",
739
+ )
740
+ parser.add_argument(
741
+ "--debug",
742
+ action="store_true",
743
+ help="Whether to print out the torch.distributed stack trace when something fails.",
744
+ )
745
+ parser.add_argument(
746
+ "training_script",
747
+ type=str,
748
+ help=(
749
+ "The full path to the script to be launched in parallel, followed by all the arguments for the training "
750
+ "script."
751
+ ),
752
+ )
753
+
754
+ # MPI arguments
755
+ mpirun_args = parser.add_argument_group("MPI Arguments", "Arguments related to mpirun for Multi-CPU")
756
+ mpirun_args.add_argument(
757
+ "--mpirun_hostfile",
758
+ type=str,
759
+ default=None,
760
+ help="Location for a hostfile for using Accelerate to launch a multi-CPU training job with mpirun. This will "
761
+ "get passed to the MPI --hostfile or -f parameter, depending on which MPI program is installed.",
762
+ )
763
+ mpirun_args.add_argument(
764
+ "--mpirun_ccl",
765
+ type=int,
766
+ default=1,
767
+ help="The number of oneCCL worker threads when using Accelerate to launch multi-CPU training with mpirun.",
768
+ )
769
+
770
+ # Other arguments of the training scripts
771
+ parser.add_argument("training_script_args", nargs=argparse.REMAINDER, help="Arguments of the training script.")
772
+
773
+ if subparsers is not None:
774
+ parser.set_defaults(func=launch_command)
775
+ return parser
776
+
777
+
778
+ def simple_launcher(args):
779
+ cmd, current_env = prepare_simple_launcher_cmd_env(args)
780
+
781
+ process = subprocess.Popen(cmd, env=current_env)
782
+ process.wait()
783
+ if process.returncode != 0:
784
+ if not args.quiet:
785
+ raise subprocess.CalledProcessError(returncode=process.returncode, cmd=cmd)
786
+ else:
787
+ sys.exit(1)
788
+
789
+
790
+ def multi_gpu_launcher(args):
791
+ import torch.distributed.run as distrib_run
792
+
793
+ current_env = prepare_multi_gpu_env(args)
794
+ if not check_cuda_p2p_ib_support():
795
+ message = "Using RTX 4000 series which doesn't support faster communication speedups. Ensuring P2P and IB communications are disabled."
796
+ warn = False
797
+ if "NCCL_P2P_DISABLE" not in current_env:
798
+ current_env["NCCL_P2P_DISABLE"] = "1"
799
+ warn = True
800
+ if "NCCL_IB_DISABLE" not in current_env:
801
+ current_env["NCCL_IB_DISABLE"] = "1"
802
+ warn = True
803
+ if warn:
804
+ logger.warning(message)
805
+
806
+ debug = getattr(args, "debug", False)
807
+ args = _filter_args(
808
+ args,
809
+ distrib_run.get_args_parser(),
810
+ ["--training_script", args.training_script, "--training_script_args", args.training_script_args],
811
+ )
812
+
813
+ with patch_environment(**current_env):
814
+ try:
815
+ distrib_run.run(args)
816
+ except Exception:
817
+ if is_rich_available() and debug:
818
+ console = get_console()
819
+ console.print("\n[bold red]Using --debug, `torch.distributed` Stack Trace:[/bold red]")
820
+ console.print_exception(suppress=[__file__], show_locals=False)
821
+ else:
822
+ raise
823
+
824
+
825
+ def deepspeed_launcher(args):
826
+ import torch.distributed.run as distrib_run
827
+
828
+ if not is_deepspeed_available():
829
+ raise ImportError("DeepSpeed is not installed => run `pip3 install deepspeed` or build it from source.")
830
+ else:
831
+ from deepspeed.launcher.runner import DEEPSPEED_ENVIRONMENT_NAME
832
+
833
+ cmd, current_env = prepare_deepspeed_cmd_env(args)
834
+ if not check_cuda_p2p_ib_support():
835
+ message = "Using RTX 4000 series which doesn't support faster communication speedups. Ensuring P2P and IB communications are disabled."
836
+ warn = False
837
+ if "NCCL_P2P_DISABLE" not in current_env:
838
+ current_env["NCCL_P2P_DISABLE"] = "1"
839
+ warn = True
840
+ if "NCCL_IB_DISABLE" not in current_env:
841
+ current_env["NCCL_IB_DISABLE"] = "1"
842
+ warn = True
843
+ if warn:
844
+ logger.warning(message)
845
+
846
+ if args.num_machines > 1 and args.deepspeed_multinode_launcher != DEEPSPEED_MULTINODE_LAUNCHERS[1]:
847
+ with open(DEEPSPEED_ENVIRONMENT_NAME, "a") as f:
848
+ valid_env_items = convert_dict_to_env_variables(current_env)
849
+ if len(valid_env_items) > 1:
850
+ f.writelines(valid_env_items)
851
+
852
+ process = subprocess.Popen(cmd, env=current_env)
853
+ process.wait()
854
+ if process.returncode != 0:
855
+ if not args.quiet:
856
+ raise subprocess.CalledProcessError(returncode=process.returncode, cmd=cmd)
857
+ else:
858
+ sys.exit(1)
859
+ else:
860
+ debug = getattr(args, "debug", False)
861
+ args = _filter_args(
862
+ args,
863
+ distrib_run.get_args_parser(),
864
+ ["--training_script", args.training_script, "--training_script_args", args.training_script_args],
865
+ )
866
+ with patch_environment(**current_env):
867
+ try:
868
+ distrib_run.run(args)
869
+ except Exception:
870
+ if is_rich_available() and debug:
871
+ console = get_console()
872
+ console.print("\n[bold red]Using --debug, `torch.distributed` Stack Trace:[/bold red]")
873
+ console.print_exception(suppress=[__file__], show_locals=False)
874
+ else:
875
+ raise
876
+
877
+
878
+ def tpu_launcher(args):
879
+ import torch_xla.distributed.xla_multiprocessing as xmp
880
+ from torch_xla import device_count
881
+
882
+ if args.no_python:
883
+ raise ValueError("--no_python cannot be used with TPU launcher")
884
+
885
+ args, current_env = prepare_tpu(args, {})
886
+
887
+ if args.module:
888
+ mod_name = args.training_script
889
+ else:
890
+ # Import training_script as a module
891
+ script_path = Path(args.training_script)
892
+ sys.path.append(str(script_path.parent.resolve()))
893
+ mod_name = script_path.stem
894
+
895
+ mod = importlib.import_module(mod_name)
896
+ if not hasattr(mod, args.main_training_function):
897
+ raise ValueError(
898
+ f"Your training script should have a function named {args.main_training_function}, or you should pass a "
899
+ "different value to `--main_training_function`."
900
+ )
901
+ if args.num_processes and args.num_processes != device_count():
902
+ raise ValueError(
903
+ f"Number of processes ({args.num_processes}) must match the number of TPU devices ({device_count()})"
904
+ )
905
+
906
+ # Patch sys.argv
907
+ sys.argv = [mod.__file__] + args.training_script_args
908
+
909
+ main_function = getattr(mod, args.main_training_function)
910
+ with patch_environment(**current_env):
911
+ xmp.spawn(PrepareForLaunch(main_function), args=())
912
+
913
+
914
+ def tpu_pod_launcher(args):
915
+ from torch_xla.distributed import xla_dist
916
+
917
+ current_env = {}
918
+ args, current_env = prepare_tpu(args, current_env, True)
919
+ debug = getattr(args, "debug", False)
920
+
921
+ training_script = args.training_script
922
+ training_script_args = args.training_script_args
923
+ new_args = _filter_args(
924
+ args, xla_dist.get_args_parser(), ["--tpu", args.tpu_name, "--positional", "", "--restart-tpuvm-pod-server"]
925
+ )
926
+
927
+ if args.tpu_use_sudo:
928
+ new_cmd = ["sudo"]
929
+ else:
930
+ new_cmd = []
931
+
932
+ new_cmd += [
933
+ "accelerate-launch",
934
+ "--tpu",
935
+ "--no_tpu_cluster",
936
+ "--num_machines",
937
+ "1",
938
+ "--mixed_precision",
939
+ "no",
940
+ "--dynamo_backend",
941
+ "no",
942
+ "--num_processes",
943
+ str(args.num_processes),
944
+ "--main_training_function",
945
+ str(args.main_training_function),
946
+ training_script,
947
+ ] + training_script_args
948
+
949
+ new_args.positional = new_cmd
950
+ bad_flags = ""
951
+ for arg in vars(new_args):
952
+ if arg.startswith("docker_"):
953
+ value = getattr(new_args, arg)
954
+ if value != "" and value is not None:
955
+ bad_flags += f'{arg}="{value}"\n'
956
+ if bad_flags != "":
957
+ raise ValueError(
958
+ f"Docker containers are not supported for TPU pod launcher currently, please remove the following flags:\n{bad_flags}"
959
+ )
960
+ new_args.env = [f"{k}={v}" for k, v in current_env.items()]
961
+ new_args.env.append("ACCELERATE_IN_TPU_POD=1")
962
+ try:
963
+ xla_dist.resolve_and_execute(new_args)
964
+ except Exception:
965
+ if is_rich_available() and debug:
966
+ console = get_console()
967
+ console.print("\n[bold red]Using --debug, `torch_xla.xla_dist` Stack Trace:[/bold red]")
968
+ console.print_exception(suppress=[__file__], show_locals=False)
969
+ else:
970
+ raise
971
+
972
+
973
+ def sagemaker_launcher(sagemaker_config: SageMakerConfig, args):
974
+ if not is_sagemaker_available():
975
+ raise ImportError(
976
+ "Please install sagemaker to be able to launch training on Amazon SageMaker with `pip install accelerate[sagemaker]`"
977
+ )
978
+ if args.module or args.no_python:
979
+ raise ValueError(
980
+ "SageMaker requires a python training script file and cannot be used with --module or --no_python"
981
+ )
982
+
983
+ from sagemaker.huggingface import HuggingFace
984
+
985
+ args, sagemaker_inputs = prepare_sagemager_args_inputs(sagemaker_config, args)
986
+
987
+ huggingface_estimator = HuggingFace(**args)
988
+
989
+ huggingface_estimator.fit(inputs=sagemaker_inputs)
990
+ print(f"You can find your model data at: {huggingface_estimator.model_data}")
991
+
992
+
993
+ def _validate_launch_command(args):
994
+ # Sanity checks
995
+ if sum([args.multi_gpu, args.cpu, args.tpu, args.use_deepspeed, args.use_fsdp]) > 1:
996
+ raise ValueError(
997
+ "You can only use one of `--cpu`, `--multi_gpu`, `--tpu`, `--use_deepspeed`, `--use_fsdp` at a time."
998
+ )
999
+ if args.multi_gpu and (args.num_processes is not None) and (args.num_processes < 2):
1000
+ raise ValueError("You need to use at least 2 processes to use `--multi_gpu`.")
1001
+
1002
+ defaults = None
1003
+ warned = []
1004
+ mp_from_config_flag = False
1005
+ # Get the default from the config file.
1006
+ if args.config_file is not None or os.path.isfile(default_config_file) and not args.cpu:
1007
+ defaults = load_config_from_file(args.config_file)
1008
+ if (
1009
+ not args.multi_gpu
1010
+ and not args.tpu
1011
+ and not args.tpu_use_cluster
1012
+ and not args.use_deepspeed
1013
+ and not args.use_fsdp
1014
+ and not args.use_megatron_lm
1015
+ ):
1016
+ args.use_deepspeed = defaults.distributed_type == DistributedType.DEEPSPEED
1017
+ args.multi_gpu = (
1018
+ True
1019
+ if defaults.distributed_type
1020
+ in (
1021
+ DistributedType.MULTI_GPU,
1022
+ DistributedType.MULTI_NPU,
1023
+ DistributedType.MULTI_MLU,
1024
+ DistributedType.MULTI_SDAA,
1025
+ DistributedType.MULTI_MUSA,
1026
+ DistributedType.MULTI_XPU,
1027
+ DistributedType.MULTI_HPU,
1028
+ )
1029
+ else False
1030
+ )
1031
+ args.tpu = defaults.distributed_type == DistributedType.XLA
1032
+ args.use_fsdp = defaults.distributed_type == DistributedType.FSDP
1033
+ args.use_megatron_lm = defaults.distributed_type == DistributedType.MEGATRON_LM
1034
+ args.tpu_use_cluster = defaults.tpu_use_cluster if args.tpu else False
1035
+ if args.gpu_ids is None:
1036
+ if defaults.gpu_ids is not None:
1037
+ args.gpu_ids = defaults.gpu_ids
1038
+ else:
1039
+ args.gpu_ids = "all"
1040
+
1041
+ if args.multi_gpu and args.num_machines is None:
1042
+ args.num_machines = defaults.num_machines
1043
+
1044
+ if len(args.gpu_ids.split(",")) < 2 and (args.gpu_ids != "all") and args.multi_gpu and args.num_machines <= 1:
1045
+ raise ValueError(
1046
+ "Less than two GPU ids were configured and tried to run on on multiple GPUs. "
1047
+ "Please ensure at least two are specified for `--gpu_ids`, or use `--gpu_ids='all'`."
1048
+ )
1049
+ if defaults.compute_environment == ComputeEnvironment.LOCAL_MACHINE:
1050
+ # Update args with the defaults
1051
+ for name, attr in defaults.__dict__.items():
1052
+ if isinstance(attr, dict):
1053
+ # Copy defaults.somedict.somearg to args.somearg and
1054
+ # defaults.fsdp_config.x to args.fsdp_x
1055
+ for key, value in attr.items():
1056
+ if name == "fsdp_config" and not key.startswith("fsdp"):
1057
+ key = "fsdp_" + key
1058
+ elif name == "fp8_config" and not key.startswith("fp8"):
1059
+ key = "fp8_" + key
1060
+ if hasattr(args, "nondefault") and key not in args.nondefault:
1061
+ setattr(args, key, value)
1062
+ elif (
1063
+ name not in ["compute_environment", "mixed_precision", "distributed_type"]
1064
+ and getattr(args, name, None) is None
1065
+ ):
1066
+ # Those args are handled separately
1067
+ setattr(args, name, attr)
1068
+ if not args.debug:
1069
+ args.debug = defaults.debug
1070
+
1071
+ if not args.mixed_precision:
1072
+ if defaults.mixed_precision is None:
1073
+ args.mixed_precision = "no"
1074
+ else:
1075
+ args.mixed_precision = defaults.mixed_precision
1076
+ mp_from_config_flag = True
1077
+ else:
1078
+ native_amp = is_bf16_available(True)
1079
+ if (
1080
+ args.mixed_precision == "bf16"
1081
+ and not native_amp
1082
+ and not (args.tpu and is_torch_xla_available(check_is_tpu=True))
1083
+ ):
1084
+ raise ValueError("bf16 mixed precision requires PyTorch >= 1.10 and a supported device.")
1085
+
1086
+ # Silently set the default here
1087
+ if args.dynamo_backend is None:
1088
+ args.dynamo_backend = "no"
1089
+ if args.num_processes == -1:
1090
+ raise ValueError("You need to manually pass in `--num_processes` using this config yaml.")
1091
+ else:
1092
+ if args.num_processes is None:
1093
+ if is_xpu_available():
1094
+ args.num_processes = torch.xpu.device_count()
1095
+ elif is_mlu_available():
1096
+ args.num_processes = torch.mlu.device_count()
1097
+ elif is_sdaa_available():
1098
+ args.num_processes = torch.sdaa.device_count()
1099
+ elif is_musa_available():
1100
+ args.num_processes = torch.musa.device_count()
1101
+ elif is_npu_available():
1102
+ args.num_processes = torch.npu.device_count()
1103
+ elif is_hpu_available():
1104
+ args.num_processes = torch.hpu.device_count()
1105
+ else:
1106
+ args.num_processes = torch.cuda.device_count()
1107
+ warned.append(f"\t`--num_processes` was set to a value of `{args.num_processes}`")
1108
+ if args.debug is None:
1109
+ args.debug = False
1110
+ if (
1111
+ not args.multi_gpu
1112
+ and args.num_processes > 1
1113
+ and (
1114
+ (is_xpu_available() and torch.xpu.device_count() > 1)
1115
+ or (is_npu_available() and torch.npu.device_count() > 1)
1116
+ or (is_hpu_available() and torch.hpu.device_count() > 1)
1117
+ or (is_mlu_available() and torch.mlu.device_count() > 1)
1118
+ or (is_sdaa_available() and torch.sdaa.device_count() > 1)
1119
+ or (is_musa_available() and torch.musa.device_count() > 1)
1120
+ or (torch.cuda.is_available() and torch.cuda.device_count() > 1)
1121
+ )
1122
+ ):
1123
+ warned.append(
1124
+ "\t\tMore than one GPU was found, enabling multi-GPU training.\n"
1125
+ "\t\tIf this was unintended please pass in `--num_processes=1`."
1126
+ )
1127
+ args.multi_gpu = True
1128
+ if args.num_machines is None:
1129
+ warned.append("\t`--num_machines` was set to a value of `1`")
1130
+ args.num_machines = 1
1131
+ if args.mixed_precision is None:
1132
+ warned.append("\t`--mixed_precision` was set to a value of `'no'`")
1133
+ args.mixed_precision = "no"
1134
+ if not hasattr(args, "use_cpu"):
1135
+ args.use_cpu = args.cpu
1136
+ if args.dynamo_backend is None:
1137
+ warned.append("\t`--dynamo_backend` was set to a value of `'no'`")
1138
+ args.dynamo_backend = "no"
1139
+ if args.debug:
1140
+ logger.debug("Running script in debug mode, expect distributed operations to be slightly slower.")
1141
+
1142
+ is_aws_env_disabled = defaults is None or (
1143
+ defaults is not None and defaults.compute_environment != ComputeEnvironment.AMAZON_SAGEMAKER
1144
+ )
1145
+ if is_aws_env_disabled and args.num_cpu_threads_per_process is None:
1146
+ args.num_cpu_threads_per_process = get_int_from_env(["OMP_NUM_THREADS"], 1)
1147
+ if args.use_cpu and args.num_processes >= 1 and get_int_from_env(["OMP_NUM_THREADS"], 0) == 0:
1148
+ local_size = get_int_from_env(
1149
+ ["MPI_LOCALNRANKS", "OMPI_COMM_WORLD_LOCAL_SIZE", "MV2_COMM_WORLD_LOCAL_SIZE"],
1150
+ max(int(args.num_processes / args.num_machines), 1),
1151
+ )
1152
+ threads_per_process = int(psutil.cpu_count(logical=False) / local_size)
1153
+ if threads_per_process > 1:
1154
+ args.num_cpu_threads_per_process = threads_per_process
1155
+ warned.append(
1156
+ f"\t`--num_cpu_threads_per_process` was set to `{args.num_cpu_threads_per_process}` to improve out-of-box performance when training on CPUs"
1157
+ )
1158
+
1159
+ if args.use_xpu is not None:
1160
+ logger.warning(
1161
+ "use_xpu is deprecated and ignored, will be removed in Accelerate v1.20. "
1162
+ "XPU is a PyTorch native citizen now, we don't need extra argument to enable it any more."
1163
+ )
1164
+
1165
+ if any(warned):
1166
+ message = "The following values were not passed to `accelerate launch` and had defaults used instead:\n"
1167
+ message += "\n".join(warned)
1168
+ message += (
1169
+ "\nTo avoid this warning pass in values for each of the problematic parameters or run `accelerate config`."
1170
+ )
1171
+ logger.warning(message)
1172
+ return args, defaults, mp_from_config_flag
1173
+
1174
+
1175
+ def launch_command(args):
1176
+ args, defaults, mp_from_config_flag = _validate_launch_command(args)
1177
+ # Use the proper launcher
1178
+ if args.use_deepspeed and not args.cpu:
1179
+ args.deepspeed_fields_from_accelerate_config = list(defaults.deepspeed_config.keys()) if defaults else []
1180
+ if mp_from_config_flag:
1181
+ args.deepspeed_fields_from_accelerate_config.append("mixed_precision")
1182
+ args.deepspeed_fields_from_accelerate_config = ",".join(args.deepspeed_fields_from_accelerate_config)
1183
+ deepspeed_launcher(args)
1184
+ elif args.use_fsdp and not args.cpu:
1185
+ multi_gpu_launcher(args)
1186
+ elif args.use_megatron_lm and not args.cpu:
1187
+ multi_gpu_launcher(args)
1188
+ elif args.multi_gpu and not args.cpu:
1189
+ multi_gpu_launcher(args)
1190
+ elif args.tpu and not args.cpu:
1191
+ if args.tpu_use_cluster:
1192
+ tpu_pod_launcher(args)
1193
+ else:
1194
+ tpu_launcher(args)
1195
+ elif defaults is not None and defaults.compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
1196
+ sagemaker_launcher(defaults, args)
1197
+ else:
1198
+ simple_launcher(args)
1199
+
1200
+
1201
+ def main():
1202
+ parser = launch_command_parser()
1203
+ args = parser.parse_args()
1204
+ launch_command(args)
1205
+
1206
+
1207
+ if __name__ == "__main__":
1208
+ main()
lib/python3.12/site-packages/accelerate/commands/menu/__init__.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2022 The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ from .selection_menu import BulletMenu
lib/python3.12/site-packages/accelerate/commands/menu/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (257 Bytes). View file
 
lib/python3.12/site-packages/accelerate/commands/menu/__pycache__/cursor.cpython-312.pyc ADDED
Binary file (3.04 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/menu/__pycache__/helpers.cpython-312.pyc ADDED
Binary file (2.18 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/menu/__pycache__/input.cpython-312.pyc ADDED
Binary file (3.13 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/menu/__pycache__/keymap.cpython-312.pyc ADDED
Binary file (4.48 kB). View file
 
lib/python3.12/site-packages/accelerate/commands/menu/__pycache__/selection_menu.cpython-312.pyc ADDED
Binary file (7.37 kB). View file