test-lora-conversion-inference.sh 5.5 KB

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