yuccaaa commited on
Commit
f5bd4a2
·
verified ·
1 Parent(s): 56901b3

Upload ms-swift/docs/source/BestPractices/NPU支持.md with huggingface_hub

Browse files
ms-swift/docs/source/BestPractices/NPU支持.md ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # NPU支持
2
+ 作者: [chuanzhubin](https://github.com/chuanzhubin)
3
+
4
+ ## 环境准备
5
+
6
+ 实验环境:8 * 昇腾910B3 64G (设备由[@chuanzhubin](https://github.com/chuanzhubin)提供, 感谢对modelscope和swift的支持~)
7
+
8
+ ```shell
9
+ # 创建新的conda虚拟环境(可选)
10
+ conda create -n swift-npu python=3.10 -y
11
+ conda activate swift-npu
12
+
13
+ # 设置pip全局镜像 (可选,加速下载)
14
+ pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
15
+ pip install ms-swift -U
16
+
17
+ # 安装torch-npu
18
+ pip install torch-npu decorator
19
+ # 如果你想要使用deepspeed (控制显存占用,训练速度会有一定下降)
20
+ pip install deepspeed
21
+ ```
22
+
23
+ 测试环境是否安装正确,NPU能否被正常加载:
24
+ ```python
25
+ from transformers.utils import is_torch_npu_available
26
+ import torch
27
+
28
+ print(is_torch_npu_available()) # True
29
+ print(torch.npu.device_count()) # 8
30
+ print(torch.randn(10, device='npu:0'))
31
+ ```
32
+
33
+ 查看NPU的P2P连接,这里看到每个NPU都通过7条HCCS与其他NPU互联
34
+ ```shell
35
+ (valle) root@valle:~/src# npu-smi info -t topo
36
+ NPU0 NPU1 NPU2 NPU3 NPU4 NPU5 NPU6 NPU7 CPU Affinity
37
+ NPU0 X HCCS HCCS HCCS HCCS HCCS HCCS HCCS 144-167
38
+ NPU1 HCCS X HCCS HCCS HCCS HCCS HCCS HCCS 144-167
39
+ NPU2 HCCS HCCS X HCCS HCCS HCCS HCCS HCCS 96-119
40
+ NPU3 HCCS HCCS HCCS X HCCS HCCS HCCS HCCS 96-119
41
+ NPU4 HCCS HCCS HCCS HCCS X HCCS HCCS HCCS 0-23
42
+ NPU5 HCCS HCCS HCCS HCCS HCCS X HCCS HCCS 0-23
43
+ NPU6 HCCS HCCS HCCS HCCS HCCS HCCS X HCCS 48-71
44
+ NPU7 HCCS HCCS HCCS HCCS HCCS HCCS HCCS X 48-71
45
+
46
+ Legend:
47
+
48
+ X = Self
49
+ SYS = Path traversing PCIe and NUMA nodes. Nodes are connected through SMP, such as QPI, UPI.
50
+ PHB = Path traversing PCIe and the PCIe host bridge of a CPU.
51
+ PIX = Path traversing a single PCIe switch
52
+ PXB = Path traversing multiple PCIe switches
53
+ HCCS = Connection traversing HCCS.
54
+ NA = Unknown relationship.
55
+ ```
56
+
57
+ 查看NPU状态, npu-smi命令详解可以查看[官方文档](https://support.huawei.com/enterprise/zh/doc/EDOC1100079287/10dcd668)
58
+ ```shell
59
+ (valle) root@valle:~/src# npu-smi info
60
+ +------------------------------------------------------------------------------------------------+
61
+ | npu-smi 24.1.rc1.b030 Version: 24.1.rc1.b030 |
62
+ +---------------------------+---------------+----------------------------------------------------+
63
+ | NPU Name | Health | Power(W) Temp(C) Hugepages-Usage(page)|
64
+ | Chip | Bus-Id | AICore(%) Memory-Usage(MB) HBM-Usage(MB) |
65
+ +===========================+===============+====================================================+
66
+ | 0 910B3 | OK | 101.8 43 0 / 0 |
67
+ | 0 | 0000:C1:00.0 | 0 0 / 0 3318 / 65536 |
68
+ +===========================+===============+====================================================+
69
+ | 1 910B3 | OK | 92.0 39 0 / 0 |
70
+ | 0 | 0000:C2:00.0 | 0 0 / 0 3314 / 65536 |
71
+ +===========================+===============+====================================================+
72
+ | 2 910B3 | OK | 102.0 40 0 / 0 |
73
+ | 0 | 0000:81:00.0 | 0 0 / 0 3314 / 65536 |
74
+ +===========================+===============+====================================================+
75
+ | 3 910B3 | OK | 99.8 40 0 / 0 |
76
+ | 0 | 0000:82:00.0 | 0 0 / 0 3314 / 65536 |
77
+ +===========================+===============+====================================================+
78
+ | 4 910B3 | OK | 98.6 45 0 / 0 |
79
+ | 0 | 0000:01:00.0 | 0 0 / 0 3314 / 65536 |
80
+ +===========================+===============+====================================================+
81
+ | 5 910B3 | OK | 99.7 44 0 / 0 |
82
+ | 0 | 0000:02:00.0 | 0 0 / 0 3314 / 65536 |
83
+ +===========================+===============+====================================================+
84
+ | 6 910B3 | OK | 103.8 45 0 / 0 |
85
+ | 0 | 0000:41:00.0 | 0 0 / 0 3314 / 65536 |
86
+ +===========================+===============+====================================================+
87
+ | 7 910B3 | OK | 98.2 44 0 / 0 |
88
+ | 0 | 0000:42:00.0 | 0 0 / 0 3315 / 65536 |
89
+ +===========================+===============+====================================================+
90
+ ```
91
+
92
+ ## 微调
93
+ 以下介绍LoRA的微调, 全参数微调设置参数`--train_type full`即可.
94
+
95
+ | 模型大小 | NPU数量 | deepspeed类型 | 最大显存占用量 |
96
+ |------|-------|-------------|-----------|
97
+ | 7B | 1 | None | 1 * 28 GB |
98
+ | 7B | 4 | None | 4 * 22 GB |
99
+ | 7B | 4 | zero2 | 4 * 28 GB |
100
+ | 7B | 4 | zero3 | 4 * 22 GB |
101
+ | 7B | 8 | None | 8 * 22 GB |
102
+ | 14B | 1 | None | 1 * 45 GB |
103
+ | 14B | 8 | None | 8 * 51 GB |
104
+ | 14B | 8 | zero2 | 8 * 49 GB |
105
+ | 14B | 8 | zero3 | 8 * 31 GB |
106
+
107
+ ### 单卡训练
108
+
109
+ 通过如下命令启动单卡微调: (注意: 如果微调期间出现nan的情况, 请设置`--torch_dtype float32`.)
110
+
111
+ ```shell
112
+ # 实验环境: 昇腾910B3
113
+ # 显存需求: 28 GB
114
+ # 运行时长: 8小时
115
+ ASCEND_RT_VISIBLE_DEVICES=0 \
116
+ swift sft \
117
+ --model Qwen/Qwen2-7B-Instruct \
118
+ --dataset AI-ModelScope/blossom-math-v2 \
119
+ --num_train_epochs 5 \
120
+ --train_type lora \
121
+ --output_dir output \
122
+ --learning_rate 1e-4 \
123
+ --gradient_accumulation_steps 16 \
124
+ --save_steps 100 \
125
+ --eval_steps 100
126
+
127
+ ```
128
+
129
+
130
+ ### 数据并行训练
131
+ 我们使用其中的4卡进行ddp训练
132
+
133
+ ```shell
134
+ # 实验环境: 4 * 昇腾910B3
135
+ # 显存需求: 4 * 22 GB
136
+ # 运行时长: 2小时
137
+ NPROC_PER_NODE=4 \
138
+ ASCEND_RT_VISIBLE_DEVICES=0,1,2,3 \
139
+ swift sft \
140
+ --model Qwen/Qwen2-7B-Instruct \
141
+ --dataset AI-ModelScope/blossom-math-v2 \
142
+ --num_train_epochs 5 \
143
+ --train_type lora \
144
+ --output_dir output \
145
+ ...
146
+ ```
147
+
148
+
149
+ ### Deepspeed训练
150
+
151
+ ZeRO2:
152
+ ```shell
153
+ # 实验环境: 4 * 昇腾910B3
154
+ # 显存需求: 4 * 28GB
155
+ # 运行时长: 3.5小时
156
+ NPROC_PER_NODE=4 \
157
+ ASCEND_RT_VISIBLE_DEVICES=0,1,2,3 \
158
+ swift sft \
159
+ --model Qwen/Qwen2-7B-Instruct \
160
+ --dataset AI-ModelScope/blossom-math-v2 \
161
+ --num_train_epochs 5 \
162
+ --train_type lora \
163
+ --output_dir output \
164
+ --deepspeed zero2 \
165
+ ...
166
+ ```
167
+
168
+ ZeRO3:
169
+ ```shell
170
+ # 实验环境: 4 * 昇腾910B3
171
+ # 显存需求: 4 * 22 GB
172
+ # 运行时长: 8.5小时
173
+ NPROC_PER_NODE=4 \
174
+ ASCEND_RT_VISIBLE_DEVICES=0,1,2,3 \
175
+ swift sft \
176
+ --model Qwen/Qwen2-7B-Instruct \
177
+ --dataset AI-ModelScope/blossom-math-v2 \
178
+ --num_train_epochs 5 \
179
+ --train_type lora \
180
+ --output_dir output \
181
+ --deepspeed zero3 \
182
+ ...
183
+ ```
184
+
185
+
186
+ ## 推理
187
+
188
+ 原始模型:
189
+ ```shell
190
+ ASCEND_RT_VISIBLE_DEVICES=0 swift infer \
191
+ --model Qwen/Qwen2-7B-Instruct \
192
+ --stream true --max_new_tokens 2048
193
+ ```
194
+
195
+ LoRA微调后:
196
+ ```shell
197
+ ASCEND_RT_VISIBLE_DEVICES=0 swift infer \
198
+ --adapters xxx/checkpoint-xxx --load_data_args true \
199
+ --stream true --max_new_tokens 2048
200
+
201
+ # merge-lora并推理
202
+ ASCEND_RT_VISIBLE_DEVICES=0 swift export --adapters xx/checkpoint-xxx --merge_lora true
203
+
204
+ ASCEND_RT_VISIBLE_DEVICES=0 swift infer \
205
+ --model xxx/checkpoint-xxx-merged --load_data_args true \
206
+ --stream true --max_new_tokens 2048
207
+ ```
208
+
209
+
210
+ ## 部署
211
+ NPU不支持使用vllm进行推理/部署加速, 但是可以使用原生pytorch进行部署.
212
+
213
+ 原始模型:
214
+ ```shell
215
+ ASCEND_RT_VISIBLE_DEVICES=0 swift deploy --model Qwen/Qwen2-7B-Instruct --max_new_tokens 2048
216
+ ```
217
+
218
+ LoRA微调后:
219
+ ```shell
220
+ ASCEND_RT_VISIBLE_DEVICES=0 swift deploy --adapters xxx/checkpoint-xxx --max_new_tokens 2048
221
+
222
+ # merge-lora并推理
223
+ ASCEND_RT_VISIBLE_DEVICES=0 swift export --adapters xx/checkpoint-xxx --merge_lora true
224
+ ASCEND_RT_VISIBLE_DEVICES=0 swift deploy --model xxx/checkpoint-xxx-merged --max_new_tokens 2048
225
+ ```