-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate.py
153 lines (127 loc) · 4.41 KB
/
evaluate.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import torch
import os
os.environ['CUDA_VISIBLE_DEVICES']="0"
import sys
sys.path.append('../')
import numpy as np
import pandas as pd
import torch.nn as nn
from torch.utils.data import DataLoader
import transformers
from transformers import (
AutoModelForCausalLM,
BitsAndBytesConfig,
AutoTokenizer,
TrainingArguments,
T5ForConditionalGeneration,
DataCollatorForSeq2Seq,
)
from datasets import load_dataset
from preprocess import get_combination
from preprocess import get_bookcorpus
import argparse
from tqdm import tqdm
from layers import ModuleInjection
from lm_eval import evaluator
from preprocess import *
import json
import time
def tokenize(prompt, add_eos_token=True):
result = tokenizer(
prompt,
truncation=True,
max_length=2048,
padding=False,
return_tensors=None,
)
if (
result["input_ids"][-1] != tokenizer.eos_token_id
and len(result["input_ids"]) < 2048
and add_eos_token
):
result["input_ids"].append(tokenizer.eos_token_id)
result["attention_mask"].append(1)
result["labels"] = result["input_ids"].copy()
return result
def generate_and_tokenize_prompt(data_point):
full_prompt = data_point["text"]
tokenized_full_prompt = tokenize(full_prompt)
return tokenized_full_prompt
def evaluate(args):
base_model = torch.load(args.load_path).to(torch.device('cuda'))
ppl = PPLMetric(base_model, tokenizer = tokenizer, datasets = ['wikitext2','ptb'], seq_len = 128, batch_size = 4, device = 'cuda' )
print(ppl)
results = evaluator.simple_evaluate(
model=base_model,
tasks= ['piqa', 'boolq', 'arc_challenge', 'winogrande', 'hellaswag'],
# tasks= ["hellaswag"],
num_fewshot=args.shots,
batch_size="auto",
max_batch_size=8,
device="cuda:0",
no_cache=True,
)
datasets = list(results['results'].keys())
acc = []
acc_norm =[]
for dataset in datasets:
acc.append(results['results'][dataset]['acc'])
if("acc_norm" in results['results'][dataset].keys()):
acc_norm.append(results['results'][dataset]['acc_norm'])
else :
acc_norm.append(-1)
datasets.append("Average")
acc.append(np.mean(np.array(acc)))
acc_norm.append(-1)
datasets.append("Perplexity")
acc.append(ppl['wikitext2'])
acc_norm.append(-1)
datasets.append("Perplexity")
acc.append(ppl['ptb'])
acc_norm.append(-1)
x = pd.DataFrame({'datasets' : datasets, 'acc' : acc, 'acc_norm' : acc_norm})
x.to_csv(args.log_path, index = False)
print("Complete")
######################################
### PPL evaluation ###
import torch
import numpy as np
from tqdm import tqdm
from dataset_ppl import get_loaders
def PPLMetric(model, tokenizer, datasets, seq_len=128, batch_size = 4, device="cuda"):
metric = {}
for dataset in datasets:
_, test_loader = get_loaders(dataset, tokenizer, seq_len=seq_len, batch_size = batch_size)
ppl = llama_eval(model, test_loader, device)
metric[dataset] = ppl
print(metric)
return metric
@torch.no_grad()
def llama_eval(model, test_lodaer, device):
nlls = []
n_samples = 0
for batch in tqdm(test_lodaer):
batch = batch.to(device)
output = model(batch)
lm_logits = output.logits
shift_logits = lm_logits[:, :-1, :].contiguous()
shift_labels = batch[:, 1:].contiguous()
loss_fct = torch.nn.CrossEntropyLoss(reduction="none")
loss = loss_fct(shift_logits.reshape(-1, shift_logits.size(-1)), shift_labels.view(-1))
nlls.append(loss)
#print(torch.cat(nlls, dim=-1).mean())
ppl = np.exp(torch.cat(nlls, dim=-1).mean().item())
return ppl.item()
#############################################
parser = argparse.ArgumentParser("main")
parser.add_argument("--log_path", type=str, default="compressed_evaluate.csv")
parser.add_argument("--load_path", type=str, default="/home/wolfi/PTC/latest.pt")
parser.add_argument("--model", type=str, default="mistralai/Mistral-7B-v0.1")
parser.add_argument("--shots", type=int, default=0)
args = parser.parse_args()
tokenizer = AutoTokenizer.from_pretrained(
args.model,
trust_remote_code=True,
torch_dtype="auto",
)
evaluate(args)