Spaces:
Running on Zero
Running on Zero
Commit 路
4f0e23c
1
Parent(s): 0342a67
feat: made attention logits plot more interactive and added intuitive logic fir attention logits and attention scores
Browse files- app.py +17 -1
- src/plots.py +20 -6
app.py
CHANGED
|
@@ -352,7 +352,7 @@ def update_attention(data, head, query_token):
|
|
| 352 |
)
|
| 353 |
return (
|
| 354 |
attention_heatmaps(sb, sa, tokens=data["tokens"]),
|
| 355 |
-
attention_bars(sb[qt], sa[qt], qt),
|
| 356 |
note,
|
| 357 |
_attention_context_markdown(data),
|
| 358 |
)
|
|
@@ -468,6 +468,22 @@ with gr.Blocks(title="RoPE Explorer") as demo:
|
|
| 468 |
attn_note = gr.Markdown()
|
| 469 |
gr.Markdown(
|
| 470 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
### How to read an attention score
|
| 472 |
|
| 473 |
Each heatmap cell is the raw dot product `Q_query 路 K_key` for the query token on
|
|
|
|
| 352 |
)
|
| 353 |
return (
|
| 354 |
attention_heatmaps(sb, sa, tokens=data["tokens"]),
|
| 355 |
+
attention_bars(sb[qt], sa[qt], qt, tokens=data["tokens"]),
|
| 356 |
note,
|
| 357 |
_attention_context_markdown(data),
|
| 358 |
)
|
|
|
|
| 468 |
attn_note = gr.Markdown()
|
| 469 |
gr.Markdown(
|
| 470 |
"""
|
| 471 |
+
### How to read the Attention logits bars
|
| 472 |
+
|
| 473 |
+
The selected query token is compared with every key token. Each bar is the raw
|
| 474 |
+
dot product `Q_query 路 K_key` for one key position:
|
| 475 |
+
|
| 476 |
+
- A **higher bar** means the key is a stronger match for this query relative to the other keys in the same chart.
|
| 477 |
+
- A **lower or negative bar** means a weaker or opposing match.
|
| 478 |
+
- The gap between **without RoPE** and **with RoPE** shows how position-aware rotation changes that comparison.
|
| 479 |
+
- These bars are **logits, not probabilities**. Applying softmax across all bars for one query would convert them into attention weights.
|
| 480 |
+
|
| 481 |
+
Use the hover text to see the exact key token and position behind each bar. The
|
| 482 |
+
query token is the position shown in the chart title.
|
| 483 |
+
"""
|
| 484 |
+
)
|
| 485 |
+
gr.Markdown(
|
| 486 |
+
"""
|
| 487 |
### How to read an attention score
|
| 488 |
|
| 489 |
Each heatmap cell is the raw dot product `Q_query 路 K_key` for the query token on
|
src/plots.py
CHANGED
|
@@ -275,15 +275,29 @@ def attention_heatmaps(
|
|
| 275 |
return fig
|
| 276 |
|
| 277 |
|
| 278 |
-
def attention_bars(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
xs = list(range(len(logits_before)))
|
|
|
|
| 280 |
fig = go.Figure()
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
fig.update_layout(
|
| 284 |
-
title=f"Attention logits from query
|
| 285 |
-
xaxis_title="key token",
|
| 286 |
-
yaxis_title="Q路K",
|
| 287 |
barmode="group",
|
| 288 |
template="plotly_white",
|
| 289 |
height=360,
|
|
|
|
| 275 |
return fig
|
| 276 |
|
| 277 |
|
| 278 |
+
def attention_bars(
|
| 279 |
+
logits_before: np.ndarray,
|
| 280 |
+
logits_after: np.ndarray,
|
| 281 |
+
query_token: int,
|
| 282 |
+
tokens=None,
|
| 283 |
+
) -> go.Figure:
|
| 284 |
xs = list(range(len(logits_before)))
|
| 285 |
+
labels = [str(tokens[i]) if tokens is not None else f"token {i}" for i in xs]
|
| 286 |
fig = go.Figure()
|
| 287 |
+
hover = (
|
| 288 |
+
"key position k=%{x}<br>key token=%{customdata}<br>"
|
| 289 |
+
"raw attention logit Q路K=%{y:.4f}<extra></extra>"
|
| 290 |
+
)
|
| 291 |
+
fig.add_trace(
|
| 292 |
+
go.Bar(x=xs, y=logits_before, customdata=labels, hovertemplate=hover, name="without RoPE")
|
| 293 |
+
)
|
| 294 |
+
fig.add_trace(
|
| 295 |
+
go.Bar(x=xs, y=logits_after, customdata=labels, hovertemplate=hover, name="with RoPE")
|
| 296 |
+
)
|
| 297 |
fig.update_layout(
|
| 298 |
+
title=f"Attention logits from query position k={query_token}",
|
| 299 |
+
xaxis_title="key token position k",
|
| 300 |
+
yaxis_title="raw attention logit (Q路K)",
|
| 301 |
barmode="group",
|
| 302 |
template="plotly_white",
|
| 303 |
height=360,
|