-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathaccuracy.sh
executable file
·137 lines (120 loc) · 4.52 KB
/
accuracy.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
#!/usr/bin/env bash
#
# Copyright (c) 2021 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
MODEL_DIR=${MODEL_DIR-$PWD}
echo 'MODEL_DIR='$MODEL_DIR
echo 'OUTPUT_DIR='$OUTPUT_DIR
echo 'DATASET_DIR='$DATASET_DIR
if [ -z "${OUTPUT_DIR}" ]; then
echo "The required environment variable OUTPUT_DIR has not been set"
exit 1
fi
# Create the output directory in case it doesn't already exist
mkdir -p ${OUTPUT_DIR}
if [ -z "${PRECISION}" ]; then
echo "The required environment variable PRECISION has not been set"
echo "Please set PRECISION to int8, fp32, bfloat32 or bfloat16."
exit 1
elif [ ${PRECISION} != "int8" ] && [ ${PRECISION} != "fp32" ] && [ ${PRECISION} != "bfloat16" ] && [ ${PRECISION} != "bfloat32" ]; then
echo "The specified precision '${PRECISION}' is unsupported."
echo "Supported precisions are: int8, fp32, bfloat32 and bfloat16"
exit 1
fi
if [[ -z "${CHECKPOINT_DIR}" ]]; then
# Unzip the squad checkpoint files
pretrained_model_dir="pretrained_model/bert_large_checkpoints"
if [ ! -d "${pretrained_model_dir}" ]; then
unzip pretrained_model/bert_large_checkpoints.zip -d pretrained_model
fi
CHECKPOINT_DIR="${MODEL_DIR}/${pretrained_model_dir}"
fi
# Create an array of input directories that are expected and then verify that they exist
declare -A input_dirs
input_dirs[CHECKPOINT_DIR]=${CHECKPOINT_DIR}
input_dirs[DATASET_DIR]=${DATASET_DIR}
for i in "${!input_dirs[@]}"; do
var_name=$i
dir_path=${input_dirs[$i]}
if [[ -z $dir_path ]]; then
echo "The required environment variable $var_name is empty" >&2
exit 1
fi
if [[ ! -d $dir_path ]]; then
echo "The $var_name path '$dir_path' does not exist" >&2
exit 1
fi
done
if [ -z "${PRETRAINED_MODEL}" ]; then
if [[ $PRECISION == "int8" ]]; then
PRETRAINED_MODEL="${MODEL_DIR}/pretrained_model/bert_large_int8_pretrained_model.pb"
elif [[ $PRECISION == "bfloat16" ]]; then
PRETRAINED_MODEL="${MODEL_DIR}/pretrained_model/bert_large_bfloat16_pretrained_model.pb"
elif [[ $PRECISION == "fp32" ]] || [[ $PRECISION == "bfloat32" ]]; then
PRETRAINED_MODEL="${MODEL_DIR}/pretrained_model/bert_large_fp32_pretrained_model.pb"
else
echo "The specified precision '${PRECISION}' is unsupported."
echo "Supported precisions are: fp32, bfloat16, bfloat32 and int8"
exit 1
fi
if [[ ! -f "${PRETRAINED_MODEL}" ]]; then
echo "The pretrained model could not be found. Please set the PRETRAINED_MODEL env var to point to the frozen graph file."
exit 1
fi
elif [[ ! -f "${PRETRAINED_MODEL}" ]]; then
echo "The file specified by the PRETRAINED_MODEL environment variable (${PRETRAINED_MODEL}) does not exist."
exit 1
fi
MODE="inference"
# If batch size env is not mentioned, then the workload will run with the default batch size.
if [ -z "${BATCH_SIZE}"]; then
if [[ $PRECISION == "int8" ]]; then
BATCH_SIZE="16"
elif [[ $PRECISION == "bfloat16" ]]; then
BATCH_SIZE="32"
elif [[ $PRECISION == "fp32" ]] || [[ $PRECISION == "bfloat32" ]]; then
BATCH_SIZE="56"
fi
echo "Running with default batch size of ${BATCH_SIZE}"
fi
# Set up env variable for bfloat32
if [[ $PRECISION == "bfloat32" ]]; then
export ONEDNN_DEFAULT_FPMATH_MODE=BF16
PRECISION="fp32"
fi
source "${MODEL_DIR}/quickstart/common/utils.sh"
_ht_status_spr
_command python ${MODEL_DIR}/benchmarks/launch_benchmark.py \
--model-name=bert_large \
--precision ${PRECISION} \
--mode=${MODE} \
--framework=tensorflow \
--batch-size ${BATCH_SIZE} \
--in-graph ${PRETRAINED_MODEL} \
--data-location=${DATASET_DIR} \
--output-dir ${OUTPUT_DIR} \
--checkpoint ${CHECKPOINT_DIR} \
--accuracy-only \
$@ \
-- DEBIAN_FRONTEND=noninteractive \
init_checkpoint=model.ckpt-3649 infer_option=SQuAD \
experimental-gelu=True 2>&1 | tee ${OUTPUT_DIR}/bert_large_${PRECISION}_inference_bs${BATCH_SIZE}_accuracy.log
if [[ $? == 0 ]]; then
echo "Accuracy:"
cat ${OUTPUT_DIR}/bert_large_${PRECISION}_inference_bs${BATCH_SIZE}_accuracy.log | grep -ie "exact_match.*f1" | tail -n 1
exit 0
else
exit 1
fi