chenglu commited on
Commit
f4637d4
1 Parent(s): 0ccc64c

格式更新

Browse files
Files changed (1) hide show
  1. eat_accelerate_in_30_minites.md +31 -42
eat_accelerate_in_30_minites.md CHANGED
@@ -1,44 +1,38 @@
1
 
2
- # 30分钟吃掉accelerate模型训练加速工具
3
 
4
 
5
- acceleratehuggingface开源的一个方便将pytorch模型迁移到 GPU/multi-GPUs/TPU/fp16/bf16 模式下训练的小巧工具。
 
 
6
 
7
- 和标准的 pytorch 方法相比,使用accelerate 进行多GPU DDP模式/TPU/fp16/bf16 训练你的模型变得非常简单,而且训练速度非常快。
8
 
9
- 官方范例:https://github.com/huggingface/accelerate/tree/main/examples
10
 
11
- 本文将以一个图片分类模型为例,演示在accelerate的帮助下使用pytorch编写一套可以在 cpu/单GPU/多GPU(DDP)模式/TPU 下通用的训练代码。
12
 
13
- 在我们的演示范例中,在kaggle的双GPU环境下,双GPU(DDP)模式是单GPU训练速度的1.6倍,加速效果非常明显。
14
 
 
15
 
 
 
16
 
17
-
18
- DP和DDP的区别
19
-
20
- * DP(DataParallel):实现简单但更慢。只能单机多卡使用。GPU分成server节点和worker节点,有负载不均衡。
21
-
22
- * DDP(DistributedDataParallel):更快但实现麻烦。可单机多卡也可多机多卡。各个GPU是平等的,无负载不均衡。
23
-
24
- 参考文章:《pytorch中的分布式训练之DP VS DDP》https://zhuanlan.zhihu.com/p/356967195
25
 
26
 
27
  ```python
28
- #从git安装最新的accelerate仓库
29
  !pip install git+https://github.com/huggingface/accelerate
30
  ```
31
 
 
32
 
33
 
34
- kaggle 源码:https://www.kaggle.com/code/lyhue1991/kaggle-ddp-tpu-examples
35
-
36
-
37
 
38
- ## 一,使用 CPU/单GPU 训练你的pytorch模型
39
 
40
-
41
- 当系统存在GPU时,accelerate 会自动使用GPU训练你的pytorch模型,否则会使用CPU训练模型。
42
 
43
  ```python
44
  import os,PIL
@@ -162,7 +156,6 @@ def training_loop(epochs = 5,
162
 
163
  #training_loop(epochs = 5,lr = 1e-3,batch_size= 1024,ckpt_path = "checkpoint.pt",
164
  # mixed_precision="no")
165
-
166
  ```
167
 
168
  ```python
@@ -172,25 +165,23 @@ training_loop(epochs = 5,lr = 1e-4,batch_size= 1024,
172
  ```
173
 
174
  ```
175
-
176
  device cuda is used!
177
  epoch【0】@2023-01-15 12:06:45 --> eval_metric= 95.20%
178
  epoch【1】@2023-01-15 12:07:01 --> eval_metric= 96.79%
179
  epoch【2】@2023-01-15 12:07:17 --> eval_metric= 98.47%
180
  epoch【3】@2023-01-15 12:07:34 --> eval_metric= 98.78%
181
  epoch【4】@2023-01-15 12:07:51 --> eval_metric= 98.87%
182
-
183
  ```
184
 
185
 
186
 
187
- ## 二,使用多GPU DDP模式训练你的pytorch模型
188
 
189
 
190
- Kaggle中右边settings 中的 ACCELERATOR选择 GPU T4x2。
191
 
192
 
193
- ### 1,设置config
194
 
195
  ```python
196
  import os
@@ -227,12 +218,12 @@ os._exit(0) # Restart the notebook to reload info from the latest config file
227
 
228
  ```
229
 
230
- ### 2,训练代码
231
 
232
 
233
  与之前代码完全一致。
