-
Notifications
You must be signed in to change notification settings - Fork 21
/
compile_multi.sh
executable file
·332 lines (281 loc) · 8.48 KB
/
compile_multi.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/bin/bash
set -ex
models=
mode="int8"
folder="tmp"
num_device=1
device_args=""
quantize_args="--quantize W8BF16"
addr_args=""
dyn_args=""
name=""
num_layers=
out_model=""
share_length=
unshare_length=
hidden_size=
dynamic=0
echo "必须把seq_length长的放在前面,这一点尤其要注意"
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--mode)
mode="$2"
shift 2
;;
--num_device)
num_device="$2"
shift 2
;;
--name)
name="$2"
shift 2
;;
--addr_mode)
addr_mode="$2"
shift 2
;;
--share_length_list)
share_length_list="$2"
shift 2
;;
--unshare_length_list)
unshare_length_list="$2"
shift 2
;;
--seq_length_list)
seq_length_list="$2"
shift 2
;;
--dynamic)
dynamic="$2"
shift 2
;;
--generation_mode)
generation_mode="$2"
shift 2
;;
--embedding_mode)
embedding_mode="$2"
shift 2
;;
*)
echo "Invalid option: $key" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
if [[ -z "$share_length_list" ]]; then
echo "Error: --share_length_list is required." >&2
exit 1
fi
if [[ -z "$seq_length_list" ]]; then
echo "Error: --seq_length_list is required." >&2
exit 1
fi
if [ "$name" = "qwen-7b" ]; then
num_layers=32
hidden_size=4096
echo "Compile Qwen-7B"
else
>&2 echo -e "Error: Invalid name $name, the input name must be \033[31mqwen-7b\033[0m"
exit 1
fi
# Split lists into arrays
IFS=',' read -r -a share_lengths <<< "$share_length_list"
IFS=',' read -r -a unshare_lengths <<< "$unshare_length_list"
IFS=',' read -r -a seq_lengths <<< "$seq_length_list"
# Loop to process different models
for index in "${!share_lengths[@]}"; do
share_length=${share_lengths[$index]}
unshare_length=${unshare_lengths[$index]}
seq_length=${seq_lengths[$index]}
folder_suffix="share${share_length}_unshare${unshare_length}_seq${seq_length}"
folder="tmp_${folder_suffix}"
folder="tmp_share${share_length}_unshare${unshare_length}_seq${seq_length}"
if [ x$mode == x"int8" ]; then
quantize_args="--quantize W8BF16"
elif [ x$mode == x"bf16" ]; then
quantize_args="--quantize BF16"
elif [ x$mode == x"int4" ]; then
quantize_args="--quantize W4BF16 --q_group_size 64"
else
echo "Error, unknown quantize mode"
exit 1
fi
if [ x$num_device != x1 ]; then
device_args="--num_device $num_device"
cur_model=${name}_${mode}_${folder_suffix}_${num_device}dev
else
cur_model=${name}_${mode}_${folder_suffix}_1dev
fi
if [ x$dynamic == x1 ]; then
dyn_args="--dynamic"
cur_model=${name}_${mode}_${folder_suffix}_${num_device}dev_dyn
fi
# out_model=${out_model}${cur_model}_
if [ x$addr_mode == x"io_alone" ]; then
addr_args="--addr_mode io_alone"
fi
outdir=${folder}/$mode"_"$num_device"dev"/block
mkdir -p $outdir
pushd $outdir
# Function to process each block in parallel
process_block() {
i=$1
model_transform.py \
--model_name block_$i \
--model_def ../../onnx/block_$i.onnx \
--mlir block_$i.mlir
model_deploy.py \
--mlir block_$i.mlir \
${quantize_args} \
--quant_input \
--quant_output \
$dyn_args \
--chip bm1684x \
$device_args \
--model block_$i.bmodel
if [ x$unshare_length != x"0" ]; then
model_transform.py \
--model_name block_unshare_$i \
--model_def ../../onnx/block_unshare_$i.onnx \
--mlir block_unshare_$i.mlir
model_deploy.py \
--mlir block_unshare_$i.mlir \
${quantize_args} \
--quant_input \
--quant_output \
--chip bm1684x \
$device_args \
--model block_unshare_$i.bmodel
fi
model_transform.py \
--model_name block_cache_$i \
--model_def ../../onnx/block_cache_$i.onnx \
--mlir block_cache_$i.mlir
model_deploy.py \
--mlir block_cache_$i.mlir \
${quantize_args} \
--quant_input \
--quant_output \
--chip bm1684x \
$device_args \
$addr_args \
--model block_cache_$i.bmodel
}
# Process each block in parallel
for ((i=0; i<$num_layers; i++)); do
process_block $i &
if [ x$unshare_length != x"0" ]; then
models=${models}${outdir}'/block_'$i'.bmodel '$outdir'/block_unshare_'$i'.bmodel '$outdir'/block_cache_'$i'.bmodel '
else
models=${models}${outdir}'/block_'$i'.bmodel '$outdir'/block_cache_'$i'.bmodel '
fi
sleep 45
done
wait # Wait for all background processes to finish
rm -f *.npz
popd
echo $models
if [ x$embedding_mode == x"default" ]; then
outdir=${folder}/$mode"_"$num_device"dev"/embedding
mkdir -p $outdir
pushd $outdir
model_transform.py \
--model_name embedding \
--model_def ../../onnx/embedding.pt \
--input_shapes [[1,${share_length}]] \
--input_types "int32" \
--mlir embedding.mlir
model_deploy.py \
--mlir embedding.mlir \
--quantize BF16 \
--quant_input \
--quant_output \
--chip bm1684x \
$device_args \
--model embedding.bmodel
models=${models}$outdir'/embedding.bmodel '
if [ x$unshare_length != x"0" ]; then
model_transform.py \
--model_name embedding_unshare \
--model_def ../../onnx/embedding.pt \
--input_shapes [[1,${unshare_length}]] \
--input_types "int32" \
--mlir embedding_unshare.mlir
model_deploy.py \
--mlir embedding_unshare.mlir \
--quantize BF16 \
--quant_input \
--quant_output \
--chip bm1684x \
$device_args \
--model embedding_unshare.bmodel
models=${models}$outdir'/embedding_unshare.bmodel '
fi
if [ x$index == x"0" ]; then
model_transform.py \
--model_name embedding_cache \
--model_def ../../onnx/embedding.pt \
--input_shapes [[1,1]] \
--input_types "int32" \
--mlir embedding_cache.mlir
model_deploy.py \
--mlir embedding_cache.mlir \
--quantize BF16 \
--quant_input \
--quant_output \
--chip bm1684x \
$device_args \
--model embedding_cache.bmodel
models=${models}$outdir'/embedding_cache.bmodel '
fi
rm -f *.npz
popd
echo $models
fi
outdir=${folder}/$mode"_"$num_device"dev"/lm_head
mkdir -p $outdir
pushd $outdir
if [ x$index == x"0" ]; then
model_transform.py \
--model_name lm_head \
--model_def ../../onnx/lm_head.pt \
--input_shapes [[1,${hidden_size}]] \
--mlir lm_head.mlir
model_deploy.py \
--mlir lm_head.mlir \
$quantize_args \
--quant_input \
--chip bm1684x \
$device_args \
--model lm_head.bmodel
model_transform.py \
--model_name greedy_head \
--model_def ../../onnx/greedy_head.onnx \
--mlir greedy_head.mlir
model_deploy.py \
--mlir greedy_head.mlir \
--chip bm1684x \
--model greedy_head.bmodel
models=${models}${outdir}'/lm_head.bmodel '$outdir'/greedy_head.bmodel '
fi
model_transform.py \
--model_name penalty_sample_head \
--model_def ../../onnx/penalty_sample_head.onnx \
--mlir penalty_sample_head.mlir
model_deploy.py \
--mlir penalty_sample_head.mlir \
--chip bm1684x \
--model penalty_sample_head.bmodel
rm *.npz *.onnx -f
models=${models}$outdir'/penalty_sample_head.bmodel '
popd
echo $models
done
model_tool --combine $models -o ${name}.bmodel