| 
 | 1 | +#!/bin/bash  | 
 | 2 | +set -e  | 
 | 3 | + | 
 | 4 | +# Array of models to iterate over  | 
 | 5 | +declare -a params=(  | 
 | 6 | +    "Gemma2ForCausalLM 64"  | 
 | 7 | +    "LlamaForCausalLM 64"  | 
 | 8 | +    "Phi3ForCausalLM 64"  | 
 | 9 | +)  | 
 | 10 | + | 
 | 11 | +MODELS_REPO=lora-tests  | 
 | 12 | +MODELS_REPO_URL=https://huggingface.co/ggml-org/$MODELS_REPO  | 
 | 13 | + | 
 | 14 | +# Clone the Hugging Face repository if the directory does not exist  | 
 | 15 | +if [ ! -d "$MODELS_REPO" ]; then  | 
 | 16 | +    echo "Cloning the Hugging Face repository..."  | 
 | 17 | +    git clone $MODELS_REPO_URL  | 
 | 18 | +else  | 
 | 19 | +    echo "Repository already exists. Skipping clone."  | 
 | 20 | +fi  | 
 | 21 | + | 
 | 22 | +# Array to store results to print  | 
 | 23 | +results=()  | 
 | 24 | + | 
 | 25 | +trim_leading_whitespace() {  | 
 | 26 | +    local input_string="$1"  | 
 | 27 | +    echo "${input_string#"${input_string%%[![:space:]]*}"}"  | 
 | 28 | +}  | 
 | 29 | + | 
 | 30 | +extract_starting_substring() {  | 
 | 31 | +    local reference_string="$1"  | 
 | 32 | +    local target_string="$2"  | 
 | 33 | + | 
 | 34 | +    local target_length=${#target_string}  | 
 | 35 | +    echo "${reference_string:0:$target_length}"  | 
 | 36 | +}  | 
 | 37 | + | 
 | 38 | +get_first_word() {  | 
 | 39 | +    local input_string="$1"  | 
 | 40 | +    read -r first_word _ <<< "$input_string"  | 
 | 41 | +    echo "$first_word"  | 
 | 42 | +}  | 
 | 43 | + | 
 | 44 | +# Load the expected strings  | 
 | 45 | +EXPECTED_BASE_FULL=$(cat $MODELS_REPO/data/pale_blue_dot.txt)  | 
 | 46 | +EXPECTED_LORA_FULL=$(cat $MODELS_REPO/data/bohemian_rhapsody.txt)  | 
 | 47 | +EXPECTED_BASE_FIRST_WORD=$(get_first_word "$EXPECTED_BASE_FULL")  | 
 | 48 | +EXPECTED_LORA_FIRST_WORD=$(get_first_word "$EXPECTED_LORA_FULL")  | 
 | 49 | + | 
 | 50 | +run_conversion_and_inference_lora() {  | 
 | 51 | +    local model_name=$1  | 
 | 52 | +    local hidden_size=$2  | 
 | 53 | + | 
 | 54 | +    echo -e "\n\n-------- RUNNING TEST FOR MODEL $model_name --------\n\n"  | 
 | 55 | + | 
 | 56 | +    # Convert safetensors to gguf  | 
 | 57 | +    echo "Running convert_hf_to_gguf.py for $model_name with hidden_size $hidden_size..."  | 
 | 58 | +    python convert_hf_to_gguf.py $MODELS_REPO/$model_name/hidden_size=$hidden_size/base \  | 
 | 59 | +        --outfile $MODELS_REPO/$model_name/hidden_size=$hidden_size/base/Base-F32.gguf \  | 
 | 60 | +        --outtype f32  | 
 | 61 | + | 
 | 62 | +    echo -e "\n\n---------------------------\n\n"  | 
 | 63 | +    echo "Running convert_lora_to_gguf.py for $model_name with hidden_size $hidden_size..."  | 
 | 64 | +    python3 convert_lora_to_gguf.py $MODELS_REPO/$model_name/hidden_size=$hidden_size/lora \  | 
 | 65 | +        --base $MODELS_REPO/$model_name/hidden_size=$hidden_size/base \  | 
 | 66 | +        --outtype f32  | 
 | 67 | + | 
 | 68 | +    echo -e "\n\n---------------------------\n\n"  | 
 | 69 | +    echo "Running llama-export-lora with lora for $model_name with hidden_size $hidden_size..."  | 
 | 70 | +    ./llama-export-lora \  | 
 | 71 | +        -m $MODELS_REPO/$model_name/hidden_size=$hidden_size/base/Base-F32.gguf \  | 
 | 72 | +        -o $MODELS_REPO/$model_name/hidden_size=$hidden_size/base/Base-F32-lora-merged.gguf \  | 
 | 73 | +        --lora $MODELS_REPO/$model_name/hidden_size=$hidden_size/lora/Lora-F32-LoRA.gguf  | 
 | 74 | + | 
 | 75 | +    # Run inference  | 
 | 76 | +    echo -e "\n\n---------------------------\n\n"  | 
 | 77 | +    echo "Running llama-cli without lora for $model_name with hidden_size $hidden_size..."  | 
 | 78 | +    OUTPUT_BASE=$(./llama-cli -m $MODELS_REPO/$model_name/hidden_size=$hidden_size/base/Base-F32.gguf \  | 
 | 79 | +        -p "$EXPECTED_BASE_FIRST_WORD" -n 50 --seed 42 --temp 0)  | 
 | 80 | + | 
 | 81 | +    echo -e "\n\n---------------------------\n\n"  | 
 | 82 | +    echo "Running llama-cli with hot lora for $model_name with hidden_size $hidden_size..."  | 
 | 83 | +    OUTPUT_LORA_HOT=$(./llama-cli -m $MODELS_REPO/$model_name/hidden_size=$hidden_size/base/Base-F32.gguf \  | 
 | 84 | +        --lora $MODELS_REPO/$model_name/hidden_size=$hidden_size/lora/Lora-F32-LoRA.gguf \  | 
 | 85 | +        -p "$EXPECTED_LORA_FIRST_WORD" -n 50 --seed 42 --temp 0)  | 
 | 86 | + | 
 | 87 | +    echo -e "\n\n---------------------------\n\n"  | 
 | 88 | +    echo "Running llama-cli with merged lora for $model_name with hidden_size $hidden_size..."  | 
 | 89 | +    OUTPUT_LORA_MERGED=$(./llama-cli -m $MODELS_REPO/$model_name/hidden_size=$hidden_size/base/Base-F32-lora-merged.gguf \  | 
 | 90 | +        -p "$EXPECTED_LORA_FIRST_WORD" -n 50 --seed 42 --temp 0)  | 
 | 91 | + | 
 | 92 | +    # Remove any initial white space  | 
 | 93 | +    OUTPUT_BASE=$(trim_leading_whitespace "$OUTPUT_BASE")  | 
 | 94 | +    OUTPUT_LORA_HOT=$(trim_leading_whitespace "$OUTPUT_LORA_HOT")  | 
 | 95 | +    OUTPUT_LORA_MERGED=$(trim_leading_whitespace "$OUTPUT_LORA_MERGED")  | 
 | 96 | +    # Extract the corresponding substring from full string  | 
 | 97 | +    EXPECTED_BASE=$(extract_starting_substring "$EXPECTED_BASE_FULL" "$OUTPUT_BASE")  | 
 | 98 | +    EXPECTED_LORA=$(extract_starting_substring "$EXPECTED_LORA_FULL" "$OUTPUT_LORA_HOT")  | 
 | 99 | + | 
 | 100 | +    # Assert output equals the expected output  | 
 | 101 | +    if [[ "$OUTPUT_BASE" != "$EXPECTED_BASE" ]]; then  | 
 | 102 | +        echo "Error: $model_name OUTPUT_BASE does not start with the expected string."  | 
 | 103 | +        echo -e "Out=$OUTPUT_BASE\n\nExp=$EXPECTED_BASE"  | 
 | 104 | +        exit 1  | 
 | 105 | +    fi  | 
 | 106 | +    if [[ "$OUTPUT_LORA_HOT" != "$EXPECTED_LORA" ]]; then  | 
 | 107 | +        echo "Error: $model_name OUTPUT_LORA_HOT does not start with the expected string."  | 
 | 108 | +        echo -e "Out=$OUTPUT_LORA_HOT\n\nExp=$EXPECTED_LORA"  | 
 | 109 | +        exit 1  | 
 | 110 | +    fi  | 
 | 111 | +    if [[ "$OUTPUT_LORA_MERGED" != "$EXPECTED_LORA" ]]; then  | 
 | 112 | +        echo "Error: $model_name OUTPUT_LORA_MERGED does not start with the expected string."  | 
 | 113 | +        echo -e "Out=$OUTPUT_LORA_MERGED\n\nExp=$EXPECTED_LORA"  | 
 | 114 | +        exit 1  | 
 | 115 | +    fi  | 
 | 116 | + | 
 | 117 | +    # Store the results  | 
 | 118 | +    results+=("  | 
 | 119 | +    \n\033[1mResults for $model_name with hidden_size $hidden_size:\033[0m  | 
 | 120 | +    \n\033[32m  • Base:\n$OUTPUT_BASE  | 
 | 121 | +    \n\033[34m  • Lora hot:\n$OUTPUT_LORA_HOT  | 
 | 122 | +    \n\033[36m  • Lora merged:\n$OUTPUT_LORA_MERGED  | 
 | 123 | +    \n \033[0m  | 
 | 124 | +    ")  | 
 | 125 | + | 
 | 126 | +    echo "All tests passed for $model_name with hidden_size $hidden_size!"  | 
 | 127 | +}  | 
 | 128 | + | 
 | 129 | +# Run test for each model  | 
 | 130 | +for param in "${params[@]}"; do  | 
 | 131 | +    run_conversion_and_inference_lora $param  | 
 | 132 | +done  | 
 | 133 | + | 
 | 134 | +# Print results  | 
 | 135 | +echo -e "\n\n---------------------------\n\n"  | 
 | 136 | +echo -e "\n\033[1mSummary of All Results:\033[0m"  | 
 | 137 | +for result in "${results[@]}"; do  | 
 | 138 | +    echo -e "$result"  | 
 | 139 | +done  | 
0 commit comments