-
Notifications
You must be signed in to change notification settings - Fork 9.6k
/
run_cpp_examples.sh
183 lines (167 loc) · 4.02 KB
/
run_cpp_examples.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
#!/usr/bin/env bash
# This script runs through the code in each of the cpp examples.
# The purpose is just as an integration test, not to actually train models in any meaningful way.
# Optionally specify a comma separated list of examples to run.
# can be run as:
# ./run_cpp_examples.sh "get_libtorch,run_all,clean"
# To get libtorch, run all examples, and remove temporary/changed data files.
BASE_DIR=`pwd`"/"`dirname $0`
echo "BASE_DIR: $BASE_DIR"
EXAMPLES=`echo $1 | sed -e 's/ //g'`
HOME_DIR=$HOME
ERRORS=""
function error() {
ERR=$1
ERRORS="$ERRORS\n$ERR"
echo $ERR
}
function get_libtorch() {
echo "Getting libtorch"
cd $HOME_DIR
if [ ! -d "libtorch" ]; then
wget https://download.pytorch.org/libtorch/nightly/cpu/libtorch-cxx11-abi-shared-with-deps-latest.zip
unzip libtorch-cxx11-abi-shared-with-deps-latest.zip
fi
if [ $? -eq 0 ]; then
echo "Successfully downloaded and extracted libtorch"
LIBTORCH_PATH="$HOME_DIR/libtorch" # Store the LibTorch path in a variable.
echo "LibTorch path: $LIBTORCH_PATH" # Print the LibTorch path
else
error "Failed to download or extract LibTorch"
fi
}
function start() {
EXAMPLE=${FUNCNAME[1]}
cd $BASE_DIR/cpp/$EXAMPLE
echo "Running example: $EXAMPLE"
}
function check_run_success() {
if [ $? -eq 0 ]; then
echo "Successfully ran $1"
else
echo "Failed to run $1"
error "Failed to run $1"
exit 1
fi
}
function autograd() {
start
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=$LIBTORCH_PATH ..
make
if [ $? -eq 0 ]; then
echo "Successfully built $EXAMPLE"
./$EXAMPLE # Run the executable
check_run_success $EXAMPLE
else
error "Failed to build $EXAMPLE"
exit 1
fi
}
function custom-dataset() {
start
# Download the dataset and unzip it
if [ ! -d "$BASE_DIR/cpp/$EXAMPLE/dataset" ]; then
wget https://data.caltech.edu/records/mzrjq-6wc02/files/caltech-101.zip
unzip caltech-101.zip
cd caltech-101
tar -xzf 101_ObjectCategories.tar.gz
mv 101_ObjectCategories $BASE_DIR/cpp/$EXAMPLE/dataset
fi
# build the executable and run it
cd $BASE_DIR/cpp/$EXAMPLE
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=$LIBTORCH_PATH ..
make
if [ $? -eq 0 ]; then
echo "Successfully built $EXAMPLE"
cd $BASE_DIR/cpp/$EXAMPLE
./build/$EXAMPLE # Run the executable
check_run_success $EXAMPLE
else
error "Failed to build $EXAMPLE"
exit 1
fi
}
function dcgan() {
start
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=$LIBTORCH_PATH ..
make
if [ $? -eq 0 ]; then
echo "Successfully built $EXAMPLE"
./$EXAMPLE --epochs 5 # Run the executable with kNumberOfEpochs = 5
check_run_success $EXAMPLE
else
error "Failed to build $EXAMPLE"
exit 1
fi
}
function mnist() {
start
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=$LIBTORCH_PATH ..
make
if [ $? -eq 0 ]; then
echo "Successfully built $EXAMPLE"
./$EXAMPLE # Run the executable
check_run_success $EXAMPLE
else
error "Failed to build $EXAMPLE"
exit 1
fi
}
function regression() {
start
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=$LIBTORCH_PATH ..
make
if [ $? -eq 0 ]; then
echo "Successfully built $EXAMPLE"
./$EXAMPLE # Run the executable
check_run_success $EXAMPLE
else
error "Failed to build $EXAMPLE"
exit 1
fi
}
function clean() {
cd $BASE_DIR
echo "Running clean to remove cruft"
# Remove the build directories
find . -type d -name 'build' -exec rm -rf {} +
# Remove the libtorch directory
rm -rf $HOME_DIR/libtorch
rm -f $HOME_DIR/libtorch-shared-with-deps-latest.zip
echo "Clean completed"
}
function run_all() {
autograd
custom-dataset
regression
dcgan
mnist
}
# by default, run all examples
if [ "" == "$EXAMPLES" ]; then
run_all
else
for i in $(echo $EXAMPLES | sed "s/,/ /g")
do
echo "Starting $i"
$i
echo "Finished $i, status $?"
done
fi
if [ "" == "$ERRORS" ]; then
echo "Completed successfully with status $?"
else
echo "Some examples failed:"
printf "$ERRORS"
exit 1
fi