-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffn_importance.py
More file actions
381 lines (303 loc) · 14.1 KB
/
Copy pathffn_importance.py
File metadata and controls
381 lines (303 loc) · 14.1 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
370
371
372
373
374
375
376
377
378
379
380
381
"""
FFN (Feed-Forward Network) Importance Analysis
Analyzes and compresses the feed-forward layers in transformers.
FFN layers typically account for 2/3 of transformer parameters.
"""
import torch
import torch.nn as nn
from typing import Dict, List, Tuple, Optional
import numpy as np
from collections import defaultdict
class FFNAnalyzer:
"""
Analyzes importance of FFN (feed-forward network) neurons.
FFN structure in transformers:
- Linear1: [hidden_size, intermediate_size] (expansion, e.g., 768 -> 3072)
- Activation: GELU/ReLU
- Linear2: [intermediate_size, hidden_size] (projection back)
Accounts for ~66% of parameters in BERT/GPT-2.
"""
def __init__(self, model: nn.Module, model_type: str = 'auto'):
self.model = model
self.model_type = self._detect_model_type() if model_type == 'auto' else model_type
self.ffn_modules = self._detect_ffn_modules()
def _detect_model_type(self) -> str:
"""Detect model architecture."""
model_class = type(self.model).__name__.lower()
if 'bert' in model_class or 'roberta' in model_class:
return 'bert'
elif 'gpt' in model_class:
return 'gpt2'
else:
return 'unknown'
def _detect_ffn_modules(self) -> Dict[str, Dict]:
"""
Detect FFN modules in the model.
Returns:
Dict mapping module names to FFN info
"""
ffn_modules = {}
if self.model_type == 'bert':
# BERT: encoder.layer.X.intermediate and output
if hasattr(self.model, 'bert'):
encoder = self.model.bert.encoder
elif hasattr(self.model, 'encoder'):
encoder = self.model.encoder
else:
return ffn_modules
for i, layer in enumerate(encoder.layer):
if hasattr(layer, 'intermediate') and hasattr(layer, 'output'):
intermediate = layer.intermediate.dense
output = layer.output.dense
ffn_modules[f'layer.{i}.ffn'] = {
'layer_idx': i,
'intermediate': intermediate,
'output': output,
'intermediate_size': intermediate.out_features,
'hidden_size': intermediate.in_features,
}
elif self.model_type == 'gpt2':
# GPT-2: transformer.h.X.mlp
if hasattr(self.model, 'transformer'):
blocks = self.model.transformer.h
elif hasattr(self.model, 'h'):
blocks = self.model.h
else:
return ffn_modules
for i, block in enumerate(blocks):
if hasattr(block, 'mlp'):
mlp = block.mlp
if hasattr(mlp, 'c_fc') and hasattr(mlp, 'c_proj'):
c_fc = mlp.c_fc
# Conv1D stores dimensions as nf (out) and nx (in)
# Conv1D weight shape: [out_features, in_features]
if hasattr(c_fc, 'nf'):
intermediate_size = c_fc.nf
hidden_size = c_fc.nx
else:
intermediate_size = c_fc.out_features
hidden_size = c_fc.in_features
ffn_modules[f'layer.{i}.ffn'] = {
'layer_idx': i,
'intermediate': c_fc,
'output': mlp.c_proj,
'intermediate_size': intermediate_size,
'hidden_size': hidden_size,
}
print(f"[FFN Analyzer] Detected {len(ffn_modules)} FFN modules")
return ffn_modules
def compute_activation_importance(
self,
calibration_data: torch.Tensor,
num_samples: int = 10
) -> Dict[str, np.ndarray]:
"""
Compute neuron importance based on activation magnitudes.
Method: Neurons with consistently low activations are less important.
Args:
calibration_data: Input data for calibration
num_samples: Number of samples to use
Returns:
Dict mapping layer names to importance scores [intermediate_size]
"""
print(f"\n[FFN Activation Method] Computing neuron importance...")
self.model.eval()
activations = defaultdict(list)
hooks = []
def make_hook(layer_name):
def hook(module, input, output):
# Capture intermediate activations (after first linear layer)
if isinstance(output, torch.Tensor):
activations[layer_name].append(output.detach().cpu().abs())
return hook
# Register hooks on intermediate layers
for name, info in self.ffn_modules.items():
intermediate = info['intermediate']
hook = intermediate.register_forward_hook(make_hook(name))
hooks.append(hook)
# Forward passes
batch_size = min(num_samples, calibration_data.shape[0])
with torch.no_grad():
for i in range(batch_size):
sample = calibration_data[i:i+1]
try:
_ = self.model(sample)
except Exception as e:
print(f" Warning: Forward pass {i} failed: {e}")
continue
# Remove hooks
for hook in hooks:
hook.remove()
# Compute importance from activations
importance_scores = {}
for name, info in self.ffn_modules.items():
intermediate_size = info['intermediate_size']
if name in activations and len(activations[name]) > 0:
# Stack activations: [num_samples, batch, seq_len, intermediate_size]
acts = torch.stack(activations[name])
# Average magnitude across samples, batch, and sequence
avg_activation = acts.mean(dim=(0, 1, 2)) # [intermediate_size]
# Normalize to get importance scores
importance = avg_activation.numpy()
importance = importance / (importance.sum() + 1e-10)
importance_scores[name] = importance
else:
# No activations captured, use uniform
importance_scores[name] = np.ones(intermediate_size) / intermediate_size
print(f" Computed activation importance for {len(importance_scores)} FFN layers")
return importance_scores
def compute_weight_magnitude_importance(self) -> Dict[str, np.ndarray]:
"""
Compute neuron importance based on weight magnitudes.
Method: |W_in| * |W_out| for each neuron.
Neurons with small weights contribute less to output.
Returns:
Dict mapping layer names to importance scores [intermediate_size]
"""
print(f"\n[FFN Weight Magnitude Method] Computing neuron importance...")
importance_scores = {}
for name, info in self.ffn_modules.items():
intermediate = info['intermediate']
output = info['output']
# Get weights
W_in = intermediate.weight.data # [intermediate_size, hidden_size]
W_out = output.weight.data # [hidden_size, intermediate_size]
# Compute magnitude for each neuron
in_magnitude = W_in.abs().sum(dim=1) # [intermediate_size]
out_magnitude = W_out.abs().sum(dim=0) # [intermediate_size]
# Combined importance: product of input and output magnitudes
importance = (in_magnitude * out_magnitude).cpu().numpy()
# Normalize
importance = importance / (importance.sum() + 1e-10)
importance_scores[name] = importance
print(f" Computed weight magnitude importance for {len(importance_scores)} FFN layers")
return importance_scores
def compute_gradient_importance(
self,
calibration_data: torch.Tensor,
num_samples: int = 10
) -> Dict[str, np.ndarray]:
"""
Compute neuron importance based on gradients.
Method: Measure gradient magnitude w.r.t. intermediate neurons.
Args:
calibration_data: Input data
num_samples: Number of samples
Returns:
Dict mapping layer names to importance scores [intermediate_size]
"""
print(f"\n[FFN Gradient Method] Computing neuron importance...")
self.model.train()
gradients = defaultdict(list)
# Store intermediate outputs for gradient computation
intermediate_outputs = {}
hooks = []
def make_hook(layer_name):
def hook(module, input, output):
if output.requires_grad:
intermediate_outputs[layer_name] = output
return hook
# Register hooks
for name, info in self.ffn_modules.items():
intermediate = info['intermediate']
hook = intermediate.register_forward_hook(make_hook(name))
hooks.append(hook)
# Compute gradients
batch_size = min(num_samples, calibration_data.shape[0])
try:
for i in range(batch_size):
sample = calibration_data[i:i+1]
outputs = self.model(sample)
# Get logits
if hasattr(outputs, 'logits'):
logits = outputs.logits
elif hasattr(outputs, 'last_hidden_state'):
logits = outputs.last_hidden_state
elif isinstance(outputs, tuple):
logits = outputs[0]
else:
logits = outputs
# Compute loss
if isinstance(logits, torch.Tensor):
loss = logits.pow(2).mean()
loss.backward()
# Capture gradients from intermediate outputs
for name, output in intermediate_outputs.items():
if output.grad is not None:
gradients[name].append(output.grad.detach().cpu().abs())
self.model.zero_grad()
except Exception as e:
print(f" Warning: Gradient computation failed: {e}")
finally:
for hook in hooks:
hook.remove()
self.model.zero_grad()
self.model.eval()
# Compute importance from gradients
importance_scores = {}
for name, info in self.ffn_modules.items():
intermediate_size = info['intermediate_size']
if name in gradients and len(gradients[name]) > 0:
# Stack gradients and average
grads = torch.stack(gradients[name])
avg_grad = grads.mean(dim=(0, 1, 2)) # [intermediate_size]
# Normalize
importance = avg_grad.numpy()
importance = importance / (importance.sum() + 1e-10)
importance_scores[name] = importance
else:
importance_scores[name] = np.ones(intermediate_size) / intermediate_size
print(f" Computed gradient importance for {len(importance_scores)} FFN layers")
return importance_scores
def get_compression_statistics(self) -> Dict[str, any]:
"""Get statistics about FFN layers for compression analysis."""
total_ffn_params = 0
total_model_params = sum(p.numel() for p in self.model.parameters())
for name, info in self.ffn_modules.items():
intermediate = info['intermediate']
output = info['output']
ffn_params = sum(p.numel() for p in intermediate.parameters())
ffn_params += sum(p.numel() for p in output.parameters())
total_ffn_params += ffn_params
return {
'total_model_params': total_model_params,
'total_ffn_params': total_ffn_params,
'ffn_percentage': total_ffn_params / total_model_params * 100,
'num_ffn_layers': len(self.ffn_modules),
}
def compute_ffn_importance(
model: nn.Module,
calibration_data: torch.Tensor,
method: str = 'activation',
num_samples: int = 10,
model_type: str = 'auto'
) -> Dict[str, np.ndarray]:
"""
Compute FFN neuron importance scores.
Args:
model: Model to analyze
calibration_data: Calibration input data
method: 'activation', 'weight_magnitude', or 'gradient'
num_samples: Number of calibration samples
model_type: 'bert', 'gpt2', or 'auto'
Returns:
Dict mapping layer names to importance scores
"""
analyzer = FFNAnalyzer(model, model_type)
if method == 'activation':
return analyzer.compute_activation_importance(calibration_data, num_samples)
elif method == 'weight_magnitude':
return analyzer.compute_weight_magnitude_importance()
elif method == 'gradient':
return analyzer.compute_gradient_importance(calibration_data, num_samples)
else:
raise ValueError(f"Unknown method: {method}")
if __name__ == "__main__":
print("FFN Importance Analysis")
print("=" * 60)
print("\nAnalyzes feed-forward network layers in transformers.")
print("FFN layers typically account for 66% of model parameters.")
print("\nSupported methods:")
print(" - activation: Based on activation magnitudes")
print(" - weight_magnitude: Based on weight magnitudes")
print(" - gradient: Based on gradient magnitudes")