forked from Samsung/ONE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_test.sh
More file actions
executable file
·173 lines (154 loc) · 5.27 KB
/
generate_test.sh
File metadata and controls
executable file
·173 lines (154 loc) · 5.27 KB
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
#!/bin/bash
#
# Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
# Copyright (C) 2017 The Android Open Source Project
#
# 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.
set -Eeuo pipefail
NNAPI_VERSION="
V1_0
V1_1
V1_2
Ex
"
# Process one test spec, and optionally provide the log file argument
# for the slicing tool. The first argument is the test spec file; the
# second optional argument specifies the log file this test should dump
# results into. Only used by the test slicing tool to collect reference
# outputs from the CPU. Also, it outputs the right #includes in the
# test harness so the test would be invoked by TestGenerated.cpp
#
# This function shouldn't be directly called from other scripts. Use
# generate_wrapper below for generating models and examples and updating the
# test framework in one shot.
spec_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
export NNAPI_BASE=$(readlink -f "$spec_dir/../../..")
: ${TEST_DIR:=tests/nnapi}
: ${FORCE:=""}
echo "NNAPI_BASE: ${NNAPI_BASE}"
echo "TEST_DIR: ${TEST_DIR}"
echo "FORCE: ${FORCE}"
function generate_one_testcase {
# Generate one testcase
local LOGFILE=$2
if [ -n "$2" ]; then
local LOGFILE="-l $2"
fi
local BASENAME=`basename -s .mod.py $1`
local MODEL="-m $NNAPI_BASE/$TEST_DIR/src/generated/models/$BASENAME.model.cpp"
local EXAMPLE="-e $NNAPI_BASE/$TEST_DIR/src/generated/examples/$BASENAME.example.cpp"
local TEST="-t $NNAPI_BASE/$TEST_DIR/src/generated/tests/$(basename $1).cpp"
$NNAPI_BASE/$TEST_DIR/nnapi_test_generator/android-10/cts_generator.py $FORCE ./`basename $1` \
$MODEL $EXAMPLE $TEST $LOGFILE
ret=$?
return $ret
}
# Driver for generate_one_testcase. Append the output of generate_one_testcase
# (which are C++ snippets that invokes the test harness) to the
# all_generated_tests.cpp
# Optionally, the "LOG" file ($2), only used by the slicing tool, would be
# passed to generate_one_testcase.
#
# This function should be called to process one test spec from other scripts.
function generate_wrapper {
local LOGFILE=""
if [ $1 = "log" ]; then
local LOGFILE=$2
shift
shift
fi
cd $NNAPI_BASE/$TEST_DIR/specs
FOUND=0
CTSONEFILE=$NNAPI_BASE/$TEST_DIR/for-cts/TestGeneratedOneFile.cpp
echo "// clang-format off" > $CTSONEFILE
echo "// DO NOT EDIT;" >> $CTSONEFILE
echo "// Generated by ml/nn/runtime/test/specs/generate_test.sh" >> $CTSONEFILE
echo "#include \"../GeneratedUtils.cpp\"" >> $CTSONEFILE
for ver in $NNAPI_VERSION;
do
VER_DIR=$NNAPI_BASE/$TEST_DIR/specs/$ver
[ ! -d $VER_DIR ] && continue
pushd $VER_DIR > /dev/null
for f in $@;
do
if [ -f $(basename $f) ]; then
generate_one_testcase $f "$LOGFILE" >> $CTSONEFILE
if [ $? -ne 0 ]; then
echo "Failed processing $f"
return $?
fi
FOUND=1
fi
done
popd > /dev/null
done
if [[ $FOUND -eq 0 ]]; then
echo did not find any files for $@
exit 1
fi
return $?
}
# Process all test spec directory specified by NNAPI_VERSION.
function generate_spec_dirs {
mkdir -vp $NNAPI_BASE/$TEST_DIR/src/generated/models
mkdir -vp $NNAPI_BASE/$TEST_DIR/src/generated/examples
mkdir -vp $NNAPI_BASE/$TEST_DIR/src/generated/tests
cd $NNAPI_BASE/$TEST_DIR/specs
for ver in $NNAPI_VERSION;
do
echo "start to generate ver: ${ver}"
VER_DIR=$NNAPI_BASE/$TEST_DIR/specs/$ver
[ ! -d $VER_DIR ] && continue
pushd $VER_DIR > /dev/null
TARGET_MODEL_DIR="-m $NNAPI_BASE/$TEST_DIR/src/generated/models"
TARGET_EXAMPLE_DIR="-e $NNAPI_BASE/$TEST_DIR/src/generated/examples"
TARGET_TEST_DIR="-t $NNAPI_BASE/$TEST_DIR/src/generated/tests"
$NNAPI_BASE/$TEST_DIR/nnapi_test_generator/android-10/cts_generator.py $FORCE $VER_DIR \
$TARGET_MODEL_DIR $TARGET_EXAMPLE_DIR $TARGET_TEST_DIR
if [ $? -ne 0 ]; then
echo "Failed processing $VER_DIR"
return $?
fi
# Workaround for cmake
OUTFILE="$NNAPI_BASE/$TEST_DIR/src/generated/all_generated_${ver}_cts_tests.cpp"
echo "Generating $OUTFILE"
echo "// Generated by tests/nnapi/specs/generate_test.sh" > "$OUTFILE"
echo "// DO NOT EDIT" >>"$OUTFILE"
echo "// clang-format off" >>"$OUTFILE"
for spec in "$NNAPI_BASE/$TEST_DIR/specs/${ver}/"*.mod.py; do
BASENAME="$(basename "$spec")"
echo "#include "\""generated/tests/${BASENAME}.cpp"\" >>"$OUTFILE"
done
popd > /dev/null
done
return $?
}
# Only run the following when not sourced by another script
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
set -eu
if [ $# -gt 0 ]; then
if [ $1 = "-f" ] || [ $1 = "--force" ]; then
FORCE="-f"
shift
fi
fi
if [ $# -eq 0 ]; then
generate_spec_dirs $FORCE
else
FILES="$@"
generate_wrapper $FILES
fi
if [ $? -ne 0 ]; then
exit $?
fi
fi # [[ "${BASH_SOURCE[0]}" == "${0}" ]]