Upload gen2.py with huggingface_hub
Browse files
gen2.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Re-generate eval outputs from the MERGED model with safer decoding (longer cap, stop at </svg>, anti-repetition)."""
|
| 2 |
+
import os, json, time, torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
from huggingface_hub import hf_hub_download, HfApi
|
| 5 |
+
TOKEN = os.environ["HF_TOKEN"]; REPO = "aixonlab/svg-slm-4b"
|
| 6 |
+
BS = int(os.environ.get("GEN_BS", 30)); MAXNEW = int(os.environ.get("MAXNEW", 6000)); LIMIT = float(os.environ.get("GEN_SECONDS", 1800))
|
| 7 |
+
t0 = time.time()
|
| 8 |
+
prompts = json.load(open(hf_hub_download("aixonlab/svg-slm-scripts", "eval_prompts.json", token=TOKEN)))
|
| 9 |
+
tok = AutoTokenizer.from_pretrained(REPO, token=TOKEN); tok.padding_side = "left"
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(REPO, torch_dtype=torch.bfloat16, token=TOKEN, device_map={"": 0}).eval()
|
| 11 |
+
out = {}
|
| 12 |
+
for i in range(0, len(prompts), BS):
|
| 13 |
+
if time.time() - t0 > LIMIT: print("GEN TIME LIMIT", flush=True); break
|
| 14 |
+
batch = prompts[i:i + BS]
|
| 15 |
+
texts = []
|
| 16 |
+
for p in batch:
|
| 17 |
+
m = [{"role": "user", "content": p["prompt"]}]
|
| 18 |
+
try: texts.append(tok.apply_chat_template(m, tokenize=False, add_generation_prompt=True, enable_thinking=False))
|
| 19 |
+
except TypeError: texts.append(tok.apply_chat_template(m, tokenize=False, add_generation_prompt=True))
|
| 20 |
+
enc = tok(texts, return_tensors="pt", padding=True, add_special_tokens=False).to("cuda")
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
g = model.generate(**enc, max_new_tokens=MAXNEW, do_sample=True, temperature=0.4, top_p=0.9,
|
| 23 |
+
repetition_penalty=1.06, stop_strings=["</svg>"], tokenizer=tok, pad_token_id=tok.pad_token_id)
|
| 24 |
+
for p, seq in zip(batch, g): out[p["id"]] = tok.decode(seq[enc["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 25 |
+
print("generated", len(out), f"{time.time()-t0:.0f}s", flush=True)
|
| 26 |
+
json.dump(out, open("/workspace/eval_outputs_v2.json", "w"))
|
| 27 |
+
HfApi(token=TOKEN).upload_file(path_or_fileobj="/workspace/eval_outputs_v2.json", path_in_repo="eval_outputs_v2.json", repo_id="aixonlab/svg-slm-4b-lora")
|
| 28 |
+
print("GEN2 DONE", flush=True)
|