Skip to content

Commit 5ebdefc

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

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-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+
/reduce-llms-for-testing
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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=reduce-llms-for-testing
12+
MODELS_REPO_URL=https://huggingface.co/ltoniazzi/$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+
# Load the expected starting strings from the text file
23+
EXPECTED_BASE_FULL=$(cat $MODELS_REPO/data/pale_blue_dot.txt)
24+
EXPECTED_LORA_HOT_FULL=$(cat $MODELS_REPO/data/bohemian_rhapsody.txt)
25+
EXPECTED_LORA_MERGED_FULL=$(cat $MODELS_REPO/data/bohemian_rhapsody.txt)
26+
27+
# Declare a regular array to store results
28+
results=()
29+
30+
trim_leading_whitespace() {
31+
local input_string="$1"
32+
# Remove leading whitespace
33+
echo "${input_string#"${input_string%%[![:space:]]*}"}"
34+
}
35+
36+
extract_starting_substring() {
37+
local reference_string="$1"
38+
local target_string="$2"
39+
40+
local target_length=${#target_string}
41+
echo "${reference_string:0:$target_length}"
42+
}
43+
44+
run_conversion_and_inference_lora() {
45+
local model_name=$1
46+
local hidden_size=$2
47+
48+
# Convert safetensors to gguf
49+
echo "Running convert_hf_to_gguf.py for $model_name with hidden_size $hidden_size..."
50+
python convert_hf_to_gguf.py $MODELS_REPO/$model_name/hidden_size=$hidden_size/base \
51+
--outfile $MODELS_REPO/$model_name/hidden_size=$hidden_size/base/Base-F32.gguf \
52+
--outtype f32
53+
54+
echo "Running convert_lora_to_gguf.py for $model_name with hidden_size $hidden_size..."
55+
python3 convert_lora_to_gguf.py $MODELS_REPO/$model_name/hidden_size=$hidden_size/lora \
56+
--base $MODELS_REPO/$model_name/hidden_size=$hidden_size/base \
57+
--outtype f32
58+
59+
echo "Running llama-export-lora with lora for $model_name with hidden_size $hidden_size..."
60+
./llama-export-lora \
61+
-m $MODELS_REPO/$model_name/hidden_size=$hidden_size/base/Base-F32.gguf \
62+
-o $MODELS_REPO/$model_name/hidden_size=$hidden_size/base/Base-F32-lora-merged.gguf \
63+
--lora $MODELS_REPO/$model_name/hidden_size=$hidden_size/lora/Lora-F32-LoRA.gguf
64+
65+
# Run inference
66+
echo "Running llama-cli without lora for $model_name with hidden_size $hidden_size..."
67+
OUTPUT_BASE=$(./llama-cli -m $MODELS_REPO/$model_name/hidden_size=$hidden_size/base/Base-F32.gguf \
68+
-p "Look again at that dot." -n 50 --seed 42 --temp 0)
69+
70+
echo "Running llama-cli with lora for $model_name with hidden_size $hidden_size..."
71+
OUTPUT_LORA_HOT=$(./llama-cli -m $MODELS_REPO/$model_name/hidden_size=$hidden_size/base/Base-F32.gguf \
72+
--lora $MODELS_REPO/$model_name/hidden_size=$hidden_size/lora/Lora-F32-LoRA.gguf \
73+
-p "I see a little silhouetto" -n 50 --seed 42 --temp 0)
74+
75+
echo "Running llama-cli with exported lora for $model_name with hidden_size $hidden_size..."
76+
OUTPUT_LORA_MERGED=$(./llama-cli -m $MODELS_REPO/$model_name/hidden_size=$hidden_size/base/Base-F32-lora-merged.gguf \
77+
-p "I see a little silhouetto" -n 50 --seed 42 --temp 0)
78+
79+
# Extract the corresponding substring from EXPECTED_BASE
80+
# and remove initial white spaces in OUTPUT_BASE
81+
OUTPUT_BASE=$(trim_leading_whitespace "$OUTPUT_BASE")
82+
EXPECTED_BASE=$(extract_starting_substring "$EXPECTED_BASE_FULL" "$OUTPUT_BASE")
83+
OUTPUT_LORA_HOT=$(trim_leading_whitespace "$OUTPUT_LORA_HOT")
84+
EXPECTED_LORA_HOT=$(extract_starting_substring "$EXPECTED_LORA_HOT_FULL" "$OUTPUT_LORA_HOT")
85+
OUTPUT_LORA_MERGED=$(trim_leading_whitespace "$OUTPUT_LORA_MERGED")
86+
EXPECTED_LORA_MERGED=$(extract_starting_substring "$EXPECTED_LORA_MERGED_FULL" "$OUTPUT_LORA_MERGED")
87+
88+
# Compare the actual output with the expected start
89+
if [[ "$OUTPUT_BASE" != "$EXPECTED_BASE" ]]; then
90+
echo "Error: $model_name OUTPUT_BASE does not start with the expected string."
91+
echo -e "Out=$OUTPUT_BASE\n\nExp=$EXPECTED_BASE"
92+
exit 1
93+
fi
94+
if [[ "$OUTPUT_LORA_HOT" != "$EXPECTED_LORA_HOT" ]]; then
95+
echo "Error: $model_name OUTPUT_LORA_HOT does not start with the expected string."
96+
echo -e "Out=$OUTPUT_LORA_HOT\n\nExp=$EXPECTED_LORA_HOT"
97+
exit 1
98+
fi
99+
if [[ "$OUTPUT_LORA_MERGED" != "$EXPECTED_LORA_MERGED" ]]; then
100+
echo "Error: $model_name OUTPUT_LORA_MERGED does not start with the expected string."
101+
echo -e "Out=$OUTPUT_LORA_MERGED\n\nExp=$EXPECTED_LORA_MERGED"
102+
exit 1
103+
fi
104+
105+
# Store the results in the regular array
106+
results+=("
107+
\n\033[1mResults for $model_name with hidden_size $hidden_size:\033[0m
108+
\n\033[32m • Base:\n$OUTPUT_BASE
109+
\n\033[34m • Lora hot:\n$OUTPUT_LORA_HOT
110+
\n\033[36m • Lora merged:\n$OUTPUT_LORA_MERGED
111+
\n \033[0m
112+
")
113+
114+
echo "All steps completed for $model_name with hidden_size $hidden_size!"
115+
}
116+
117+
# Run test for each model
118+
for param in "${params[@]}"; do
119+
run_conversion_and_inference_lora $param
120+
done
121+
122+
# Print all collected results
123+
echo -e "\n\n\033[1mSummary of All Results:\033[0m"
124+
for result in "${results[@]}"; do
125+
echo -e "$result"
126+
done

0 commit comments

Comments
 (0)