-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_generation.py
More file actions
267 lines (219 loc) · 8.37 KB
/
graph_generation.py
File metadata and controls
267 lines (219 loc) · 8.37 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
from __future__ import annotations
from collections import defaultdict
from typing import Dict, Iterable, List, Literal, Tuple
import torch
from .native import compact_topk_threshold as compact_topk_threshold_native
from .native import get_native_extension_status as _get_native_extension_status
from .native import is_native_extension_available
Node = Dict[str, object]
Implementation = Literal['baseline', 'optimized', 'extension', 'auto']
def _validate_feat_acts(feat_acts: torch.Tensor, top_k: int) -> Tuple[torch.Tensor, int]:
if feat_acts.dim() != 3:
raise ValueError('feat_acts must have shape [batch, seq_len, feature_dim].')
if top_k <= 0:
raise ValueError('top_k must be positive.')
return feat_acts.contiguous(), min(int(top_k), int(feat_acts.shape[-1]))
def _topk_threshold_torch(top_vals: torch.Tensor, top_idx: torch.Tensor, threshold: float):
valid_mask = top_vals >= threshold
batch_idx, pos_idx, k_idx = torch.where(valid_mask)
values = top_vals[batch_idx, pos_idx, k_idx]
features = top_idx[batch_idx, pos_idx, k_idx]
return batch_idx, pos_idx, features, values
def _build_nodes(
batch_idx: torch.Tensor,
pos_idx: torch.Tensor,
feature_idx: torch.Tensor,
values: torch.Tensor,
layer_idx: int,
include_batch: bool,
) -> List[Node]:
batch_list = batch_idx.tolist()
pos_list = pos_idx.tolist()
feature_list = feature_idx.tolist()
value_list = values.tolist()
nodes: List[Node] = []
for batch, pos, feature, value in zip(batch_list, pos_list, feature_list, value_list):
node_id = (
f'{layer_idx}_{batch}_{feature}_{pos}'
if include_batch
else f'{layer_idx}_{feature}_{pos}'
)
node = {
'node_id': node_id,
'feature': int(feature),
'layer': str(layer_idx),
'ctx_idx': int(pos),
'feature_type': 'cross layer transcoder',
'influence': float(value),
'activation': float(value),
}
if include_batch:
node['batch_idx'] = int(batch)
nodes.append(node)
return nodes
def extract_features_baseline(
feat_acts: torch.Tensor,
layer_idx: int,
top_k: int = 50,
threshold: float = 0.01,
) -> List[Node]:
feat_acts, top_k = _validate_feat_acts(feat_acts, top_k)
batch_size, seq_len, _ = feat_acts.shape
include_batch = batch_size > 1
nodes: List[Node] = []
for batch in range(batch_size):
for pos in range(seq_len):
pos_feats = feat_acts[batch, pos, :]
top_vals, top_idx = torch.topk(pos_feats, k=top_k)
for value, feature in zip(top_vals.tolist(), top_idx.tolist()):
if value < threshold:
continue
node_id = (
f'{layer_idx}_{batch}_{feature}_{pos}'
if include_batch
else f'{layer_idx}_{feature}_{pos}'
)
node = {
'node_id': node_id,
'feature': int(feature),
'layer': str(layer_idx),
'ctx_idx': int(pos),
'feature_type': 'cross layer transcoder',
'influence': float(value),
'activation': float(value),
}
if include_batch:
node['batch_idx'] = int(batch)
nodes.append(node)
return nodes
def extract_features_optimized(
feat_acts: torch.Tensor,
layer_idx: int,
top_k: int = 50,
threshold: float = 0.01,
) -> List[Node]:
feat_acts, top_k = _validate_feat_acts(feat_acts, top_k)
top_vals, top_idx = torch.topk(feat_acts, k=top_k, dim=-1)
batch_idx, pos_idx, feature_idx, values = _topk_threshold_torch(top_vals, top_idx, threshold)
return _build_nodes(
batch_idx=batch_idx,
pos_idx=pos_idx,
feature_idx=feature_idx,
values=values,
layer_idx=layer_idx,
include_batch=feat_acts.shape[0] > 1,
)
def extract_features_extension(
feat_acts: torch.Tensor,
layer_idx: int,
top_k: int = 50,
threshold: float = 0.01,
) -> List[Node]:
feat_acts, top_k = _validate_feat_acts(feat_acts, top_k)
top_vals, top_idx = torch.topk(feat_acts, k=top_k, dim=-1)
if is_native_extension_available():
batch_idx, pos_idx, feature_idx, values = compact_topk_threshold_native(
top_vals.contiguous(),
top_idx.contiguous(),
float(threshold),
)
else:
batch_idx, pos_idx, feature_idx, values = _topk_threshold_torch(top_vals, top_idx, threshold)
return _build_nodes(
batch_idx=batch_idx,
pos_idx=pos_idx,
feature_idx=feature_idx,
values=values,
layer_idx=layer_idx,
include_batch=feat_acts.shape[0] > 1,
)
def extract_features(
feat_acts: torch.Tensor,
layer_idx: int,
top_k: int = 50,
threshold: float = 0.01,
implementation: Implementation = 'auto',
) -> List[Node]:
if implementation == 'baseline':
return extract_features_baseline(feat_acts, layer_idx, top_k=top_k, threshold=threshold)
if implementation == 'optimized':
return extract_features_optimized(feat_acts, layer_idx, top_k=top_k, threshold=threshold)
if implementation == 'extension':
return extract_features_extension(feat_acts, layer_idx, top_k=top_k, threshold=threshold)
if implementation == 'auto':
return extract_features_extension(feat_acts, layer_idx, top_k=top_k, threshold=threshold)
raise ValueError('implementation must be one of: baseline, optimized, extension, auto.')
def _encode_feature_activations(hidden: torch.Tensor, transcoder: Dict[str, torch.Tensor]) -> torch.Tensor:
if hidden.dim() != 3:
raise ValueError('hidden must have shape [batch, seq_len, hidden_dim].')
weight = transcoder['W_enc']
bias = transcoder['b_enc']
batch_size, seq_len, hidden_dim = hidden.shape
if weight.shape[1] != hidden_dim:
if weight.shape[0] == hidden_dim:
weight = weight.t()
else:
raise ValueError('W_enc shape does not match hidden_dim.')
hidden_flat = hidden.reshape(batch_size * seq_len, hidden_dim)
feat_acts = torch.relu(hidden_flat.matmul(weight.t()) + bias)
return feat_acts.reshape(batch_size, seq_len, -1)
def generate_attribution_graph(
hidden_states: Dict[int, torch.Tensor],
transcoders: Dict[int, Dict[str, torch.Tensor]],
top_k: int = 50,
threshold: float = 0.01,
implementation: Implementation = 'auto',
) -> Dict[str, object]:
nodes: List[Node] = []
node_influences = defaultdict(float)
for layer_idx, hidden in hidden_states.items():
if layer_idx not in transcoders:
continue
feat_acts = _encode_feature_activations(hidden, transcoders[layer_idx])
layer_nodes = extract_features(
feat_acts=feat_acts,
layer_idx=layer_idx,
top_k=top_k,
threshold=threshold,
implementation=implementation,
)
nodes.extend(layer_nodes)
for node in layer_nodes:
key = (layer_idx, int(node['feature']))
node_influences[key] += float(node['influence'])
return {
'nodes': nodes,
'node_influences': dict(node_influences),
'summary': {
'num_nodes': len(nodes),
'num_layers': len(hidden_states),
'implementation': implementation if implementation != 'auto' else 'extension_or_fallback',
},
}
def generate_attribution_graph_optimized(
hidden_states: Dict[int, torch.Tensor],
transcoders: Dict[int, Dict[str, torch.Tensor]],
top_k: int = 50,
threshold: float = 0.01,
) -> Dict[str, object]:
return generate_attribution_graph(
hidden_states=hidden_states,
transcoders=transcoders,
top_k=top_k,
threshold=threshold,
implementation='auto',
)
def canonicalize_nodes(nodes: Iterable[Node]) -> List[Tuple[int, int, int, float]]:
canonical = []
for node in nodes:
canonical.append(
(
int(node.get('batch_idx', 0)),
int(node['ctx_idx']),
int(node['feature']),
float(node['activation']),
)
)
return canonical
def get_native_extension_status():
return _get_native_extension_status()