Spaces:
Build error
Build error
eval fine-tuned checkpoints
Browse files- llm_toolkit/eval_epochs.py +67 -63
- scripts/eval-epochs.sh +15 -0
- scripts/eval-mac.sh +14 -7
llm_toolkit/eval_epochs.py
CHANGED
@@ -3,6 +3,20 @@ import sys
|
|
3 |
import subprocess
|
4 |
from dotenv import find_dotenv, load_dotenv
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from llm_toolkit.llm_utils import *
|
7 |
from llm_toolkit.translation_utils import *
|
8 |
|
@@ -12,90 +26,80 @@ def evaluate_model_all_epochs(
|
|
12 |
tokenizer,
|
13 |
model_name,
|
14 |
adapter_path_base,
|
15 |
-
|
16 |
-
|
17 |
start_epoch=0,
|
18 |
end_epoch=-1,
|
|
|
|
|
|
|
19 |
):
|
20 |
-
new_env = os.environ.copy()
|
21 |
-
new_env["MODEL_NAME"] = model_name
|
22 |
-
model = model_name.split("/")[-1]
|
23 |
-
|
24 |
-
new_env["LOAD_IN_4BIT"] = "true" if load_in_4bit else "false"
|
25 |
-
if result_file is not None:
|
26 |
-
new_env["RESULTS_PATH"] = result_file
|
27 |
-
|
28 |
if adapter_path_base is None:
|
29 |
num_train_epochs = 0
|
30 |
print(f"No adapter path provided. Running with base model:{model_name}")
|
31 |
else:
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
d
|
40 |
-
for d in os.listdir(adapter_path_base)
|
41 |
-
if os.path.isdir(os.path.join(adapter_path_base, d))
|
42 |
-
]
|
43 |
-
|
44 |
-
subdirs = sorted(subdirs, key=lambda x: int(x.split("-")[-1]))
|
45 |
-
num_train_epochs = len(subdirs)
|
46 |
-
print(f"found {num_train_epochs} checkpoints: {subdirs}")
|
47 |
-
|
48 |
-
for i in range(start_epoch, num_train_epochs + 1):
|
49 |
-
print(f"Epoch {i}")
|
50 |
-
if i == 0:
|
51 |
-
os.unsetenv("ADAPTER_NAME_OR_PATH")
|
52 |
-
else:
|
53 |
-
adapter_path = adapter_path_base + "/" + subdirs[i - 1]
|
54 |
-
new_env["ADAPTER_NAME_OR_PATH"] = adapter_path
|
55 |
|
56 |
-
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
subprocess.run(
|
61 |
-
f"python llm_toolkit/eval_shots.py {num_of_entries}",
|
62 |
-
shell=True,
|
63 |
-
env=new_env,
|
64 |
-
stdout=f_obj,
|
65 |
-
text=True,
|
66 |
-
)
|
67 |
|
|
|
68 |
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
workding_dir = os.path.dirname(found_dotenv)
|
78 |
-
os.chdir(workding_dir)
|
79 |
-
sys.path.append(workding_dir)
|
80 |
-
print("workding dir:", workding_dir)
|
81 |
-
print(f"adding {workding_dir} to sys.path")
|
82 |
-
sys.path.append(workding_dir)
|
83 |
|
|
|
84 |
model_name = os.getenv("MODEL_NAME")
|
85 |
adapter_path_base = os.getenv("ADAPTER_PATH_BASE")
|
86 |
-
start_epoch = int(os.getenv("START_EPOCH",
|
87 |
end_epoch = os.getenv("END_EPOCH", -1)
|
88 |
load_in_4bit = os.getenv("LOAD_IN_4BIT", "true").lower() == "true"
|
89 |
-
|
90 |
-
|
91 |
-
num_of_entries = int(sys.argv[1]) if len(sys.argv) > 1 else -1
|
92 |
|
93 |
print(
|
94 |
model_name,
|
95 |
adapter_path_base,
|
96 |
load_in_4bit,
|
97 |
start_epoch,
|
98 |
-
|
99 |
)
|
100 |
|
101 |
device = check_gpu()
|
@@ -132,11 +136,11 @@ if __name__ == "__main__":
|
|
132 |
tokenizer,
|
133 |
model_name,
|
134 |
adapter_path_base,
|
|
|
|
|
135 |
start_epoch=start_epoch,
|
136 |
end_epoch=end_epoch,
|
137 |
-
|
138 |
-
num_of_entries=num_of_entries,
|
139 |
-
result_file=result_file,
|
140 |
)
|
141 |
|
142 |
if is_cuda:
|
|
|
3 |
import subprocess
|
4 |
from dotenv import find_dotenv, load_dotenv
|
5 |
|
6 |
+
found_dotenv = find_dotenv(".env")
|
7 |
+
|
8 |
+
if len(found_dotenv) == 0:
|
9 |
+
found_dotenv = find_dotenv(".env.example")
|
10 |
+
print(f"loading env vars from: {found_dotenv}")
|
11 |
+
load_dotenv(found_dotenv, override=False)
|
12 |
+
|
13 |
+
workding_dir = os.path.dirname(found_dotenv)
|
14 |
+
os.chdir(workding_dir)
|
15 |
+
sys.path.append(workding_dir)
|
16 |
+
print("workding dir:", workding_dir)
|
17 |
+
print(f"adding {workding_dir} to sys.path")
|
18 |
+
sys.path.append(workding_dir)
|
19 |
+
|
20 |
from llm_toolkit.llm_utils import *
|
21 |
from llm_toolkit.translation_utils import *
|
22 |
|
|
|
26 |
tokenizer,
|
27 |
model_name,
|
28 |
adapter_path_base,
|
29 |
+
dataset,
|
30 |
+
results_path,
|
31 |
start_epoch=0,
|
32 |
end_epoch=-1,
|
33 |
+
batch_size=1,
|
34 |
+
max_new_tokens=300,
|
35 |
+
device="cuda",
|
36 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
if adapter_path_base is None:
|
38 |
num_train_epochs = 0
|
39 |
print(f"No adapter path provided. Running with base model:{model_name}")
|
40 |
else:
|
41 |
+
# find subdirectories in adapter_path_base
|
42 |
+
# and sort them by epoch number
|
43 |
+
subdirs = [
|
44 |
+
d
|
45 |
+
for d in os.listdir(adapter_path_base)
|
46 |
+
if os.path.isdir(os.path.join(adapter_path_base, d))
|
47 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
subdirs = sorted(subdirs, key=lambda x: int(x.split("-")[-1]))
|
50 |
+
num_train_epochs = len(subdirs)
|
51 |
+
print(f"found {num_train_epochs} checkpoints: {subdirs}")
|
52 |
|
53 |
+
if end_epoch < 0 or end_epoch > num_train_epochs:
|
54 |
+
end_epoch = num_train_epochs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
print(f"Running from epoch {start_epoch} to {end_epoch}")
|
57 |
|
58 |
+
for i in range(start_epoch, end_epoch + 1):
|
59 |
+
print(f"Epoch {i}")
|
60 |
+
if i > 0:
|
61 |
+
adapter_path = adapter_path_base + "/" + subdirs[i - 1]
|
62 |
+
print(f"loading adapter: {adapter_path}")
|
63 |
+
adapter_name = model.load_adapter(adapter_path)
|
64 |
+
model.active_adapters = adapter_name
|
65 |
+
|
66 |
+
predictions = eval_model(
|
67 |
+
model,
|
68 |
+
tokenizer,
|
69 |
+
dataset,
|
70 |
+
device=device,
|
71 |
+
batch_size=batch_size,
|
72 |
+
max_new_tokens=max_new_tokens,
|
73 |
+
)
|
74 |
+
|
75 |
+
model_name_with_epochs = f"{model_name}/epochs-{i:02d}"
|
76 |
|
77 |
+
save_results(
|
78 |
+
model_name_with_epochs,
|
79 |
+
results_path,
|
80 |
+
dataset,
|
81 |
+
predictions,
|
82 |
+
)
|
83 |
+
|
84 |
+
metrics = calc_metrics(dataset["english"], predictions, debug=True)
|
85 |
+
print(f"{model_name_with_epochs} metrics: {metrics}")
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
+
if __name__ == "__main__":
|
89 |
model_name = os.getenv("MODEL_NAME")
|
90 |
adapter_path_base = os.getenv("ADAPTER_PATH_BASE")
|
91 |
+
start_epoch = int(os.getenv("START_EPOCH", 1))
|
92 |
end_epoch = os.getenv("END_EPOCH", -1)
|
93 |
load_in_4bit = os.getenv("LOAD_IN_4BIT", "true").lower() == "true"
|
94 |
+
results_path = os.getenv("RESULTS_PATH", None)
|
95 |
+
data_path = os.getenv("DATA_PATH")
|
|
|
96 |
|
97 |
print(
|
98 |
model_name,
|
99 |
adapter_path_base,
|
100 |
load_in_4bit,
|
101 |
start_epoch,
|
102 |
+
results_path,
|
103 |
)
|
104 |
|
105 |
device = check_gpu()
|
|
|
136 |
tokenizer,
|
137 |
model_name,
|
138 |
adapter_path_base,
|
139 |
+
datasets["test"],
|
140 |
+
results_path,
|
141 |
start_epoch=start_epoch,
|
142 |
end_epoch=end_epoch,
|
143 |
+
device=device,
|
|
|
|
|
144 |
)
|
145 |
|
146 |
if is_cuda:
|
scripts/eval-epochs.sh
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/sh
|
2 |
+
|
3 |
+
BASEDIR=$(dirname "$0")
|
4 |
+
cd $BASEDIR/..
|
5 |
+
echo Current Directory:
|
6 |
+
pwd
|
7 |
+
|
8 |
+
export ORG_NAME=$1
|
9 |
+
export MODEL=$2
|
10 |
+
|
11 |
+
export MODEL_NAME=$ORG_NAME/$MODEL
|
12 |
+
export ADAPTER_PATH_BASE=llama-factory/saves/$MODEL
|
13 |
+
|
14 |
+
echo Evaluating $MODEL_NAME
|
15 |
+
python llm_toolkit/eval_epochs.py
|
scripts/eval-mac.sh
CHANGED
@@ -11,16 +11,23 @@ cat /etc/os-release
|
|
11 |
lscpu
|
12 |
grep MemTotal /proc/meminfo
|
13 |
|
14 |
-
pip install torch torchvision torchaudio
|
15 |
-
pip install -r requirements.txt
|
16 |
|
17 |
-
export START_NUM_SHOTS=50
|
18 |
|
19 |
-
|
20 |
|
21 |
-
|
22 |
|
23 |
-
|
24 |
|
25 |
-
./scripts/eval-model.sh shenzhi-wang/Llama3.1-8B-Chinese-Chat
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
lscpu
|
12 |
grep MemTotal /proc/meminfo
|
13 |
|
14 |
+
# pip install torch torchvision torchaudio
|
15 |
+
# pip install -r requirements.txt
|
16 |
|
17 |
+
# export START_NUM_SHOTS=50
|
18 |
|
19 |
+
# ./scripts/eval-model.sh internlm/internlm2_5-7b-chat
|
20 |
|
21 |
+
# ./scripts/eval-model.sh Qwen/Qwen2-7B-Instruct
|
22 |
|
23 |
+
# ./scripts/eval-model.sh shenzhi-wang/Mistral-7B-v0.3-Chinese-Chat
|
24 |
|
25 |
+
# ./scripts/eval-model.sh shenzhi-wang/Llama3.1-8B-Chinese-Chat
|
26 |
|
27 |
+
./scripts/eval-epochs.sh internlm internlm2_5-7b-chat
|
28 |
+
|
29 |
+
./scripts/eval-epochs.sh Qwen Qwen2-7B-Instruct
|
30 |
+
|
31 |
+
./scripts/eval-epochs.sh shenzhi-wang Mistral-7B-v0.3-Chinese-Chat
|
32 |
+
|
33 |
+
./scripts/eval-epochs.sh shenzhi-wang Llama3.1-8B-Chinese-Chat
|