-
Notifications
You must be signed in to change notification settings - Fork 4
/
eval.py
188 lines (139 loc) · 6.08 KB
/
eval.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
# Copyright (c) 2023 Qualcomm Technologies, Inc.
# All Rights Reserved.
import math
import torch
from torch import nn
torch.backends.cuda.matmul.allow_tf32 = False
torch.backends.cudnn.allow_tf32 = False
@torch.no_grad()
def eval_model(model, model_str, enc, dev="cuda"):
model.eval()
use_cache = model.config.use_cache
model.config.use_cache = False
enc = enc.input_ids
enc = enc.to(dev)
nsamples = enc.numel() // model.seqlen
losses = 0.0
for i in range(nsamples):
if (i % 10) == 0:
print(f"\tPass {i+1} of {nsamples}")
batch = enc[:, (i * model.seqlen) : ((i + 1) * model.seqlen)].to(dev)
out = model(batch)
if "logits" in out.keys():
logits = out["logits"]
else:
raise ValueError(f"Unknown model out keys:", out.keys())
loss = nn.CrossEntropyLoss()
L = loss(logits[:, :-1, :].view(-1, logits.size(-1)), batch[:, 1:].view(-1))
losses += L.item()
ppl = math.exp(losses / nsamples)
model.config.use_cache = use_cache
outdir = {}
outdir["ppl"] = ppl
return outdir
@torch.no_grad()
def eval_model_lowmem(model, model_str, enc, dev="cuda"):
model.eval()
enc = enc.input_ids
nsamples = enc.numel() // model.seqlen
use_cache = model.config.use_cache
model.config.use_cache = False
if "opt" in model_str:
layers = model.model.decoder.layers
model.model.decoder.embed_tokens = model.model.decoder.embed_tokens.to(dev)
model.model.decoder.embed_positions = model.model.decoder.embed_positions.to(dev)
if hasattr(model.model.decoder, "project_out") and model.model.decoder.project_out:
model.model.decoder.project_out = model.model.decoder.project_out.to(dev)
if hasattr(model.model.decoder, "project_in") and model.model.decoder.project_in:
model.model.decoder.project_in = model.model.decoder.project_in.to(dev)
else:
layers = model.transformer.h
model.transformer.word_embeddings = model.transformer.word_embeddings.to(dev)
model.transformer.word_embeddings_layernorm = (
model.transformer.word_embeddings_layernorm.to(dev)
)
layers[0] = layers[0].to(dev)
dtype = next(iter(model.parameters())).dtype
inps = torch.zeros((nsamples, model.seqlen, model.config.hidden_size), dtype=dtype, device=dev)
cache = {"i": 0, "attention_mask": None, "alibi": None}
class Catcher(nn.Module):
def __init__(self, module):
super().__init__()
self.module = module
def forward(self, inp, **kwargs):
inps[cache["i"]] = inp
cache["i"] += 1
cache["attention_mask"] = kwargs["attention_mask"]
if not "opt" in model_str:
cache["alibi"] = kwargs["alibi"]
raise ValueError
layers[0] = Catcher(layers[0])
for i in range(nsamples):
batch = enc[:, (i * model.seqlen) : ((i + 1) * model.seqlen)].to(dev)
try:
model(batch)
except ValueError:
pass
layers[0] = layers[0].module.cpu()
if "opt" in model_str:
model.model.decoder.embed_tokens = model.model.decoder.embed_tokens.cpu()
model.model.decoder.embed_positions = model.model.decoder.embed_positions.cpu()
if hasattr(model.model.decoder, "project_out") and model.model.decoder.project_out:
model.model.decoder.project_out = model.model.decoder.project_out.cpu()
if hasattr(model.model.decoder, "project_in") and model.model.decoder.project_in:
model.model.decoder.project_in = model.model.decoder.project_in.cpu()
torch.cuda.empty_cache()
else:
model.transformer.word_embeddings = model.transformer.word_embeddings.cpu()
model.transformer.word_embeddings_layernorm = (
model.transformer.word_embeddings_layernorm.cpu()
)
torch.cuda.empty_cache()
alibi = cache["alibi"]
outs = torch.zeros_like(inps)
attention_mask = cache["attention_mask"]
for layer_i in range(len(layers)):
layer = layers[layer_i].to(dev)
if "opt" in model_str:
for j in range(nsamples):
outs[j] = layer(inps[j].unsqueeze(0), attention_mask=attention_mask)[0]
else:
for j in range(nsamples):
outs[j] = layer(inps[j].unsqueeze(0), attention_mask=attention_mask, alibi=alibi)[0]
layers[layer_i] = layer.cpu()
del layer
torch.cuda.empty_cache()
inps, outs = outs, inps
if "opt" in model_str:
if model.model.decoder.final_layer_norm is not None:
model.model.decoder.final_layer_norm = model.model.decoder.final_layer_norm.to(dev)
if model.model.decoder.project_out is not None:
model.model.decoder.project_out = model.model.decoder.project_out.to(dev)
else:
model.transformer.ln_f = model.transformer.ln_f.to(dev)
model.lm_head = model.lm_head.to(dev)
enc = enc.to(dev)
sum_nll = 0.0
for i in range(nsamples):
hidden_states = inps[i].unsqueeze(0)
if "opt" in model_str:
if model.model.decoder.final_layer_norm is not None:
hidden_states = model.model.decoder.final_layer_norm(hidden_states)
if model.model.decoder.project_out is not None:
hidden_states = model.model.decoder.project_out(hidden_states)
else:
hidden_states = model.transformer.ln_f(hidden_states)
lm_logits = model.lm_head(hidden_states)
shift_logits = lm_logits[:, :-1, :].contiguous()
shift_labels = enc[:, (i * model.seqlen) : ((i + 1) * model.seqlen)][:, 1:]
del hidden_states
torch.cuda.empty_cache()
loss_fct = nn.CrossEntropyLoss()
loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
neg_log_likelihood = loss.float() * model.seqlen
sum_nll += neg_log_likelihood.item()
ppl = math.exp(sum_nll / (nsamples * model.seqlen))
model.config.use_cache = use_cache
outdir = {}
outdir["ppl"] = ppl
return outdir