forked from aim-uofa/LoRAPrune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
213 lines (178 loc) · 6.64 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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 peft import LoraConfig
from datasets import load_from_disk
from dataset_types import MedicalReport
from transformers import LlamaForCausalLM, LlamaTokenizer
from lmformatenforcer import JsonSchemaParser
from lmformatenforcer.integrations.transformers import build_transformers_prefix_allowed_tokens_fn
from transformers import pipeline
from tqdm import tqdm
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 = "",
dataset: 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'"
assert (
dataset
), "Please specify a --dataset, e.g. --dataset='wikitext'"
tokenizer = LlamaTokenizer.from_pretrained(base_model)
model = LlamaForCausalLM.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",
)
hf_pipeline = pipeline('text-generation', model=model, tokenizer=tokenizer, device_map='auto')
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()
#########################################################
# Run on dataset
#########################################################
data = load_from_disk(dataset)
data = data['eval']
hf_pipeline.model = model
results = []
# Process data in batches
for i, sample in tqdm(enumerate(data), desc="Processing Batches"):
prompt = """
Extract the medical report information into the following model:
{schema}
If something is not clear, or incomplete, leave it blank.
### INPUT:
{instruction}
### RESPONSE:
""".format(
schema=MedicalReport.schema(),
instruction=sample["instruction"]
)
# Create a character level parser and build a transformers prefix function
parser = JsonSchemaParser(MedicalReport.schema())
prefix_function = build_transformers_prefix_allowed_tokens_fn(hf_pipeline.tokenizer, parser)
# Process batch
outputs = hf_pipeline(prompt, prefix_allowed_tokens_fn=prefix_function, max_length=4096)
# Extract results
result = outputs[0]['generated_text'][len(prompt):]
medical_report = MedicalReport.model_validate_json(result)
if not medical_report.is_valid():
print(f"Invalid medical report: {medical_report}")
results.append({str(i): medical_report.model_dump_json()})
with open('medical_report_output.json', 'w') as f:
json.dump(results, f)
return
if __name__ == "__main__":
fire.Fire(main)