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
| {
"$schema": "https://lvg.dev/v1",
"id": "transformer-attn-debugger",
"title": "Transformer Attention Debugger",
"target": {
"model_id": "active_model",
"layer": "encoder.blocks.5.self_attn",
"pass": "inference",
"scope": "forward",
"batch": "active"
},
"inputs": [
{ "as": "Q", "from": { "type": "tensor", "source": "layer_output", "name": "Q" } },
{ "as": "K", "from": { "type": "tensor", "source": "layer_output", "name": "K" } },
{ "as": "V", "from": { "type": "tensor", "source": "layer_output", "name": "V" } },
{ "as": "ATTN", "from": { "type": "tensor", "source": "layer_output", "name": "attn" } },
{ "as": "TOKENS", "from": { "type": "meta", "key": "token_strings" } }
],
"parameters": [
{ "name": "head", "value": 0, "ui": { "type": "slider", "min": 0, "max": 11, "step": 1 } },
{
"name": "dr_method",
"value": "pca",
"ui": { "type": "select", "options": [ { "label": "PCA", "value": "pca" }, { "label": "UMAP", "value": "umap" } ] }
}
],
"signals": [
{ "name": "hover_token", "value": null },
{ "name": "selected_tokens", "value": [] }
],
"state": {
"read": [
{ "key": "debug.selected_tokens", "as": "selected_tokens", "default": [] },
{ "key": "debug.selected_head", "as": "head", "default": 0 }
],
"write": [
{ "key": "debug.selected_tokens", "from": "selected_tokens", "on": { "type": "brush", "view": "token-embed" } },
{ "key": "debug.selected_head", "from": "head", "on": "immediate" }
]
},
"layout": { "type": "grid", "columns": 2, "gap": 12, "responsive": "wrap" },
"views": [
{
"type": "generic",
"id": "token-embed",
"title": "Token Embedding (V) — DR Scatter",
"data": {
"sources": [
{ "name": "V_src", "from": "V", "kind": "tensor" },
{ "name": "TOK", "from": "TOKENS", "kind": "meta" }
],
"transforms": [
{ "op": "reshape", "from": "V_src", "to": "table", "axis_names": ["batch","tokens","heads","dim"] },
{ "op": "reduce", "from": "V_src", "dims": [2], "fn": "mean", "keepdims": false, "as": "V_tokens" },
{ "op": "project", "from": "V_tokens", "method": { "$param": "dr_method" }, "k": 2, "as": "V_2d" },
{ "op": "expr", "from": "V_2d", "expr": "add_field(index(),'tok_idx')", "as": "V_xy" },
{ "op": "join", "left": "V_xy", "right": "TOK", "on": ["tok_idx"], "how": "left", "as": "V_labeled" }
],
"output": "V_labeled"
},
"encoding": {
"mark": "scatter",
"data": "V_labeled",
"x": { "field": "x0", "type": "quant" },
"y": { "field": "x1", "type": "quant" },
"color": { "field": "tok_idx", "type": "index" },
"size": { "field": "variance", "type": "quant" },
"tooltip": ["tok_idx", "token_string"]
},
"interactions": [
{ "type": "brush", "target": "xy", "signal": "selected_tokens" },
{ "type": "select", "on": "hover", "fields": ["tok_idx"], "signal": "hover_token" }
],
"actions": [
{ "id": "export-embed", "label": "Export CSV", "kind": "export_csv", "from": "V_labeled" }
]
},
{
"type": "targeted",
"id": "attn-matrix",
"title": "Attention Matrix — Head & Token Subset",
"preset": "transformer_attention_matrix",
"inputs": { "attn": "ATTN", "token_strings": "TOKENS" },
"options": { "head": { "$param": "head" }, "normalize_along": "row" },
"interactions": [
{ "type": "link", "with": "token-embed", "using": ["tok_idx"] }
],
"actions": [
{ "id": "pin-head", "label": "Set as Compare A", "kind": "write_state", "toStateKey": "debug.compare.attnA", "from": "head" },
{ "id": "pin-selection", "label": "Set Tokens as Compare A", "kind": "write_state", "toStateKey": "debug.compare.tokensA", "from": "selected_tokens" }
]
},
{
"type": "generic",
"id": "attn-compare",
"title": "Compare Current Head vs Pinned",
"data": {
"sources": [
{ "name": "ATTN_src", "from": "ATTN", "kind": "tensor" },
{ "name": "head_curr", "from": "head", "kind": "state" },
{ "name": "head_A", "from": "debug.compare.attnA", "kind": "state" },
{ "name": "sel_tokens", "from": "selected_tokens", "kind": "state" },
{ "name": "tokensA", "from": "debug.compare.tokensA", "kind": "state" }
],
"transforms": [
{ "op": "slice", "from": "ATTN_src", "spec": { "axes": [0,1,2,3], "index": ["all", { "$state": "head_curr" }, "all", "all"] }, "as": "A_curr" },
{ "op": "slice", "from": "ATTN_src", "spec": { "axes": [0,1,2,3], "index": ["all", { "$state": "head_A" }, "all", "all"] }, "as": "A_pin" },
{ "op": "reduce", "from": "A_curr", "dims": [0], "fn": "mean", "as": "A_curr2D" },
{ "op": "reduce", "from": "A_pin", "dims": [0], "fn": "mean", "as": "A_pin2D" },
{ "op": "slice", "from": "A_curr2D", "spec": { "axes": [0,1], "index": [ { "$state": "sel_tokens" }, { "$state": "sel_tokens" } ] }, "as": "A_curr_sub" },
{ "op": "slice", "from": "A_pin2D", "spec": { "axes": [0,1], "index": [ { "$state": "tokensA" }, { "$state": "tokensA" } ] }, "as": "A_pin_sub" },
{ "op": "compare", "a": "A_curr_sub", "b": "A_pin_sub", "mode": "diff", "as": "A_diff" }
],
"output": "A_diff"
},
"encoding": {
"mark": "heatmap",
"data": "A_diff",
"x": { "field": "col", "type": "index" },
"y": { "field": "row", "type": "index" },
"color": { "field": "value", "type": "quant" },
"tooltip": ["row","col","value"]
},
"actions": [
{ "id": "export-diff", "label": "Export Diff CSV", "kind": "export_csv", "from": "A_diff" }
]
}
]
}
|