Skip to content

Commit 9a8f050

Browse files
ltoniazziLorenzo Toniazzi
authored andcommitted
Add printing to check weights match torch version
1 parent fb487bb commit 9a8f050

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,6 @@ poetry.toml
129129

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

0 commit comments

Comments
 (0)