test-lora-conversion-inference.sh 5.3 KB

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