| | import torch
|
| | from transformers import AutoTokenizer, AutoModelForCausalLM
|
| | from datasets import load_dataset
|
| | from tqdm import tqdm
|
| | import json
|
| | import csv
|
| | import os
|
| | import evaluate
|
| |
|
| |
|
| | MODEL_PATH = "../train/output/qlora-codellama-bugfix"
|
| | EVAL_FILE = "eval.jsonl"
|
| | OUTPUT_JSON = "./output/eval_results.json"
|
| | OUTPUT_CSV = "./output/eval_results.csv"
|
| | MAX_INPUT_LEN = 1024
|
| | MAX_NEW_TOKENS = 256
|
| | DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| |
|
| |
|
| | os.makedirs(os.path.dirname(OUTPUT_JSON), exist_ok=True)
|
| |
|
| |
|
| | print("π Loading model...")
|
| | tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| | model = AutoModelForCausalLM.from_pretrained(
|
| | MODEL_PATH,
|
| | torch_dtype=torch.bfloat16,
|
| | device_map="auto"
|
| | )
|
| | model.eval()
|
| |
|
| |
|
| | print("π Loading evaluation data...")
|
| | eval_data = load_dataset("json", data_files=EVAL_FILE, split="train")
|
| |
|
| |
|
| | results = []
|
| | print("βοΈ Running inference...")
|
| | for example in tqdm(eval_data):
|
| | prompt = example["prompt"]
|
| | reference = example["completion"]
|
| |
|
| | inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=MAX_INPUT_LEN).to(DEVICE)
|
| |
|
| | with torch.no_grad():
|
| | outputs = model.generate(
|
| | **inputs,
|
| | max_new_tokens=MAX_NEW_TOKENS,
|
| | do_sample=False,
|
| | num_beams=4,
|
| | pad_token_id=tokenizer.pad_token_id,
|
| | eos_token_id=tokenizer.eos_token_id
|
| | )
|
| |
|
| | prediction = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| |
|
| | results.append({
|
| | "prompt": prompt,
|
| | "reference": reference.strip(),
|
| | "prediction": prediction.strip()
|
| | })
|
| |
|
| |
|
| | with open(OUTPUT_JSON, "w", encoding="utf-8") as f:
|
| | json.dump(results, f, indent=2)
|
| | print(f"β
Saved JSON to {OUTPUT_JSON}")
|
| |
|
| | with open(OUTPUT_CSV, "w", encoding="utf-8", newline='') as f:
|
| | writer = csv.DictWriter(f, fieldnames=["prompt", "reference", "prediction"])
|
| | writer.writeheader()
|
| | writer.writerows(results)
|
| | print(f"β
Saved CSV to {OUTPUT_CSV}")
|
| |
|
| |
|
| | print("π Computing BLEU and ROUGE...")
|
| | bleu = evaluate.load("bleu")
|
| | rouge = evaluate.load("rouge")
|
| |
|
| | predictions = [r["prediction"] for r in results]
|
| | references = [r["reference"] for r in results]
|
| |
|
| | bleu_score = bleu.compute(predictions=predictions, references=[[ref] for ref in references])
|
| | rouge_score = rouge.compute(predictions=predictions, references=references)
|
| |
|
| | print("\nπ Evaluation Results:")
|
| | print("BLEU:", bleu_score)
|
| | print("ROUGE:", json.dumps(rouge_score, indent=2))
|
| |
|