-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot_activations.py
347 lines (283 loc) · 11 KB
/
plot_activations.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import os
import argparse
import functools
import matplotlib
matplotlib.use('agg')
import matplotlib.style as mplstyle
mplstyle.use('fast')
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm
import torch
import torch.nn as nn
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
AutoConfig,
LlamaForCausalLM,
OPTForCausalLM,
MistralForCausalLM,
)
from transformers.models.llama.modeling_llama import LlamaDecoderLayer
from transformers.models.opt.modeling_opt import OPTDecoderLayer
from transformers.models.mistral.modeling_mistral import MistralDecoderLayer
from fastchat.model.model_adapter import get_conversation_template
from utils.sharegpt import make_sharegpt_data_module
from utils.modelutils import build_tokenizer
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--fp16_model_path', type=str,
default='facebook/opt-1.3b', help='model name')
parser.add_argument('--output_path', type=str, default='./outputs/visualizations',
help='where to save the visualization results')
parser.add_argument('--dataset_path', type=str,
default="./datasets/ShareGPT_Vicuna_unfiltered/ShareGPT_V4.3_unfiltered_cleaned_split.json")
parser.add_argument('--num_tokens', type=int, default=128)
args = parser.parse_args()
return args
@torch.no_grad()
def get_act(args, model, dataset, tokenizer):
model.eval()
device = next(model.parameters()).device
abs_acts = {}
if isinstance(model, LlamaForCausalLM):
target_layer_type = LlamaDecoderLayer
elif isinstance(model, OPTForCausalLM):
target_layer_type = OPTDecoderLayer
elif isinstance(model, MistralForCausalLM):
target_layer_type = MistralDecoderLayer
else:
raise NotImplementedError
def stat_tensor(name, tensor):
hidden_dim = tensor.shape[-1]
tensor = tensor.view(-1, hidden_dim).abs().detach()
if name in abs_acts:
abs_acts[name] = torch.cat([abs_acts[name], tensor.cpu()], dim=0)
else:
abs_acts[name] = tensor.cpu()
def stat_input_hook(m, x, y, name):
if isinstance(x, tuple):
x = x[0]
if isinstance(y, tuple):
y = y[0]
stat_tensor(name, y)
hooks = []
for name, m in model.named_modules():
if isinstance(m, target_layer_type):
hooks.append(
m.register_forward_hook(
functools.partial(stat_input_hook, name=name))
)
i = 0
tokens = 0
while tokens < args.num_tokens:
max_length = args.num_tokens - tokens
input_ids = dataset[i]["input_ids"][:, :max_length].to(device)
tokens += input_ids.shape[1]
model(input_ids, output_attentions=True)
i += 1
for h in hooks:
h.remove()
return abs_acts
@torch.no_grad()
def get_attn(args, model, dataset):
model.eval()
device = next(model.parameters()).device
attns_list = []
for data in tqdm(dataset):
input_ids = data["input_ids"][:, :args.num_tokens].to(device)
output = model(input_ids, output_attentions=True)
attns_list.append(output.attentions)
attns_sum_pooling = [None] * model.config.num_hidden_layers
for layer in range(model.config.num_hidden_layers):
attns_sum_pooling[layer] = torch.cat([attns[layer].sum(dim=1) for attns in attns_list], dim=0).mean(0).cpu().numpy()
return attns_sum_pooling
@torch.no_grad()
def get_kv_act(args, model, dataset):
model.eval()
device = next(model.parameters()).device
acts = {}
target_layer_type = nn.Linear
target_layer_names = ["k_proj", "v_proj"]
def stat_tensor(name, tensor):
hidden_dim = tensor.shape[-1]
tensor = tensor.view(-1, hidden_dim).detach()
if name in acts:
acts[name] = torch.cat([acts[name], tensor.cpu()], dim=0)
else:
acts[name] = tensor.cpu()
def stat_input_hook(m, x, y, name):
if isinstance(x, tuple):
x = x[0]
if isinstance(y, tuple):
y = y[0]
stat_tensor(name, y)
hooks = []
for name, m in model.named_modules():
if isinstance(m, target_layer_type) and \
any([target_layer_name in name for target_layer_name in target_layer_names]):
hooks.append(
m.register_forward_hook(
functools.partial(stat_input_hook, name=name))
)
i = 0
tokens = 0
while tokens < args.num_tokens:
max_length = args.num_tokens - tokens
input_ids = dataset[i]["input_ids"][:, :max_length].to(device)
tokens += input_ids.shape[1]
model(input_ids, output_attentions=True)
i += 1
for h in hooks:
h.remove()
return acts
def plot_activations(args, name, abs_act: torch.Tensor):
# set up the figure and axes
fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')
# plot activation
n_tokens, hidden_dim = abs_act.shape
x = np.arange(1, hidden_dim + 1)
y = np.arange(1, n_tokens + 1)
X, Y = np.meshgrid(x, y)
surf = ax1.plot_surface(X, Y, abs_act, cmap='coolwarm',
rstride=1, cstride=1, linewidth=0.5,
antialiased=True, zorder=1)
# fig.colorbar(surf, ax=ax1, shrink=0.5, aspect=10)
# plot separation line between prompt & query
if "vicuna" in args.fp16_model_path:
margin = 500
x = np.arange(-margin, hidden_dim + margin)
y = np.zeros_like(x) + args.prompt_len - 1
z = np.zeros_like(x)
ax1.plot(x, y, z, "r--", zorder=10)
# ax1.set_title(f'Layer {name.split(".")[-1]}')
ax1.set_xlabel('Channel')
ax1.set_ylabel('Token')
# ax1.set_zlabel('Absolute Value')
jpg_path = os.path.join(args.output_path, f'{name}.jpg')
plt.savefig(jpg_path, bbox_inches='tight', pad_inches=0, dpi=800)
plt.close()
# convert jpg to pdf
import img2pdf
pdf_path = os.path.join(args.output_path, f'{name}.pdf')
with open(pdf_path, "wb") as f:
f.write(img2pdf.convert(jpg_path))
os.remove(jpg_path)
def plot_attn_map(args, attns):
for layer in tqdm(range(0, len(attns), 8)):
# set up the figure and axes
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_facecolor("grey")
# plot attn map
attn = attns[layer]
attn_mask = np.triu(np.ones_like(attn), k=1)
attn[attn_mask == 1] = np.nan
im = ax1.imshow(attn, cmap='coolwarm')
# llama-2-7B
# ax1.set_xticks([0, 12])
# ax1.set_xticklabels(["[BOS]", "."])
# llama-30B
# ax1.set_xticks([0, 27])
# ax1.set_xticklabels(["[BOS]", "'"])
# ax1.set_xticks(range(args.num_tokens))
# ax1.set_xticklabels(args.input_tokens, rotation=80, fontsize=3.5)
plt.colorbar(im)
# plot separation line
if "vicuna" in args.fp16_model_path:
ax1.axvline(args.prompt_len, linestyle="--", color="r")
# ax1.set_title(f'Layer {layer}')
plt.savefig(os.path.join(args.output_path, f'layer.{layer}.pdf'), bbox_inches='tight', pad_inches=0.0)
plt.close()
def calc_smoothness(x):
dim = x.shape[-1]
x = x.reshape(-1, dim)
x_std = x.std(dim=-1, keepdim=True)
x_std = x_std.squeeze()
absmax = x.abs().max(-1)[0]
return absmax.mean(), x_std.mean()
@torch.no_grad()
def main(args):
# build model
config = AutoConfig.from_pretrained(args.fp16_model_path)
model = AutoModelForCausalLM.from_pretrained(
args.fp16_model_path,
config=config,
device_map="auto",
torch_dtype=torch.float16,
)
num_hidden_layers = model.config.num_hidden_layers
tokenizer = build_tokenizer(args, model)
# system prompt
context = get_conversation_template("vicuna")
context.append_message(context.roles[0], None)
prompt = context.get_prompt()
prompt_ids = tokenizer(prompt, return_tensors='pt').input_ids
args.prompt_len = prompt_ids.shape[-1]
# dataset
args.max_seq_len = 2048
args.max_train_samples = 1
dataset = make_sharegpt_data_module(args, tokenizer, min_gpt_len=args.num_tokens, seed=1)
# get activation
args.num_tokens = 128
abs_acts = get_act(args, model, dataset, tokenizer)
# get attn
args.num_tokens = 64 # for better visualization
args.input_tokens = tokenizer.convert_ids_to_tokens(dataset[0]["input_ids"].squeeze()[:args.num_tokens])
attns = get_attn(args, model, dataset)
# get kv activation
args.num_tokens = 1024
kv_acts = get_kv_act(args, model, dataset)
del model
torch.cuda.empty_cache()
# output path
model_name = args.fp16_model_path.split("/")[-1]
output_path = args.output_path
# plot attn map
args.output_path = os.path.join(output_path, model_name, "attn")
os.makedirs(args.output_path, exist_ok=True)
plot_attn_map(args, attns)
# plot activations
args.output_path = os.path.join(output_path, model_name, "act")
os.makedirs(args.output_path, exist_ok=True)
for name, abs_act in tqdm(abs_acts.items()):
if int(name.split(".")[-1]) in range(0, num_hidden_layers, 8):
plot_activations(args, name, abs_act)
# smoothness of kv cache
if "llama-7b" in model_name:
pivot_token_ids = [0]
elif "llama-13b" in model_name:
pivot_token_ids = [0]
elif "llama-2-7b" in model_name:
pivot_token_ids = [0, 12]
elif "llama-2-13b" in model_name:
pivot_token_ids = [0]
elif "llama-3-8b" in model_name:
pivot_token_ids = [0]
else:
return
for name in ["k", "v"]:
pivot_range_list, pivot_std_list = [], []
non_pivot_range_list, non_pivot_std_list = [], []
for layer in range(config.num_hidden_layers):
acts = kv_acts[f"model.layers.{layer}.self_attn.{name}_proj"]
pivot_mask = np.array([False] * acts.shape[0])
pivot_mask[pivot_token_ids] = True
pivot_acts = acts[pivot_mask, :]
non_pivot_acts = acts[~pivot_mask, :]
pivot_absmax, pivot_std = calc_smoothness(pivot_acts)
non_pivot_absmax, non_pivot_std = calc_smoothness(non_pivot_acts)
pivot_range_list.append(pivot_absmax)
pivot_std_list.append(pivot_std)
non_pivot_range_list.append(non_pivot_absmax)
non_pivot_std_list.append(non_pivot_std)
pivot_absmax = sum(pivot_range_list) / len(pivot_range_list)
pivot_std = sum(pivot_std_list) / len(pivot_std_list)
non_pivot_absmax = sum(non_pivot_range_list) / len(non_pivot_range_list)
non_pivot_std = sum(non_pivot_std_list) / len(non_pivot_std_list)
print(f"pivot tokens {name} cache -- absmax/std: {pivot_absmax:.2f}/{pivot_std:.2f}\n"
f"non-pivot tokens {name} cache -- absmax/std: {non_pivot_absmax:.2f}/{non_pivot_std:.2f}")
if __name__ == '__main__':
args = parse_args()
main(args)