From 9a11611a7d3a0377dcb7bf8fb9f1f9de04391fd5 Mon Sep 17 00:00:00 2001 From: crasm Date: Fri, 19 Jan 2024 02:09:57 -0500 Subject: [PATCH] scripts : add some fancy conversion from snake_case to PascalCase --- scripts/ci-run.sh | 81 ++++++++++++++++++++++++++------------------- scripts/lib.sh | 21 ++++++++++-- scripts/lib_test.sh | 9 +++++ 3 files changed, 74 insertions(+), 37 deletions(-) diff --git a/scripts/ci-run.sh b/scripts/ci-run.sh index 3e7ca8af4..934d9f77b 100755 --- a/scripts/ci-run.sh +++ b/scripts/ci-run.sh @@ -9,62 +9,75 @@ source 'lib.sh' #### END SETUP #### # TODO: send model to ctest_model tests using env variable -# GG_TEST_CTEST_MODEL_MODELFILE= +# GG_CI_CTEST_MODELFILE= IsValidTestTarget() { case "$1" in cmake | ctest_main | model_3b | model_7b | test_cpu | test_cuda | test_metal | ctest_model) - return 0;; + return $_OK;; *) - return 1;; + return $_ERR;; esac } -declare -a test_targets +declare -a targets if (( $# > 0 )); then - test_targets=("$@") -elif _IsSet GG_TEST_TARGETS; then - read -r -a test_targets <<< "$GG_TEST_TARGETS" + targets=("$@") +elif _IsSet GG_CI_TARGETS; then + read -r -a targets <<< "$GG_CI_TARGETS" else cat >&2 <<'EOF' -You must specify the test targets either as commandline arguments or using the -GG_TEST_TARGETS environment variable. Test targets will be run sequentially -in-order. +usage: + ci-run.sh [targets...] -Some test targets depend on other test targets, as described below. +config variables: + GG_CI_TARGETS : Space delimited sequence of targets. + Overridden by commandline arguments. + GG_CI_WORKDIR : Build files and results. + Defaults to /tmp. + GG_CI_DATADIR : Persistent model files and datasets, unchanged between runs. + Defaults to ~/.cache/llama.cpp/ci-data. + GG_CI_TEMPDIR : Scratch directory for quantized model files. + Defaults to ~/.cache/llama.cpp/ci-temp -cli usage: - ci-run.sh test_targets... +examples: + # A run on a low-spec VM without a dedicated GPU. + ci-run.sh cmake ctest_main model_3b test_cpu ctest_model -ci usage: - GG_TEST_TARGETS='test_targets...' ci-run.sh - -example: - # This is a typical run for a low-spec host without a dedicated GPU. - ci-run.sh cmake ctest_main model_3b test_cpu ctest_model + # A run on a Mac Studio with a ramdisk at ~/tmp + GG_CI_WORKDIR=~/tmp/ci-work \ + GG_CI_TEMPDIR=~/tmp/ci-temp \ + ci-run.sh cmake ctest_main model_7b test_cpu test_metal ctest_model test targets: - cmake : run cmake to produce debug and release builds - ctest_main : run main ctest tests for debug and release - (requires: cmake) - model_3b : download and quantize openllama_3b_v2 - model_7b : download and quantize openllama_7b_v2 - test_cpu : test CPU inference, perplexity tests, etc. - (requires: model_3b or model_7b) - test_cuda : test CUDA ... - (requires: model_3b or model_7b) - test_metal : test Metal ... - (requires: model_3b or model_7b) - ctest_model : run ctest tests that require the openllama model - (requires: model_3b or model_7b) + cmake : Run cmake to produce debug and release builds. + ctest_main : Run main ctest tests for debug and release. + model_3b, + model_7b : Download and quantize openllama_3b_v2 and/or openllama_7b_v2. + test_cpu, + test_cuda, + test_metal : Test CPU inference, perplexity tests, etc. + ctest_model : Run ctest tests that require the openllama model. EOF - exit 1 + exit $_ERR fi -for target in "${test_targets[@]}"; do +for target in "${targets[@]}"; do if IsValidTestTarget "$target"; then _LogInfo "Received test target: $target" else _LogFatal "Invalid test target: $target" fi done + +cd .. +[[ -d .git && -x .git ]] || _LogFatal 'Could not cd to llama.cpp root direcory' + +TargetCmake() { + echo hello +} + +for target in "${targets[@]}"; do + pascal_target="$(_SnakeToPascalCase "$target")" + "Target$pascal_target" +done diff --git a/scripts/lib.sh b/scripts/lib.sh index b5e5a7171..d4d240736 100644 --- a/scripts/lib.sh +++ b/scripts/lib.sh @@ -5,6 +5,9 @@ if [[ ${BASH_SOURCE[0]} -ef $0 ]]; then exit 1 fi +readonly _OK=0 +readonly _ERR=1 + _Log() { local level=$1 msg=$2 printf >&2 '%s: %s\n' "$level" "$msg" @@ -25,14 +28,26 @@ _LogFatal() { # Return true if the variable with name $1 is set _IsSet() { - (( $# != 1 )) && return false + (( $# != 1 )) && return $_ERR if [[ -n ${!1+x} ]]; then - return 0 + return $_OK else - return 1 + return $_ERR fi } _IsNotSet() { ! _IsSet "$@" } + +_SnakeToPascalCase() { + (( $# != 1 )) && return $_ERR + local IFS='_' + local pascal='' + for word in $1; do + local head; head=$(tr '[:lower:]' '[:upper:]' <<< "${word:0:1}") + local tail=${word:1} + pascal+="$head$tail" + done + echo -n "$pascal" +} diff --git a/scripts/lib_test.sh b/scripts/lib_test.sh index dec391466..7cb9693e7 100755 --- a/scripts/lib_test.sh +++ b/scripts/lib_test.sh @@ -12,6 +12,8 @@ shellcheck --external-sources "$this" source 'lib.sh' #### END SETUP #### +shellcheck 'lib.sh' + Pass() { local test_func="${FUNCNAME[1]}" _Log 'PASSED' "$test_func" @@ -47,3 +49,10 @@ TestIsNotSet() { Fail 'bar was detected as set' else Pass; fi }; TestIsNotSet + +TestSnakeToPascalCase() { + local id; id=$(_SnakeToPascalCase this_is_an_id) + if [[ $id != ThisIsAnId ]]; then + Fail "this_is_an_id was converted to $id" + else Pass; fi +}; TestSnakeToPascalCase