KBaba7/llama.cpp
0
1#!/bin/bash2 3PROG=${0##*/}4build_dir="build-ci-debug"5 6# Print Color Commands7red=$(tput setaf 1)8green=$(tput setaf 2)9yellow=$(tput setaf 3)10blue=$(tput setaf 4)11magenta=$(tput setaf 5)12cyan=$(tput setaf 6)13normal=$(tput sgr0)14 15 16# Print Help Message17####################18 19print_full_help() {20 cat << EOF21Usage: $PROG [OPTION]... <test_regex> (test_number)22Debug specific ctest program.23 24Options:25 -h, --help display this help and exit26 -g run in gdb mode27 28Arguments:29 <test_regex> (Mandatory) Supply one regex to the script to filter tests30 (test_number) (Optional) Test number to run a specific test31 32Example:33 $PROG test-tokenizer34 $PROG test-tokenizer 335EOF36}37 38abort() {39 echo "Error: $1" >&240 cat << EOF >&241Usage: $PROG [OPTION]... <test_regex> (test_number)42Debug specific ctest program.43Refer to --help for full instructions.44EOF45 exit 146}47 48 49# Dependency Sanity Check50#########################51 52check_dependency() {53 command -v "$1" >/dev/null 2>&1 || {54 abort "$1 is required but not found. Please install it and try again."55 }56}57 58check_dependency ctest59check_dependency cmake60 61 62# Step 0: Check the args63########################64 65if [ x"$1" = x"-h" ] || [ x"$1" = x"--help" ]; then66 print_full_help >&267 exit 068fi69 70# Parse command-line options71gdb_mode=false72while getopts "g" opt; do73 case $opt in74 g)75 gdb_mode=true76 echo "gdb_mode Mode Enabled"77 ;;78 esac79done80 81# Shift the option parameters82shift $((OPTIND - 1))83 84# Positionial Argument Processing : <test_regex>85if [ -z "${1}" ]; then86 abort "Test regex is required"87else88 test_suite=${1:-}89fi90 91# Positionial Argument Processing : (test_number)92test_number=${2:-}93 94 95# Step 1: Reset and Setup folder context96########################################97 98## Sanity check that we are actually in a git repo99repo_root=$(git rev-parse --show-toplevel)100if [ ! -d "$repo_root" ]; then101 abort "Not in a Git repository."102fi103 104## Reset folder to root context of git repo and Create and enter build directory105pushd "$repo_root"106rm -rf "$build_dir" && mkdir "$build_dir" || abort "Failed to make $build_dir"107 108 109# Step 2: Setup Build Environment and Compile Test Binaries110###########################################################111 112# Note: test-eval-callback requires -DLLAMA_CURL113cmake -B "./$build_dir" -DCMAKE_BUILD_TYPE=Debug -DGGML_CUDA=1 -DLLAMA_CURL=1 || abort "Failed to build environment"114pushd "$build_dir"115make -j || abort "Failed to compile"116popd > /dev/null || exit 1117 118 119# Step 3: Find all tests available that matches REGEX120####################################################121 122# Ctest Gather Tests123# `-R test-tokenizer` : looks for all the test files named `test-tokenizer*` (R=Regex)124# `-N` : "show-only" disables test execution & shows test commands that you can feed to GDB.125# `-V` : Verbose Mode126printf "\n\nGathering tests that fit REGEX: ${test_suite} ...\n"127pushd "$build_dir"128tests=($(ctest -R ${test_suite} -V -N | grep -E " +Test +#[0-9]+*" | cut -d':' -f2 | awk '{$1=$1};1'))129if [ ${#tests[@]} -eq 0 ]; then130 abort "No tests available... check your compilation process..."131fi132popd > /dev/null || exit 1133 134 135# Step 4: Identify Test Command for Debugging136#############################################137 138# Select test number139if [ -z $test_number ]; then140 # List out available tests141 printf "Which test would you like to debug?\n"142 id=0143 for s in "${tests[@]}"144 do145 echo "Test# ${id}"146 echo " $s"147 ((id++))148 done149 150 # Prompt user which test they wanted to run151 printf "\nRun test#? "152 read test_number153 154else155 printf "\nUser Already Requested #${test_number}\n"156 157fi158 159# Grab all tests commands160pushd "$build_dir"161sIFS=$IFS # Save Initial IFS (Internal Field Separator)162IFS=$'\n' # Change IFS (Internal Field Separator) (So we split ctest output by newline rather than by spaces)163test_args=($(ctest -R ${test_suite} -V -N | grep "Test command" | cut -d':' -f3 | awk '{$1=$1};1' )) # Get test args164IFS=$sIFS # Reset IFS (Internal Field Separator)165popd > /dev/null || exit 1166 167# Grab specific test command168single_test_name="${tests[test_number]}"169single_test_command="${test_args[test_number]}"170 171 172# Step 5: Execute or GDB Debug173##############################174 175printf "${magenta}Running Test #${test_number}: ${single_test_name}${normal}\n"176printf "${cyan}single_test_command: ${single_test_command}${normal}\n"177 178if [ "$gdb_mode" = "true" ]; then179 # Execute debugger180 pushd "$repo_root" || exit 1181 eval "gdb --args ${single_test_command}"182 popd > /dev/null || exit 1183 184else185 # Execute Test186 pushd "$repo_root" || exit 1187 eval "${single_test_command}"188 exit_code=$?189 popd > /dev/null || exit 1190 191 # Print Result192 printf "${blue}Ran Test #${test_number}: ${single_test_name}${normal}\n"193 printf "${yellow}Command: ${single_test_command}${normal}\n"194 if [ $exit_code -eq 0 ]; then195 printf "${green}TEST PASS${normal}\n"196 else197 printf "${red}TEST FAIL${normal}\n"198 fi199 200fi201 202# Return to the directory from which the user ran the command.203popd > /dev/null || exit 1204 