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