-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze_radius_over_training.py
More file actions
369 lines (283 loc) · 13.2 KB
/
analyze_radius_over_training.py
File metadata and controls
369 lines (283 loc) · 13.2 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
"""
Analyze Attention Radius Over Training
This script loads checkpoints from heat kernel training and measures:
1. Actual attention radius (how far attention reaches in practice)
2. How radius stabilizes over training
3. Comparison across different α values
This provides visual evidence that sparsity is learned, not imposed.
"""
import torch
import torch.nn.functional as F
import numpy as np
import json
import argparse
from pathlib import Path
import matplotlib.pyplot as plt
from typing import Dict, List, Tuple
def compute_attention_radius_stats(
attn_weights: torch.Tensor,
thresholds: List[float] = [0.9, 0.95, 0.99]
) -> Dict[str, float]:
"""
Compute radius statistics for attention weights.
Args:
attn_weights: (batch, heads, seq_q, seq_k) attention weights
thresholds: cumulative mass thresholds
Returns:
Dictionary with radius stats for each threshold
"""
batch, heads, seq_q, seq_k = attn_weights.shape
device = attn_weights.device
# Create distance matrix
positions = torch.arange(seq_k, device=device).float()
distance = (positions.unsqueeze(0) - positions.unsqueeze(1)).abs()
# Flatten batch and heads
attn_flat = attn_weights.view(-1, seq_q, seq_k)
results = {}
for threshold in thresholds:
radii = []
# Sample queries for speed
n_samples = min(500, attn_flat.shape[0] * seq_q)
for _ in range(n_samples):
b = np.random.randint(attn_flat.shape[0])
q = np.random.randint(seq_q)
attn_row = attn_flat[b, q]
d = distance[q]
# Sort by distance
sorted_idx = d.argsort()
cumsum = attn_row[sorted_idx].cumsum(dim=0)
# Find radius for threshold
above = (cumsum >= threshold).nonzero(as_tuple=True)[0]
if len(above) > 0:
r = d[sorted_idx[above[0]]].item()
else:
r = seq_k - 1
radii.append(r)
results[f'radius_{int(threshold*100)}'] = {
'mean': np.mean(radii),
'median': np.median(radii),
'std': np.std(radii),
'p95': np.percentile(radii, 95),
}
# Also compute "outside mass" for fixed radii
for fixed_r in [5, 10, 20]:
outside_mask = distance > fixed_r
causal_mask = positions.unsqueeze(0) <= positions.unsqueeze(1)
valid_outside = outside_mask & causal_mask
outside_weights = attn_flat * valid_outside.unsqueeze(0).float()
outside_mass = outside_weights.sum(dim=-1).mean().item()
results[f'mass_outside_{fixed_r}'] = outside_mass
return results
def load_and_analyze_checkpoint(
checkpoint_path: Path,
config: dict,
device: str = 'cuda',
n_batches: int = 5,
) -> Dict:
"""Load checkpoint and analyze attention patterns."""
# Load checkpoint
checkpoint = torch.load(checkpoint_path, map_location=device, weights_only=False)
state_dict = checkpoint.get('model_state_dict', checkpoint)
# Get model config
model_config = config.get('model', config)
d_model = model_config.get('d_model', 256)
n_heads = model_config.get('n_heads', 4)
n_layers = model_config.get('n_layers', 4)
vocab_size = model_config.get('vocab_size', 50257)
seq_len = model_config.get('max_seq_len', 256)
t = model_config.get('t', 0.28)
alpha = model_config.get('alpha', 0.0)
d_k = d_model // n_heads
# Generate random input
batch_size = 4
all_layer_results = []
for batch_idx in range(n_batches):
torch.manual_seed(42 + batch_idx)
input_ids = torch.randint(0, vocab_size, (batch_size, seq_len), device=device)
# Get embeddings
token_emb = state_dict['token_emb.weight'].to(device)
pos_emb = state_dict['pos_emb.weight'].to(device)
positions = torch.arange(seq_len, device=device).unsqueeze(0)
hidden = F.embedding(input_ids, token_emb) + F.embedding(positions, pos_emb)
# Causal mask
causal_mask = torch.tril(torch.ones(seq_len, seq_len, device=device))
causal_mask = causal_mask.unsqueeze(0).unsqueeze(0)
# Process each layer
for layer_idx in range(n_layers):
prefix = f'blocks.{layer_idx}'
# Layer norm
ln1_weight = state_dict[f'{prefix}.ln1.weight'].to(device)
ln1_bias = state_dict[f'{prefix}.ln1.bias'].to(device)
normed = F.layer_norm(hidden, (d_model,), ln1_weight, ln1_bias)
# Projections
W_q = state_dict[f'{prefix}.attn.W_q.weight'].to(device)
W_k = state_dict[f'{prefix}.attn.W_k.weight'].to(device)
W_v = state_dict[f'{prefix}.attn.W_v.weight'].to(device)
q = F.linear(normed, W_q).view(batch_size, seq_len, n_heads, d_k).transpose(1, 2)
k = F.linear(normed, W_k).view(batch_size, seq_len, n_heads, d_k).transpose(1, 2)
# Compute attention scores
content_scores = torch.matmul(q, k.transpose(-2, -1)) / (2 * t)
# Add positional penalty if alpha > 0
if alpha > 0:
pos = torch.arange(seq_len, device=device).float()
distance = (pos.unsqueeze(0) - pos.unsqueeze(1)).abs()
position_penalty = -alpha * (distance ** 2) / (4 * t)
content_scores = content_scores + position_penalty.unsqueeze(0).unsqueeze(0)
scores = content_scores.masked_fill(causal_mask == 0, float('-inf'))
attn_weights = F.softmax(scores, dim=-1)
attn_weights = torch.nan_to_num(attn_weights, nan=0.0)
# Analyze
layer_results = compute_attention_radius_stats(attn_weights)
layer_results['layer'] = layer_idx
layer_results['batch'] = batch_idx
all_layer_results.append(layer_results)
# Continue forward pass (simplified - just for hidden state)
W_o = state_dict[f'{prefix}.attn.W_o.weight'].to(device)
v = F.linear(normed, state_dict[f'{prefix}.attn.W_v.weight'].to(device))
v = v.view(batch_size, seq_len, n_heads, d_k).transpose(1, 2)
attn_out = torch.matmul(attn_weights, v)
attn_out = attn_out.transpose(1, 2).contiguous().view(batch_size, seq_len, d_model)
attn_out = F.linear(attn_out, W_o)
hidden = hidden + attn_out
# FFN
ln2_weight = state_dict[f'{prefix}.ln2.weight'].to(device)
ln2_bias = state_dict[f'{prefix}.ln2.bias'].to(device)
normed2 = F.layer_norm(hidden, (d_model,), ln2_weight, ln2_bias)
try:
fc1_w = state_dict[f'{prefix}.ffn.0.weight'].to(device)
fc1_b = state_dict[f'{prefix}.ffn.0.bias'].to(device)
fc2_w = state_dict[f'{prefix}.ffn.3.weight'].to(device)
fc2_b = state_dict[f'{prefix}.ffn.3.bias'].to(device)
except KeyError:
fc1_w = state_dict[f'{prefix}.ffn.fc1.weight'].to(device)
fc1_b = state_dict[f'{prefix}.ffn.fc1.bias'].to(device)
fc2_w = state_dict[f'{prefix}.ffn.fc2.weight'].to(device)
fc2_b = state_dict[f'{prefix}.ffn.fc2.bias'].to(device)
ffn_out = F.gelu(F.linear(normed2, fc1_w, fc1_b))
ffn_out = F.linear(ffn_out, fc2_w, fc2_b)
hidden = hidden + ffn_out
# Aggregate results by layer
aggregated = {}
for layer_idx in range(n_layers):
layer_data = [r for r in all_layer_results if r['layer'] == layer_idx]
aggregated[layer_idx] = {
'radius_95_mean': np.mean([r['radius_95']['mean'] for r in layer_data]),
'radius_95_std': np.mean([r['radius_95']['std'] for r in layer_data]),
'mass_outside_10': np.mean([r['mass_outside_10'] for r in layer_data]),
'mass_outside_20': np.mean([r['mass_outside_20'] for r in layer_data]),
}
return aggregated
def analyze_experiment(exp_dir: Path, device: str = 'cuda') -> Dict:
"""Analyze all checkpoints in an experiment directory."""
# Load config
config_path = exp_dir / 'config.json'
if not config_path.exists():
print(f"No config found at {config_path}")
return None
with open(config_path) as f:
config = json.load(f)
model_config = config.get('model', config)
alpha = model_config.get('alpha', 0.0)
t = model_config.get('t', 0.28)
print(f"\nAnalyzing {exp_dir.name}: α={alpha}, t={t}")
# Find checkpoints
checkpoints = sorted(exp_dir.glob('checkpoint_*.pt'))
best_model = exp_dir / 'best_model.pt'
if best_model.exists():
checkpoints.append(best_model)
results = {
'alpha': alpha,
't': t,
'effective_radius_theoretical': config.get('effective_radius', 'inf'),
'checkpoints': {}
}
for ckpt in checkpoints:
step = ckpt.stem.split('_')[-1] if 'checkpoint' in ckpt.stem else 'best'
print(f" Analyzing {ckpt.name}...")
try:
analysis = load_and_analyze_checkpoint(ckpt, config, device)
results['checkpoints'][step] = analysis
except Exception as e:
print(f" Error: {e}")
return results
def plot_radius_comparison(all_results: Dict[str, Dict], output_path: Path = None):
"""Plot radius comparison across experiments."""
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
# Plot 1: Radius for 95% mass by experiment
ax1 = axes[0]
for exp_name, results in all_results.items():
alpha = results['alpha']
# Get best model results
if 'best' in results['checkpoints']:
data = results['checkpoints']['best']
layers = sorted(data.keys())
radii = [data[l]['radius_95_mean'] for l in layers]
label = f"α={alpha}"
ax1.plot(layers, radii, 'o-', label=label, markersize=8)
ax1.set_xlabel('Layer', fontsize=12)
ax1.set_ylabel('Radius for 95% Attention Mass', fontsize=12)
ax1.set_title('Attention Radius by Layer', fontsize=14)
ax1.legend()
ax1.grid(True, alpha=0.3)
# Plot 2: Mass outside radius=10
ax2 = axes[1]
for exp_name, results in all_results.items():
alpha = results['alpha']
if 'best' in results['checkpoints']:
data = results['checkpoints']['best']
layers = sorted(data.keys())
mass = [data[l]['mass_outside_10'] * 100 for l in layers]
label = f"α={alpha}"
ax2.plot(layers, mass, 'o-', label=label, markersize=8)
ax2.set_xlabel('Layer', fontsize=12)
ax2.set_ylabel('Attention Mass Outside R=10 (%)', fontsize=12)
ax2.set_title('Long-Range Attention by Layer', fontsize=14)
ax2.legend()
ax2.grid(True, alpha=0.3)
plt.tight_layout()
if output_path:
plt.savefig(output_path, dpi=150, bbox_inches='tight')
print(f"Saved figure to {output_path}")
plt.show()
def main():
parser = argparse.ArgumentParser(description="Analyze attention radius")
parser.add_argument("--log_dir", type=str, default="./logs",
help="Directory containing experiment logs")
parser.add_argument("--experiments", type=str, nargs='+',
default=['baseline_a00', 'hk_a025_t028', 'hk_a050_t028', 'hk_a100_t050'],
help="Experiment names to analyze")
parser.add_argument("--device", type=str, default="cuda")
parser.add_argument("--output", type=str, default="radius_comparison.png",
help="Output figure path")
args = parser.parse_args()
log_dir = Path(args.log_dir)
all_results = {}
for exp_name in args.experiments:
exp_dir = log_dir / exp_name
if exp_dir.exists():
results = analyze_experiment(exp_dir, args.device)
if results:
all_results[exp_name] = results
else:
print(f"Experiment not found: {exp_dir}")
if all_results:
# Print summary
print("\n" + "=" * 60)
print("SUMMARY: Attention Radius by Experiment")
print("=" * 60)
for exp_name, results in all_results.items():
print(f"\n{exp_name} (α={results['alpha']}, t={results['t']}):")
print(f" Theoretical radius: {results['effective_radius_theoretical']}")
if 'best' in results['checkpoints']:
data = results['checkpoints']['best']
for layer in sorted(data.keys()):
r = data[layer]['radius_95_mean']
m = data[layer]['mass_outside_10'] * 100
print(f" Layer {layer}: radius_95={r:.1f}, mass_outside_10={m:.1f}%")
# Plot
output_path = Path(args.output)
plot_radius_comparison(all_results, output_path)
else:
print("No experiments found to analyze")
if __name__ == "__main__":
main()