234
 
235
- 如果是脚本方式启动,需要将训练代码写入到脚本文件中,如cv_example.py
236
 
237
  ```python
238
  %%writefile cv_example.py
@@ -360,10 +351,10 @@ training_loop(epochs = 5,lr = 1e-4,batch_size= 1024,ckpt_path = "checkpoint.pt",
360
 
361
  ```
362
 
363
- ### 3,执行代码
364
 
365
 
366
- **方式1,在notebook中启动**
367
 
368
  ```python
369
  from accelerate import notebook_launcher
@@ -389,8 +380,7 @@ epoch【4】@2023-01-15 12:11:30 --> eval_metric= 98.32%
389
  ```
390
 
391
 
392
-
393
- **方式2,accelerate方式执行脚本**
394
 
395
  ```python
396
  !accelerate launch ./cv_example.py
@@ -406,10 +396,10 @@ epoch【4】@2023-02-03 11:38:43 --> eval_metric= 98.38%
406
  ```
407
 
408
 
409
- **方式3,torch方式执行��本**
410
 
411
  ```python
412
- # or traditional pytorch style
413
  !python -m torch.distributed.launch --nproc_per_node 2 --use_env ./cv_example.py
414
  ```
415
 
@@ -424,13 +414,13 @@ epoch【4】@2023-01-15 12:19:10 --> eval_metric= 98.51%
424
 
425
 
426
 
427
- ## 三,使用TPU加速你的pytorch模型
428
 
429
 
430
- Kaggle中右边settings 中的 ACCELERATOR选择 TPU v3-8。
431
 
432
 
433
- ### 1,安装torch_xla
434
 
435
  ```python
436
  #安装torch_xla支持
@@ -449,7 +439,7 @@ Kaggle中右边settings 中的 ACCELERATOR选择 TPU v3-8。
449
  import torch_xla
450
  ```
451
 
452
- ### 2,训练代码
453
 
454
 
455
  和之前代码完全一样。
