-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinference.py
172 lines (148 loc) · 5.62 KB
/
inference.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import json
import sys
import os
import fire
import torch
import time
import transformers
import numpy as np
from typing import List
from peft.peft_model import set_peft_model_state_dict
from loraprune.peft_model import get_peft_model
from loraprune.utils import freeze, prune_from_checkpoint
from loraprune.lora import LoraConfig
from datasets import load_dataset
from transformers import AutoModelForCausalLM, AutoTokenizer
if torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
try:
if torch.backends.mps.is_available():
device = "mps"
except: # noqa: E722
pass
def main(
base_model: str = "",
lora_r: int = 8,
lora_alpha: int = 16,
lora_dropout: float = 0.,
lora_target_modules: List[str] = [
"o_proj",
"gate_proj",
"down_proj",
"up_proj"
],
lora_weights: str = "tloen/alpaca-lora-7b",
cutoff_len: int = 128
):
assert (
base_model
), "Please specify a --base_model, e.g. --base_model='decapoda-research/llama-7b-hf'"
tokenizer = AutoTokenizer.from_pretrained(base_model, legacy=False)
model = AutoModelForCausalLM.from_pretrained(
base_model,
load_in_8bit=False,
torch_dtype=torch.float16,
device_map='auto',
)
config = LoraConfig(
r=lora_r,
lora_alpha=lora_alpha,
target_modules=lora_target_modules,
lora_dropout=lora_dropout,
bias="none",
task_type="CAUSAL_LM",
)
model = get_peft_model(model, config)
if lora_weights:
# Check the available weights and load them
checkpoint_name = os.path.join(
lora_weights, "pytorch_model.bin"
) # Full checkpoint
if not os.path.exists(checkpoint_name):
checkpoint_name = os.path.join(
lora_weights, "adapter_model.bin"
) # only LoRA model - LoRA config above has to fit
resume_from_checkpoint = (
False # So the trainer won't try loading its state
)
# The two files above have a different name depending on how they were saved, but are actually the same.
if os.path.exists(checkpoint_name):
print(f"Restarting from {checkpoint_name}")
adapters_weights = torch.load(checkpoint_name)
for name, param in adapters_weights.items():
if 'lora_mask' in name:
adapters_weights[name] = param.reshape(-1)
model = set_peft_model_state_dict(model, adapters_weights)
else:
print(f"Checkpoint {checkpoint_name} not found")
model = model.to(device)
freeze(model)
prune_from_checkpoint(model)
# unwind broken decapoda-research config
model.config.pad_token_id = tokenizer.pad_token_id = 0 # unk
model.config.bos_token_id = 1
model.config.eos_token_id = 2
model.half() # seems to fix bugs for some users.
model.eval()
# if torch.__version__ >= "2" and sys.platform != "win32":
# model = torch.compile(model)
from torch.utils.data.dataset import Dataset
times = []
class IndexDataset(Dataset):
def __init__(self, tensors):
self.tensors = tensors
def __getitem__(self, index):
return self.tensors[index]
def __len__(self):
return len(self.tensors)
def process_data(samples, tokenizer, seq_len, field_name):
test_ids = tokenizer("\n\n".join(samples[field_name]), return_tensors='pt').input_ids[0]
test_ids_batch = []
nsamples = test_ids.numel() // seq_len
for i in range(nsamples):
batch = test_ids[(i * seq_len):((i + 1) * seq_len)]
test_ids_batch.append(batch)
test_ids_batch = torch.stack(test_ids_batch)
return IndexDataset(tensors=test_ids_batch)
def PPLMetric(model, loader, device="cuda"):
ppl = llama_eval(model, loader, device)
print(ppl)
return ppl
@torch.no_grad()
def llama_eval(model, loader, device):
model.eval()
nlls = []
n_samples = 0
for batch in loader:
batch = batch.to(device)
with torch.cuda.amp.autocast():
t1 = time.time()
output = model(batch)
times.append(time.time() - t1)
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()
eval_data = load_dataset('wikitext', 'wikitext-2-raw-v1', split='test')
test_dataset = process_data(eval_data, tokenizer, cutoff_len, 'text')
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False)
results = PPLMetric(model, loader=test_loader)
times = np.mean(times)
print("wikitext2 ppl:{:.2f} inference time:{:2f}".format(results, times))
times = []
eval_data = load_dataset('ptb_text_only', 'penn_treebank', split='validation', trust_remote_code=True)
test_dataset = process_data(eval_data, tokenizer, cutoff_len, 'sentence')
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False)
results = PPLMetric(model, loader=test_loader)
times = np.mean(times)
print("PTB ppl:{:.2f} inference time:{:2f}".format(results, times))
return
if __name__ == "__main__":
fire.Fire(main)