|
| 1 | +#!/usr/bin/env bash |
| 2 | +# This script runs through the code in each of the cpp examples. |
| 3 | +# The purpose is just as an integration test, not to actually train models in any meaningful way. |
| 4 | + |
| 5 | +# Optionally specify a comma separated list of examples to run. |
| 6 | +# can be run as: |
| 7 | +# ./run_cpp_examples.sh "get_libtorch,run_all,clean" |
| 8 | +# To get libtorch, run all examples, and remove temporary/changed data files. |
| 9 | + |
| 10 | +BASE_DIR=`pwd`"/"`dirname $0` |
| 11 | +echo "BASE_DIR: $BASE_DIR" |
| 12 | +EXAMPLES=`echo $1 | sed -e 's/ //g'` |
| 13 | +HOME_DIR=$HOME |
| 14 | +ERRORS="" |
| 15 | + |
| 16 | +function error() { |
| 17 | + ERR=$1 |
| 18 | + ERRORS="$ERRORS\n$ERR" |
| 19 | + echo $ERR |
| 20 | +} |
| 21 | + |
| 22 | +function get_libtorch() { |
| 23 | + echo "Getting libtorch" |
| 24 | + cd $HOME_DIR |
| 25 | + wget https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip |
| 26 | + unzip libtorch-shared-with-deps-latest.zip |
| 27 | + |
| 28 | + if [ $? -eq 0 ]; then |
| 29 | + echo "Successfully downloaded and extracted libtorch" |
| 30 | + LIBTORCH_PATH="$HOME_DIR/libtorch" # Store the LibTorch path in a variable. |
| 31 | + echo "LibTorch path: $LIBTORCH_PATH" # Print the LibTorch path |
| 32 | + else |
| 33 | + error "Failed to download or extract LibTorch" |
| 34 | + fi |
| 35 | +} |
| 36 | + |
| 37 | +function start() { |
| 38 | + EXAMPLE=${FUNCNAME[1]} |
| 39 | + cd $BASE_DIR/cpp/$EXAMPLE |
| 40 | + echo "Running example: $EXAMPLE" |
| 41 | +} |
| 42 | + |
| 43 | +function autograd() { |
| 44 | + start |
| 45 | + mkdir build |
| 46 | + cd build |
| 47 | + cmake -DCMAKE_PREFIX_PATH=$LIBTORCH_PATH .. |
| 48 | + make |
| 49 | + if [ $? -eq 0 ]; then |
| 50 | + echo "Successfully built $EXAMPLE" |
| 51 | + ./$EXAMPLE # Run the executable |
| 52 | + else |
| 53 | + error "Failed to build $EXAMPLE" |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | +} |
| 57 | + |
| 58 | +function clean() { |
| 59 | + cd $BASE_DIR |
| 60 | + echo "Running clean to remove cruft" |
| 61 | + # Remove the build directories |
| 62 | + find . -type d -name 'build' -exec rm -rf {} + |
| 63 | + # Remove the libtorch directory |
| 64 | + rm -rf $HOME_DIR/libtorch |
| 65 | + echo "Clean completed" |
| 66 | +} |
| 67 | + |
| 68 | +function run_all() { |
| 69 | + autograd |
| 70 | +} |
| 71 | + |
| 72 | +# by default, run all examples |
| 73 | +if [ "" == "$EXAMPLES" ]; then |
| 74 | + run_all |
| 75 | +else |
| 76 | + for i in $(echo $EXAMPLES | sed "s/,/ /g") |
| 77 | + do |
| 78 | + echo "Starting $i" |
| 79 | + $i |
| 80 | + echo "Finished $i, status $?" |
| 81 | + done |
| 82 | +fi |
| 83 | + |
| 84 | +if [ "" == "$ERRORS" ]; then |
| 85 | + echo "Completed successfully with status $?" |
| 86 | +else |
| 87 | + echo "Some examples failed:" |
| 88 | + printf "$ERRORS" |
| 89 | + exit 1 |
| 90 | +fi |
0 commit comments