@@ -579,7 +569,7 @@ def training_loop(epochs = 5,
579
 
580
  ```
581
 
582
- ### 3,启动训练
583
 
584
  ```python
585
  from accelerate import notebook_launcher
@@ -593,5 +583,4 @@ notebook_launcher(training_loop, args, num_processes=8)
593
 
594
  ```
595
 
596
- 作者介绍:吃货本货。算法工程师,擅长数据挖掘和计算机视觉算法。eat pytorch/tensorflow/pyspark 系列github开源教程的作者。
597
-
 
1
 
2
+ # 30 分钟吃掉 Accelerate 模型训练加速工具
3
 
4
 
5
+ 🤗 Accelerate Hugging Face 开源的一个方便将 PyTorch 模型迁移到 GPU/multi-GPUs/TPU/fp16/bf16 模式下训练的小巧工具。
6
+
7
+ 和标准的 PyTorch 方法相比,使用 Accelerate 进行多 GPU DDP 模式/TPU/fp16/bf16 训练你的模型变得非常简单,而且训练速度非常快。
8
 
9
+ 官方范例:<url>https://github.com/huggingface/accelerate/tree/main/examples</url>
10
 
11
+ 本文将以一个图片分类模型为例,演示在 Accelerate 的帮助下使用 PyTorch 编写一套可以在 CPU、单 GPU、多 GPU (DDP) 模式、TPU 下通用的训练代码。
12
 
13
+ 在我们的演示范例中,在 Kaggle 的双 GPU 环境下,双 GPU (DDP) 模式是单 GPU 训练速度的 1.6 倍,加速效果非常明显。
14
 
 
15
 
16
+ DP 和 DDP 的区别
17
 
18
+ * DP (DataParallel):实现简单但更慢。只能单机多卡使用。GPU 分成 server 节点和 worker 节点,有负载不均衡。
19
+ * DDP (DistributedDataParallel):更快但实现麻烦。可单机多卡也可多机多卡。各个 GPU 是平等的,无负载不均衡。
20
 
21
+ 参考文章:《PyTorch 中的分布式训练之 DP vs. DDP》<url>https://zhuanlan.zhihu.com/p/356967195</url>
 
 
 
 
 
 
 
22
 
23
 
24
  ```python
25
+ #从 git 安装最新的 accelerate 仓库
26
  !pip install git+https://github.com/huggingface/accelerate
27
  ```
28
 
29
+ Kaggle 源码:https://www.kaggle.com/code/lyhue1991/kaggle-ddp-tpu-examples
30
 
31
 
32
+ ## 一、使用 CPU / 单 GPU 训练你的 PyTorch 模型
 
 
33
 
 
34
 
35
+ 当系统存在 GPU 时,Accelerate 会自动使用 GPU 训练你的 PyTorch 模型,否则会使用 CPU 训练模型。
 
36
 
37
  ```python
38
  import os,PIL
 
156
 
157
  #training_loop(epochs = 5,lr = 1e-3,batch_size= 1024,ckpt_path = "checkpoint.pt",
158
  # mixed_precision="no")
 
159
  ```
160
 
161
  ```python
 
165
  ```
166
 
167
  ```
 
168
  device cuda is used!
169
  epoch【0】@2023-01-15 12:06:45 --> eval_metric= 95.20%
170
  epoch【1】@2023-01-15 12:07:01 --> eval_metric= 96.79%
171
  epoch【2】@2023-01-15 12:07:17 --> eval_metric= 98.47%
172
  epoch【3】@2023-01-15 12:07:34 --> eval_metric= 98.78%
173
  epoch【4】@2023-01-15 12:07:51 --> eval_metric= 98.87%
 
174
  ```
175
 
176
 
177
 
178
+ ## 二、使用多 GPU DDP 模式训练你的 PyTorch 模型
179
 
180
 
181
+ Kaggle 中右边 settings 中的 ACCELERATOR 选择 GPU T4x2。
182
 
183
 
184
+ ### 1. 设置 config
185
 
186
  ```python
187
  import os
 
218
 
219
  ```
220
 
221
+ ### 2. 训练代码
222
 
223
 
224
  与之前代码完全一致。
225
 
226
+ 如果是脚本方式启动,需要将训练代码写入到脚本文件中,如 `cv_example.py`
227
 
228
  ```python
229
  %%writefile cv_example.py
 
351
 
352
  ```
353
 
354
+ ### 3. 执行代码
355
 
356
 
357
+ **方式 1: 在 Notebook 中启动**
358
 
359
  ```python
360
  from accelerate import notebook_launcher
 
380
  ```
381
 
382
 
383
+ **方式 2: Accelerate 方式执行脚本**
 
384
 
385
  ```python
386
  !accelerate launch ./cv_example.py
 
396
  ```
397
 
398
 
399
+ **方式 3: PyTorch 方式执行脚本**
400
 
401
  ```python
402
+ # or traditional PyTorch style
403
  !python -m torch.distributed.launch --nproc_per_node 2 --use_env ./cv_example.py
404
  ```
405
 
 
414
 
415
 
416
 
417
+ ## 三、使用 TPU 加速你的 PyTorch 模型
418
 
419
 
420
+ Kaggle 中右边 Settings 中的 ACCELERATOR 选择 TPU v3-8。
421
 
422
 
423
+ ### 1. 安装 `torch_xla`
424
 
425
  ```python
426
  #安装torch_xla支持
 
439
  import torch_xla
440
  ```
441
 
442
+ ### 2. 训练代码
443
 
444
 
445
  和之前代码完全一样。
 
569
 
570
  ```
571
 
572
+ ### 3. 启动训练
573
 
574
  ```python
575
  from accelerate import notebook_launcher
 
583
 
584
  ```
585
 
586
+ 作者介绍:吃货本货。算法工程师,擅长数据挖掘和计算机视觉算法。eat pytorch/tensorflow/pyspark 系列 GitHub 开源教程的作者。