-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
172 lines (139 loc) · 5.98 KB
/
main.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
from argparse import ArgumentParser
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from tqdm import tqdm
import uuid
from util import top_tokens_by_layer, top_tokens, plot_attention_heads_by_layer
print("===============loading model")
precision = "bf16"
size = "1B"
model_name = f"meta-llama/Llama-3.2-{size}-Instruct"
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
device = "mps"
model = model.to(device)
def main(prompt, max_new_tokens=250, images=False, save_embeddings=False, save_attentions=False):
if save_embeddings:
print("===============setting hook")
# clear pre-existing hooks
for _, module in model.named_modules():
module._forward_hooks.clear()
module._backward_hooks.clear()
activation_sequences = []
def collect_activations():
def hook(model, input, output):
# Only capture the last token's embedding (shape: [1, 2048])
activation_sequences.append(output[0, -1].detach().cpu().numpy())
return hook
# Register hook on the final norm layer before lm_head
hook = model.model.norm.register_forward_hook(collect_activations())
print("===============setting up dirs")
output_dir = os.path.join("model_outputs", size, precision, prompt)
os.makedirs(output_dir, exist_ok=True)
os.makedirs(os.path.join(output_dir, "logits"), exist_ok=True)
print("===============setting up prompt")
# format prompt
prompt = tokenizer.apply_chat_template(
[{"role": "user", "content": prompt}],
tokenize=False,
add_generation_prompt=True,
return_tensors="pt"
)
# tokenize prompt
input_ids = tokenizer.encode(prompt, return_tensors='pt')
input_ids = input_ids.to(device)
# Store generated tokens and their logits at each intermediate generation step
generated_tokens = []
current_input_ids = input_ids
generated_token_ids = []
print("===============starting generation")
for step in tqdm(range(max_new_tokens)):
print("run model one step...")
with torch.no_grad():
outputs = model(
current_input_ids,
output_attentions=True,
return_dict=True
)
print("save top logits...")
# Get logits for the next token and find top token
next_token_logits = outputs.logits[0, -1]
next_token = next_token_logits.argmax().unsqueeze(0).unsqueeze(0)
generated_token_ids.append(next_token.item())
generated_tokens.append(next_token)
# Get top 10 tokens and their logits efficiently
top_logits, top_indices = next_token_logits.topk(10)
# Batch decode tokens
decoded_top_tokens = tokenizer.batch_decode([[i] for i in top_indices])
# Create DataFrame directly from dict
df = pd.DataFrame({
'Token ID': top_indices.cpu().numpy(),
'Token': decoded_top_tokens,
'Logit': top_logits.cpu().numpy()
})
# Save to CSV
df.to_csv(
os.path.join(output_dir, f"logits/step_{step}_top_10_logit_distribution.csv"),
escapechar='\\',
index=False
)
print("save next")
# Sample next token
next_token = torch.argmax(next_token_logits).unsqueeze(0).unsqueeze(0)
generated_tokens.append(next_token)
# Update input_ids for next iteration
current_input_ids = torch.cat([current_input_ids, next_token], dim=1)
# Optional: break if we generate an end token
if next_token.item() == tokenizer.eos_token_id:
break
if save_embeddings:
hook.remove()
# save embeddings
embeddings_data = {
'embeddings': np.vstack(activation_sequences), # Stack all embeddings into shape (n_tokens, 2048)
'prompt_tokens': input_ids[0].cpu().numpy().tolist(),
'generated_tokens': generated_token_ids,
'token_strings': [tokenizer.decode([tid]) for tid in generated_token_ids]
}
np.save(os.path.join(output_dir, 'embedding_sequences.npy'), embeddings_data, allow_pickle=True)
# Save the full generation with all its special tokens
generated_text = tokenizer.decode(current_input_ids[0])
with open(os.path.join(output_dir, 'generated_text.txt'), 'w') as f:
f.write(generated_text)
# Save the attention tensor with shape
# (n_layers, n_batch, n_heads, n_prompt_tokens, n_prompt_tokens)
if save_attentions:
all_layers_attention = [layer.cpu().numpy() for layer in outputs.attentions]
attention_array = np.stack(all_layers_attention)
np.save(
os.path.join(output_dir, 'attention_scores.npy'),
attention_array
)
# save images of the attention head matrices for each layer
if images:
os.makedirs(os.path.join(output_dir, "attention-by-head-by-layer"), exist_ok=True)
for layer_num in range(16):
plot_attention_heads_by_layer(
layer_num,
os.path.join(output_dir, "attention-by-head-by-layer/attention-matrices"),
attention_array
)
df, fig = top_tokens_by_layer(outputs, input_ids, tokenizer)
plt.savefig(os.path.join(output_dir, 'top_tokens_by_layer.png'))
df.to_csv(os.path.join(output_dir, 'top_tokens_by_layer.csv'))
df, fig = top_tokens(outputs, input_ids, tokenizer)
plt.savefig(os.path.join(output_dir, 'top_tokens.png'))
df.to_csv(os.path.join(output_dir, 'top_tokens.csv'))
if __name__=="__main__":
parser = ArgumentParser()
parser.add_argument('--prompts_file', default='prompts.txt')
args = parser.parse_args()
with open(args.prompts_file) as f:
prompts = [x.replace('\n', '') for x in f.readlines()]
for p in prompts[:1]:
print(p)
main(p)