-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·313 lines (271 loc) · 9.46 KB
/
run.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
# !/usr/bin/env bash
# Author: Chunyu Xue
# Model name (default = bert_16)
model_name="bert_16"
# Max batch size (default = 128)
max_batch_size=128
# Type mode (default = 0, is_training)
type_mode=0
# Plot state (default = 0, is_plot)
plot_state=0
# Bert func, [${1}=batch_size, ${2}=type_mode, ${3}=seq_len]
bert_func(){
if [ ${2} = 0 ];then
# Training
# Cp onnx model flie
if [ ! -e "$(pwd)/custom_python_samples/tensorrt_bert/onnx/bert_${3}.onnx" ];then
echo ""
echo "-----------------------------------------------"
echo "Error:"
echo "-----------------------------------------------"
echo "Target onnx file does not exist, please run .py program to generate it..."
echo "Target .py file path: $(pwd)/custom_python_samples/tensorrt_bert"
echo "Usage: python bert_to_onnx_dynamic_seq.py --seq_len [SEQ_LEN]"
echo "-----------------------------------------------"
echo ""
exit 1
fi
if [ ! -e "$(pwd)/data/bert" ];then
mkdir $(pwd)/data/resnet
fi
cp $(pwd)/custom_python_samples/tensorrt_bert/onnx/bert_${3}.onnx $(pwd)/data/bert/bert_${3}_base_uncased.onnx
if [ ${1} -gt 32 ];then
./bin/trtexec --onnx=$(pwd)/data/bert/bert_${3}_base_uncased.onnx --minShapes=input_ids:1x${3},token_type_ids:1x${3},attention_mask:1x${3} --optShapes=input_ids:32x${3},token_type_ids:32x${3},attention_mask:32x${3} --maxShapes=input_ids:${1}x${3},token_type_ids:${1}x${3},attention_mask:${1}x${3} --workspace=4096 --saveEngine=./engines/bert_${3}_dynamic.trt
else
./bin/trtexec --onnx=$(pwd)/data/bert/bert_${3}_base_uncased.onnx --minShapes=input_ids:1x${3},token_type_ids:1x${3},attention_mask:1x${3} --optShapes=input_ids:${1}x${3},token_type_ids:${1}x${3},attention_mask:${1}x${3} --maxShapes=input_ids:${1}x${3},token_type_ids:${1}x${3},attention_mask:${1}x${3} --workspace=4096 --saveEngine=./engines/bert_${3}_dynamic.trt
fi
exit 1
elif [ ${2} = 1 ];then
# Inference
# -------------------------- Get devices list --------------------------
str_list=$(nvidia-smi -L)
device_count=0
gpu_devices=()
# Split
OLD_IFS="$IFS"
IFS=" "
# Transfer to array
str_list=(${str_list})
IFS="${OLD_IFS}"
# Classify
j="${#str_list[@]}"
for((i=0;i<${j};i++))
do
if [ ${str_list[${i}]} = "(UUID:" ];then
gpu_devices[device_count]=${str_list[${i}+1]//)/}
device_count=$(($device_count+1))
fi
done
# Final clear
j="${#gpu_devices[@]}"
for((i=0;i<${j};i++))
do
if [ ${gpu_devices[${i}]:0:1} != "M" ];then
# Remove all incorrect devices
unset gpu_devices[i]
fi
done
# ----------------------------------------------------------------------
# ----------------------------- Check Log Dir --------------------------
# Check the log dir and recreate it
if [ -e ./log/${model_name} ];then
rm -rf ./log/${model_name}
fi
mkdir ./log/${model_name}
# ----------------------------------------------------------------------
# ----------------------------- Main Func Loop -------------------------
for((i=1;i<(${batch_size}+1);i=i*2))
do
# # Visit all MIG devices, 1-based in shell
j="${#gpu_devices[@]}"
for((k=1;k<${j}+1;k++))
do
CUDA_VISIBLE_DEVICES=${gpu_devices[${k}]} ./bin/trtexec --loadEngine=./engines/bert_${3}_dynamic.trt --iterations=500 --shapes=input_ids:${i}x${3},token_type_ids:${i}x${3},attention_mask:${i}x${3} > ./log/${model_name}/device_${k}_bs_${i}.txt
# sleep 30
done
done
# ----------------------------------------------------------------------
fi
}
# ResNet func, [${1}=batch_size, ${2}=type_mode, ${3}=layer_num]
resnet_func(){
if [ ${2} = 0 ];then
# Training
# Cp onnx model flie
if [ ! -e "$(pwd)/custom_python_samples/tensorrt_resnet/onnx/resnet_${3}.onnx" ];then
echo ""
echo "-----------------------------------------------"
echo "Error:"
echo "-----------------------------------------------"
echo "Target onnx file does not exist, please run .py program to generate it..."
echo "Target .py file path: $(pwd)/custom_python_samples/tensorrt_resnet"
echo "Usage: python main.py --layer_num [LAYER_NUM]"
echo "-----------------------------------------------"
echo ""
exit 1
fi
if [ ! -e "$(pwd)/data/resnet" ];then
mkdir $(pwd)/data/resnet
fi
cp $(pwd)/custom_python_samples/tensorrt_resnet/onnx/resnet_${3}.onnx $(pwd)/data/resnet/resnet_${3}.onnx
if [ ${1} -gt 32 ];then
./bin/trtexec --onnx=$(pwd)/data/resnet/resnet_${3}.onnx --minShapes=input:1x3x224x224 --optShapes=input:32x3x224x224 --maxShapes=input:${1}x3x224x224 --workspace=4096 --saveEngine=./engines/resnet_${3}_dynamic.trt
else
./bin/trtexec --onnx=$(pwd)/data/resnet/resnet_${3}.onnx --minShapes=input:1x3x224x224 --optShapes=input:${1}x3x224x224 --maxShapes=input:${1}x3x224x224 --workspace=4096 --saveEngine=./engines/resnet_${3}_dynamic.trt
fi
exit 1
elif [ ${2} = 1 ];then
# Inference
# -------------------------- Get devices list --------------------------
str_list=$(nvidia-smi -L)
device_count=0
gpu_devices=()
# Split
OLD_IFS="$IFS"
IFS=" "
# Transfer to array
str_list=(${str_list})
IFS="${OLD_IFS}"
# Classify
j="${#str_list[@]}"
for((i=0;i<${j};i++))
do
if [ ${str_list[${i}]} = "(UUID:" ];then
gpu_devices[device_count]=${str_list[${i}+1]//)/}
device_count=$(($device_count+1))
fi
done
# Final clear
j="${#gpu_devices[@]}"
for((i=0;i<${j};i++))
do
if [ ${gpu_devices[${i}]:0:1} != "M" ];then
# Remove all incorrect devices
unset gpu_devices[i]
fi
done
# ----------------------------------------------------------------------
# ----------------------------- Check Log Dir --------------------------
# Check the log dir and recreate it
if [ -e ./log/${model_name} ];then
rm -rf ./log/${model_name}
fi
mkdir ./log/${model_name}
# ----------------------------------------------------------------------
# ----------------------------- Main Func Loop -------------------------
for((i=1;i<(${batch_size}+1);i=i*2))
do
# # Visit all MIG devices, 1-based in shell
j="${#gpu_devices[@]}"
for((k=1;k<${j}+1;k++))
do
CUDA_VISIBLE_DEVICES=${gpu_devices[${k}]} ./bin/trtexec --loadEngine=./engines/resnet_${3}_dynamic.trt --iterations=500 --shapes=input:${i}x3x224x224 > ./log/${model_name}/device_${k}_bs_${i}.txt
# sleep 30
done
done
# ----------------------------------------------------------------------
fi
}
# Help func
help_message(){
echo "-----------------------------------------------"
echo "Usage:"
echo "bash ./run.sh [-m MODEL_NAME] [-b MAX_BATCH_SIZE] [-t TYPE_MODE] [-p] [-h]"
echo "-----------------------------------------------"
echo "Notice:"
echo "- NOTICE: run.sh should be put in the main directory of TensorRT (the same dir level as bin and data)."
echo "-----------------------------------------------"
echo "Description:"
echo "- MODEL_NAME: the name of model to be tested (chosen in [bert_16, bert_64, bert_128, resnet_50, resnet_101, resnet_152], default = bert_16)."
echo "- MAX_BATCH_SIZE: max batch size of the chosen model in inference (pow of 2, default = 128)."
echo "- TYPE_MODE: 0 / 1 (0 for is_training, 1 for is_inference)"
echo "- [-p]: Plot result (For inference stage | Path: './output_figs/', 0 for is_plot, 1 for not_plot)"
echo "- [-h]: help message"
echo "-----------------------------------------------"
}
# Args
while getopts ":m:b:t:p::h::" opt
do
case ${opt} in
m)
model_name=${OPTARG}
;;
b)
batch_size=${OPTARG}
;;
t)
type_mode=${OPTARG}
;;
p)
plot_state=${OPTARG}
;;
h)
echo ""
help_message
echo ""
;;
?)
echo ""
echo "Invalid argument received..."
help_message
echo ""
exit 1;;
esac
done
# Command info
echo ""
echo "-----------------------------------------------"
echo "Info:"
echo "- Model name: ${model_name}"
echo "- Max batch size: ${batch_size} (increased from 1 with the pow of 2)"
echo "- Type mode: ${type_mode} (0 for is_training, 1 for is_inference)"
echo "- Plot result ${plot_state} (For inference stage | Path: './output_figs/', 0 for is_plot, 1 for not_plot)"
echo "-----------------------------------------------"
echo ""
# Error detected
if [ ${batch_size} -gt 128 ] || [ ${batch_size} -lt 1 ];then
echo "Batch size out of range...exit"
exit 1;
fi
# Function
if [ ${model_name} = "bert_16" ];then
# Bert_16
bert_func ${batch_size} ${type_mode} 16
elif [ ${model_name} = "bert_64" ];then
# Bert_64
bert_func ${batch_size} ${type_mode} 64
elif [ ${model_name} = "bert_128" ];then
# Bert_128
bert_func ${batch_size} ${type_mode} 128
elif [ ${model_name} = "resnet_50" ];then
# ResNet_50
resnet_func ${batch_size} ${type_mode} 50
elif [ ${model_name} = "resnet_101" ];then
# ResNet_101
resnet_func ${batch_size} ${type_mode} 101
elif [ ${model_name} = "resnet_152" ];then
# ResNet_152
resnet_func ${batch_size} ${type_mode} 152
else
echo ""
echo "Error:"
echo "-----------------------------------------------"
echo "Wrong network choice (optional: [bert_16, bert_64, bert_128, resnet_50, resnet_101, resnet_152])"
echo "-----------------------------------------------"
echo ""
exit 1
fi
# Format output
echo ""
echo ""
echo "#################################### PERFORMANCE SUMMARY ####################################"
python format_output.py --model_name ${model_name}
# Plot result
if [ ${plot_state} = 0 ];then
echo ""
echo ""
echo "###################################### PLOTTING RESULT ######################################"
python curve_plotter.py --model_name ${model_name}
echo ""
echo "Plot work is completed! Save pics to path: './output_figs/'"
fi