Spaces:
Runtime error
Runtime error
File size: 2,582 Bytes
1cf9214 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import os
import glob
import re
import json
import torch
import torch.utils.data
from transformers import AutoTokenizer, AutoModel
from tqdm import tqdm
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True).bfloat16().cuda()
choices = ["A", "B", "C", "D"]
choice_tokens = [tokenizer.encode(choice, add_special_tokens=False)[0] for choice in choices]
def build_prompt(text):
return "[Round {}]\n\n้ฎ๏ผ{}\n\n็ญ๏ผ".format(1, text)
extraction_prompt = '็ปผไธๆ่ฟฐ๏ผABCDไธญๆญฃ็กฎ็้้กนๆฏ๏ผ'
accuracy_dict, count_dict = {}, {}
with torch.no_grad():
for entry in glob.glob("./CEval/val/**/*.jsonl", recursive=True):
dataset = []
with open(entry, encoding='utf-8') as file:
for line in file:
dataset.append(json.loads(line))
correct = 0
dataloader = torch.utils.data.DataLoader(dataset, batch_size=8)
for batch in tqdm(dataloader):
texts = batch["inputs_pretokenized"]
queries = [build_prompt(query) for query in texts]
inputs = tokenizer(queries, padding=True, return_tensors="pt", truncation=True, max_length=2048).to('cuda')
outputs = model.generate(**inputs, do_sample=False, max_new_tokens=512)
intermediate_outputs = []
for idx in range(len(outputs)):
output = outputs.tolist()[idx][len(inputs["input_ids"][idx]):]
response = tokenizer.decode(output)
intermediate_outputs.append(response)
answer_texts = [text + intermediate + "\n" + extraction_prompt for text, intermediate in
zip(texts, intermediate_outputs)]
input_tokens = [build_prompt(answer_text) for answer_text in answer_texts]
inputs = tokenizer(input_tokens, padding=True, return_tensors="pt", truncation=True, max_length=2048).to('cuda')
outputs = model(**inputs, return_last_logit=True)
logits = outputs.logits[:, -1]
logits = logits[:, choice_tokens]
preds = logits.argmax(dim=-1)
correct += (preds.cpu() == batch["label"]).sum().item()
accuracy = correct / len(dataset)
print(entry, accuracy)
accuracy_dict[entry] = accuracy
count_dict[entry] = len(dataset)
acc_total, count_total = 0.0, 0
for key in accuracy_dict:
acc_total += accuracy_dict[key] * count_dict[key]
count_total += count_dict[key]
print(acc_total / count_total) |