|
import torch |
|
from peft import PeftModel, PeftConfig |
|
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer |
|
|
|
|
|
peft_model_id="finetuned_model/results" |
|
config = PeftConfig.from_pretrained(peft_model_id) |
|
|
|
|
|
model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path, load_in_8bit=True, device_map={"":0}) |
|
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path) |
|
|
|
|
|
model = PeftModel.from_pretrained(model, peft_model_id, device_map={"":0}) |
|
model.eval() |
|
|
|
print("Peft model loaded") |
|
|
|
from datasets import load_dataset |
|
from random import randrange |
|
|
|
|
|
import evaluate |
|
import numpy as np |
|
import datasets |
|
from tqdm import tqdm |
|
|
|
|
|
metric = evaluate.load("rouge") |
|
|
|
def evaluate_peft_model(sample,max_target_length=50): |
|
|
|
outputs = model.generate(input_ids=sample["input_ids"].unsqueeze(0).cuda(), do_sample=True, top_p=0.9, max_new_tokens=max_target_length) |
|
prediction = tokenizer.decode(outputs[0].detach().cpu().numpy(), skip_special_tokens=True) |
|
|
|
|
|
labels = np.where(sample['labels'] != -100, sample['labels'], tokenizer.pad_token_id) |
|
labels = tokenizer.decode(labels, skip_special_tokens=True) |
|
|
|
|
|
return prediction, labels |
|
|
|
|
|
|
|
list_input = [{"natural": "go to P03 and then go to P04, remain in P04 until P05","raw_ltl":"0"}] |
|
test_dataset = datasets.Dataset.from_list(list_input) |
|
|
|
|
|
predictions, references = [] , [] |
|
for sample in tqdm(test_dataset): |
|
p,l = evaluate_peft_model(sample) |
|
print(p,l) |
|
predictions.append(p) |
|
references.append(l) |
|
|