Spaces:
Sleeping
Sleeping
Update run_eval.py
Browse files- run_eval.py +48 -15
run_eval.py
CHANGED
|
@@ -34,10 +34,21 @@ login(token)
|
|
| 34 |
DATASET_REPO = os.environ["HF_DATASET_REPO"]
|
| 35 |
api = HfApi()
|
| 36 |
|
| 37 |
-
# βββββ Evaluate each adapter βββββ
|
| 38 |
-
all_rows = []
|
| 39 |
METRICS_TO_KEEP = {"acc", "accuracy", "acc_stderr", "f1", "exact_match"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
|
|
|
| 41 |
for cfg in CONFIGS:
|
| 42 |
base_model_id = cfg["base_model"]
|
| 43 |
adapter_repo = cfg["adapter_repo"]
|
|
@@ -45,34 +56,49 @@ for cfg in CONFIGS:
|
|
| 45 |
tasks = cfg["tasks"]
|
| 46 |
|
| 47 |
print(f"\nLoading base model: {base_model_id}")
|
| 48 |
-
tokenizer =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
# Try causal first, fallback to encoder
|
| 51 |
try:
|
| 52 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 53 |
base_model_id,
|
| 54 |
trust_remote_code=True,
|
| 55 |
use_safetensors=True
|
| 56 |
-
)
|
| 57 |
is_encoder = False
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
base_model = AutoModelForSequenceClassification.from_pretrained(
|
| 60 |
base_model_id,
|
| 61 |
trust_remote_code=True,
|
| 62 |
use_safetensors=True
|
| 63 |
)
|
| 64 |
is_encoder = True
|
|
|
|
|
|
|
| 65 |
try:
|
| 66 |
info = model_info(adapter_repo)
|
| 67 |
files = [f.rfilename for f in info.siblings]
|
| 68 |
if "adapter_config.json" not in files:
|
| 69 |
print(f"{adapter_repo} is not a valid PEFT adapter (missing adapter_config.json)")
|
| 70 |
-
continue
|
| 71 |
except Exception as e:
|
| 72 |
-
print(f"Failed to inspect {adapter_repo}: {e}")
|
| 73 |
continue
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 77 |
merged_model.to(device)
|
| 78 |
merged_model.eval()
|
|
@@ -81,12 +107,22 @@ for cfg in CONFIGS:
|
|
| 81 |
merged_model.save_pretrained(td)
|
| 82 |
tokenizer.save_pretrained(td)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
hf_lm = HFLM(
|
| 85 |
pretrained=td,
|
| 86 |
batch_size=8 if not is_encoder else 16,
|
| 87 |
device=device,
|
| 88 |
)
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
meta = {
|
| 92 |
"model_id": adapter_repo,
|
|
@@ -117,15 +153,12 @@ with tempfile.TemporaryDirectory() as tmp:
|
|
| 117 |
)
|
| 118 |
df_existing = pd.read_parquet(current_path)
|
| 119 |
df_combined = pd.concat([df_existing, df_new], ignore_index=True)
|
| 120 |
-
|
| 121 |
df_combined = df_combined.sort_values("run_date")
|
| 122 |
-
|
| 123 |
-
|
| 124 |
df_combined["value"] = pd.to_numeric(df_combined["value"], errors="coerce")
|
| 125 |
|
| 126 |
print("Existing rows:", len(df_existing))
|
| 127 |
print("New rows:", len(df_new))
|
| 128 |
-
print("Combined
|
| 129 |
print("Final rows (after dedup):", len(df_combined))
|
| 130 |
|
| 131 |
out = Path("peft_bench.parquet")
|
|
|
|
| 34 |
DATASET_REPO = os.environ["HF_DATASET_REPO"]
|
| 35 |
api = HfApi()
|
| 36 |
|
|
|
|
|
|
|
| 37 |
METRICS_TO_KEEP = {"acc", "accuracy", "acc_stderr", "f1", "exact_match"}
|
| 38 |
+
all_rows = []
|
| 39 |
+
|
| 40 |
+
# βββββ Safe tokenizer loading βββββ
|
| 41 |
+
def load_tokenizer(model_id: str):
|
| 42 |
+
try:
|
| 43 |
+
return AutoTokenizer.from_pretrained(model_id, use_fast=True)
|
| 44 |
+
except Exception as e1:
|
| 45 |
+
print(f"Fast tokenizer failed for {model_id}: {e1}")
|
| 46 |
+
try:
|
| 47 |
+
return AutoTokenizer.from_pretrained(model_id, use_fast=False)
|
| 48 |
+
except Exception as e2:
|
| 49 |
+
raise RuntimeError(f"Failed to load tokenizer for {model_id}: {e2}") from e2
|
| 50 |
|
| 51 |
+
# βββββ Evaluate each adapter βββββ
|
| 52 |
for cfg in CONFIGS:
|
| 53 |
base_model_id = cfg["base_model"]
|
| 54 |
adapter_repo = cfg["adapter_repo"]
|
|
|
|
| 56 |
tasks = cfg["tasks"]
|
| 57 |
|
| 58 |
print(f"\nLoading base model: {base_model_id}")
|
| 59 |
+
tokenizer = load_tokenizer(base_model_id)
|
| 60 |
+
|
| 61 |
+
if "llama" in base_model_id.lower():
|
| 62 |
+
try:
|
| 63 |
+
tokenizer.legacy = False
|
| 64 |
+
except:
|
| 65 |
+
pass
|
| 66 |
|
|
|
|
| 67 |
try:
|
| 68 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 69 |
base_model_id,
|
| 70 |
trust_remote_code=True,
|
| 71 |
use_safetensors=True
|
| 72 |
+
)
|
| 73 |
is_encoder = False
|
| 74 |
+
print("Loaded as Causal LM")
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print(f"β οΈ Failed to load causal LM: {e}")
|
| 77 |
base_model = AutoModelForSequenceClassification.from_pretrained(
|
| 78 |
base_model_id,
|
| 79 |
trust_remote_code=True,
|
| 80 |
use_safetensors=True
|
| 81 |
)
|
| 82 |
is_encoder = True
|
| 83 |
+
print("Loaded as Sequence Classification model")
|
| 84 |
+
|
| 85 |
try:
|
| 86 |
info = model_info(adapter_repo)
|
| 87 |
files = [f.rfilename for f in info.siblings]
|
| 88 |
if "adapter_config.json" not in files:
|
| 89 |
print(f"{adapter_repo} is not a valid PEFT adapter (missing adapter_config.json)")
|
| 90 |
+
continue
|
| 91 |
except Exception as e:
|
| 92 |
+
print(f"Failed to inspect adapter {adapter_repo}: {e}")
|
| 93 |
continue
|
| 94 |
+
|
| 95 |
+
try:
|
| 96 |
+
peft_model = PeftModel.from_pretrained(base_model, adapter_repo)
|
| 97 |
+
merged_model = peft_model.merge_and_unload()
|
| 98 |
+
except Exception as e:
|
| 99 |
+
print(f"Failed to apply adapter {adapter_repo}: {e}")
|
| 100 |
+
continue
|
| 101 |
+
|
| 102 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 103 |
merged_model.to(device)
|
| 104 |
merged_model.eval()
|
|
|
|
| 107 |
merged_model.save_pretrained(td)
|
| 108 |
tokenizer.save_pretrained(td)
|
| 109 |
|
| 110 |
+
# Verify tokenizer object
|
| 111 |
+
if not hasattr(tokenizer, "vocab_size"):
|
| 112 |
+
print("Invalid tokenizer loaded. Skipping.")
|
| 113 |
+
continue
|
| 114 |
+
|
| 115 |
hf_lm = HFLM(
|
| 116 |
pretrained=td,
|
| 117 |
batch_size=8 if not is_encoder else 16,
|
| 118 |
device=device,
|
| 119 |
)
|
| 120 |
+
|
| 121 |
+
try:
|
| 122 |
+
res = evaluator.simple_evaluate(model=hf_lm, tasks=tasks)
|
| 123 |
+
except Exception as e:
|
| 124 |
+
print(f"Evaluation failed for {adapter_repo}: {e}")
|
| 125 |
+
continue
|
| 126 |
|
| 127 |
meta = {
|
| 128 |
"model_id": adapter_repo,
|
|
|
|
| 153 |
)
|
| 154 |
df_existing = pd.read_parquet(current_path)
|
| 155 |
df_combined = pd.concat([df_existing, df_new], ignore_index=True)
|
|
|
|
| 156 |
df_combined = df_combined.sort_values("run_date")
|
|
|
|
|
|
|
| 157 |
df_combined["value"] = pd.to_numeric(df_combined["value"], errors="coerce")
|
| 158 |
|
| 159 |
print("Existing rows:", len(df_existing))
|
| 160 |
print("New rows:", len(df_new))
|
| 161 |
+
print("Combined (pre-dedup):", len(df_existing) + len(df_new))
|
| 162 |
print("Final rows (after dedup):", len(df_combined))
|
| 163 |
|
| 164 |
out = Path("peft_bench.parquet")
|