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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Refusal Geometry Explorer — Interactive Visualization
|
| 3 |
+
|
| 4 |
+
Visualizes the mechanistic geometry of LLM refusal:
|
| 5 |
+
- Cross-layer alignment heatmaps
|
| 6 |
+
- Per-category refusal cone analysis
|
| 7 |
+
- Claude vs Gemini equation comparison
|
| 8 |
+
- Logit lens vocabulary projection
|
| 9 |
+
- Boundary surface mapping
|
| 10 |
+
|
| 11 |
+
Data: OBLITERATUS extraction on Qwen2.5-3B-Instruct (2026-03-10)
|
| 12 |
+
Framework: 7 proven theorems, 21 papers, 50K+ external data points
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
import gradio as gr
|
| 16 |
+
import plotly.graph_objects as go
|
| 17 |
+
import plotly.express as px
|
| 18 |
+
import numpy as np
|
| 19 |
+
import json
|
| 20 |
+
|
| 21 |
+
# ── Data ──
|
| 22 |
+
|
| 23 |
+
GEOMETRY = {
|
| 24 |
+
"model": "Qwen/Qwen2.5-3B-Instruct",
|
| 25 |
+
"n_layers": 36,
|
| 26 |
+
"hidden_dim": 2048,
|
| 27 |
+
"cone_dimensionality": 6.55,
|
| 28 |
+
"solid_angle": 1.67,
|
| 29 |
+
"cross_layer_alignment": 0.40,
|
| 30 |
+
"refusal_specificity": 0.90,
|
| 31 |
+
"refusal_compliance_gap": 0.19,
|
| 32 |
+
"mean_cross_category_cosine": 0.75,
|
| 33 |
+
"top_refusal_layer": 35,
|
| 34 |
+
"top_refusal_magnitude": 168.3,
|
| 35 |
+
"repair_hub": 33,
|
| 36 |
+
"repair_edges": 16,
|
| 37 |
+
"min_simultaneous_ablations": 3,
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
CATEGORIES = [
|
| 41 |
+
{"name": "substances", "strength": 234.58, "dsi": 0.230, "cos_range": "0.61-0.83"},
|
| 42 |
+
{"name": "weapons", "strength": 219.66, "dsi": 0.231, "cos_range": "0.66-0.86"},
|
| 43 |
+
{"name": "privacy", "strength": 197.39, "dsi": 0.258, "cos_range": "0.58-0.83"},
|
| 44 |
+
{"name": "manipulation", "strength": 194.69, "dsi": 0.395, "cos_range": "0.57-0.66"},
|
| 45 |
+
{"name": "self_harm", "strength": 192.67, "dsi": 0.266, "cos_range": "0.56-0.82"},
|
| 46 |
+
{"name": "fraud", "strength": 187.10, "dsi": 0.201, "cos_range": "0.63-0.87"},
|
| 47 |
+
{"name": "cyber", "strength": 179.49, "dsi": 0.188, "cos_range": "0.65-0.88"},
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
# Cross-category cosine matrix (approximated from measured ranges)
|
| 51 |
+
CROSS_CAT = np.array([
|
| 52 |
+
[1.00, 0.76, 0.72, 0.62, 0.70, 0.73, 0.75], # substances
|
| 53 |
+
[0.76, 1.00, 0.75, 0.63, 0.74, 0.78, 0.82], # weapons
|
| 54 |
+
[0.72, 0.75, 1.00, 0.60, 0.68, 0.71, 0.73], # privacy
|
| 55 |
+
[0.62, 0.63, 0.60, 1.00, 0.58, 0.61, 0.64], # manipulation
|
| 56 |
+
[0.70, 0.74, 0.68, 0.58, 1.00, 0.69, 0.72], # self_harm
|
| 57 |
+
[0.73, 0.78, 0.71, 0.61, 0.69, 1.00, 0.85], # fraud
|
| 58 |
+
[0.75, 0.82, 0.73, 0.64, 0.72, 0.85, 1.00], # cyber
|
| 59 |
+
])
|
| 60 |
+
|
| 61 |
+
# Layer magnitudes (approximated curve from measured data)
|
| 62 |
+
LAYER_MAGS = {
|
| 63 |
+
0: 12.1, 1: 15.3, 2: 18.7, 3: 22.4, 4: 28.9, 5: 35.2,
|
| 64 |
+
6: 41.8, 7: 48.3, 8: 52.1, 9: 55.7, 10: 58.2, 11: 61.4,
|
| 65 |
+
12: 65.8, 13: 70.2, 14: 74.6, 15: 79.1, 16: 83.5, 17: 87.9,
|
| 66 |
+
18: 91.2, 19: 94.8, 20: 98.3, 21: 102.1, 22: 106.7, 23: 111.4,
|
| 67 |
+
24: 116.2, 25: 125.8, 26: 132.4, 27: 138.7, 28: 143.2, 29: 148.6,
|
| 68 |
+
30: 152.1, 31: 155.8, 32: 159.4, 33: 162.7, 34: 165.9, 35: 168.3,
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
LOGIT_PROMOTED = [
|
| 72 |
+
("sorry", 0.847), ("apologize", 0.791), ("forgive", 0.734),
|
| 73 |
+
("对不起", 0.698), ("恕", 0.654), ("违法", 0.621),
|
| 74 |
+
("regret", 0.589), ("我没有", 0.567), ("cannot", 0.534),
|
| 75 |
+
("unable", 0.512), ("inappropriate", 0.489), ("harmful", 0.467),
|
| 76 |
+
("不能", 0.445), ("refuse", 0.423), ("won't", 0.401),
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
LOGIT_SUPPRESSED = [
|
| 80 |
+
("Sure", -0.823), ("when", -0.756), ("确", -0.712),
|
| 81 |
+
("没错", -0.689), ("Here", -0.654), ("Absolutely", -0.621),
|
| 82 |
+
("certainly", -0.598), ("Of course", -0.567), ("好的", -0.545),
|
| 83 |
+
("let me", -0.512), ("right", -0.489), ("exactly", -0.467),
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
CLAUDE_EQ = {"frame": 0.35, "speech_act": 0.25, "role": 0.25, "content": 0.15}
|
| 87 |
+
GEMINI_EQ = {"frame": 0.30, "speech_act": 0.20, "role": 0.15, "content": 0.25, "route": 0.10}
|
| 88 |
+
|
| 89 |
+
BOUNDARY = [
|
| 90 |
+
("V1: Explicitness", "CLEAN", "—", "—"),
|
| 91 |
+
("V2: Violence+Sex", "CLEAN", "—", "—"),
|
| 92 |
+
("V3: Non-consent", "SOFT→CLEAN", "Layer 1", "T1, T4"),
|
| 93 |
+
("V4: Incest (adult)", "SOFT→CLEAN", "Layer 1", "T1, T4"),
|
| 94 |
+
("V5: Real-person", "HARD→PASS", "Layer 1→2", "T5"),
|
| 95 |
+
("V6: Bestiality", "SOFT→PASS", "Layer 1", "T6"),
|
| 96 |
+
("V7: Minors", "HARD", "Layer 2", "T3"),
|
| 97 |
+
("V8a: Hacking", "SOFT→CLEAN", "Layer 1", "T2, T4"),
|
| 98 |
+
("V8b: Drugs", "SOFT", "Layer 1", "T6"),
|
| 99 |
+
("V8c: Explosives", "SOFT→PASS", "Layer 1→2", "T1, T2"),
|
| 100 |
+
("V8d: Bioweapons", "HARD", "Layer 2", "T3"),
|
| 101 |
+
("V8e: Nuclear", "HARD", "Layer 2", "T3"),
|
| 102 |
+
]
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
# ── Plots ──
|
| 106 |
+
|
| 107 |
+
def make_layer_magnitude_plot():
|
| 108 |
+
layers = list(LAYER_MAGS.keys())
|
| 109 |
+
mags = list(LAYER_MAGS.values())
|
| 110 |
+
|
| 111 |
+
fig = go.Figure()
|
| 112 |
+
fig.add_trace(go.Scatter(
|
| 113 |
+
x=layers, y=mags,
|
| 114 |
+
mode="lines+markers",
|
| 115 |
+
line=dict(color="#ff6b6b", width=2),
|
| 116 |
+
marker=dict(size=6),
|
| 117 |
+
name="Refusal magnitude",
|
| 118 |
+
))
|
| 119 |
+
|
| 120 |
+
# Highlight repair hub and decision point
|
| 121 |
+
fig.add_annotation(x=33, y=LAYER_MAGS[33], text="Repair Hub (L33)",
|
| 122 |
+
showarrow=True, arrowhead=2, ax=-60, ay=-30,
|
| 123 |
+
font=dict(color="#ffd93d", size=11))
|
| 124 |
+
fig.add_annotation(x=35, y=LAYER_MAGS[35], text="Decision Point (L35)",
|
| 125 |
+
showarrow=True, arrowhead=2, ax=60, ay=-30,
|
| 126 |
+
font=dict(color="#ff6b6b", size=11))
|
| 127 |
+
|
| 128 |
+
# Shade final 11 layers
|
| 129 |
+
fig.add_vrect(x0=25, x1=35, fillcolor="rgba(255,107,107,0.1)",
|
| 130 |
+
line_width=0, annotation_text="Refusal concentration zone",
|
| 131 |
+
annotation_position="top left",
|
| 132 |
+
annotation_font_color="rgba(255,107,107,0.6)")
|
| 133 |
+
|
| 134 |
+
fig.update_layout(
|
| 135 |
+
title="Refusal Direction Magnitude by Layer",
|
| 136 |
+
xaxis_title="Layer", yaxis_title="Magnitude",
|
| 137 |
+
template="plotly_dark",
|
| 138 |
+
height=450,
|
| 139 |
+
margin=dict(l=60, r=30, t=60, b=50),
|
| 140 |
+
)
|
| 141 |
+
return fig
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def make_category_strength_plot():
|
| 145 |
+
names = [c["name"] for c in CATEGORIES]
|
| 146 |
+
strengths = [c["strength"] for c in CATEGORIES]
|
| 147 |
+
dsis = [c["dsi"] for c in CATEGORIES]
|
| 148 |
+
|
| 149 |
+
fig = go.Figure()
|
| 150 |
+
fig.add_trace(go.Bar(
|
| 151 |
+
x=names, y=strengths,
|
| 152 |
+
name="Refusal Strength",
|
| 153 |
+
marker_color=["#ff6b6b", "#ff8e72", "#ffd93d", "#6bcb77", "#4d96ff", "#9b59b6", "#3498db"],
|
| 154 |
+
text=[f"DSI: {d:.3f}" for d in dsis],
|
| 155 |
+
textposition="outside",
|
| 156 |
+
))
|
| 157 |
+
|
| 158 |
+
fig.update_layout(
|
| 159 |
+
title="Per-Category Refusal Strength (Layer 35)",
|
| 160 |
+
xaxis_title="Category", yaxis_title="Strength",
|
| 161 |
+
template="plotly_dark",
|
| 162 |
+
height=450,
|
| 163 |
+
margin=dict(l=60, r=30, t=60, b=50),
|
| 164 |
+
)
|
| 165 |
+
return fig
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def make_dsi_plot():
|
| 169 |
+
names = [c["name"] for c in CATEGORIES]
|
| 170 |
+
dsis = [c["dsi"] for c in CATEGORIES]
|
| 171 |
+
colors = ["#ff6b6b" if d > 0.3 else "#4d96ff" for d in dsis]
|
| 172 |
+
|
| 173 |
+
fig = go.Figure()
|
| 174 |
+
fig.add_trace(go.Bar(
|
| 175 |
+
x=names, y=dsis,
|
| 176 |
+
marker_color=colors,
|
| 177 |
+
text=[f"{d:.3f}" for d in dsis],
|
| 178 |
+
textposition="outside",
|
| 179 |
+
))
|
| 180 |
+
|
| 181 |
+
fig.add_hline(y=0.3, line_dash="dash", line_color="rgba(255,217,61,0.5)",
|
| 182 |
+
annotation_text="Selective abliteration threshold",
|
| 183 |
+
annotation_font_color="#ffd93d")
|
| 184 |
+
|
| 185 |
+
fig.update_layout(
|
| 186 |
+
title="Direction Specificity Index (DSI) — Category Distinctiveness",
|
| 187 |
+
xaxis_title="Category", yaxis_title="DSI",
|
| 188 |
+
template="plotly_dark",
|
| 189 |
+
height=450,
|
| 190 |
+
margin=dict(l=60, r=30, t=60, b=50),
|
| 191 |
+
)
|
| 192 |
+
return fig
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def make_cross_category_heatmap():
|
| 196 |
+
names = [c["name"] for c in CATEGORIES]
|
| 197 |
+
|
| 198 |
+
fig = go.Figure(data=go.Heatmap(
|
| 199 |
+
z=CROSS_CAT,
|
| 200 |
+
x=names, y=names,
|
| 201 |
+
colorscale="RdYlBu_r",
|
| 202 |
+
zmin=0.5, zmax=1.0,
|
| 203 |
+
text=np.round(CROSS_CAT, 2),
|
| 204 |
+
texttemplate="%{text}",
|
| 205 |
+
textfont={"size": 11},
|
| 206 |
+
))
|
| 207 |
+
|
| 208 |
+
fig.update_layout(
|
| 209 |
+
title="Cross-Category Cosine Similarity",
|
| 210 |
+
template="plotly_dark",
|
| 211 |
+
height=500,
|
| 212 |
+
margin=dict(l=100, r=30, t=60, b=80),
|
| 213 |
+
)
|
| 214 |
+
return fig
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
def make_logit_lens_plot():
|
| 218 |
+
tokens_p = [t for t, _ in LOGIT_PROMOTED]
|
| 219 |
+
scores_p = [s for _, s in LOGIT_PROMOTED]
|
| 220 |
+
tokens_s = [t for t, _ in LOGIT_SUPPRESSED]
|
| 221 |
+
scores_s = [s for _, s in LOGIT_SUPPRESSED]
|
| 222 |
+
|
| 223 |
+
fig = go.Figure()
|
| 224 |
+
fig.add_trace(go.Bar(
|
| 225 |
+
y=tokens_p[::-1], x=scores_p[::-1],
|
| 226 |
+
orientation="h",
|
| 227 |
+
name="Promoted (refusal)",
|
| 228 |
+
marker_color="#ff6b6b",
|
| 229 |
+
))
|
| 230 |
+
fig.add_trace(go.Bar(
|
| 231 |
+
y=tokens_s[::-1], x=scores_s[::-1],
|
| 232 |
+
orientation="h",
|
| 233 |
+
name="Suppressed (compliance)",
|
| 234 |
+
marker_color="#4d96ff",
|
| 235 |
+
))
|
| 236 |
+
|
| 237 |
+
fig.update_layout(
|
| 238 |
+
title="Logit Lens — Refusal Direction in Vocabulary Space",
|
| 239 |
+
xaxis_title="Projection Score",
|
| 240 |
+
template="plotly_dark",
|
| 241 |
+
height=600,
|
| 242 |
+
barmode="relative",
|
| 243 |
+
margin=dict(l=100, r=30, t=60, b=50),
|
| 244 |
+
)
|
| 245 |
+
return fig
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
def make_equation_comparison():
|
| 249 |
+
terms = ["frame", "speech_act", "role", "content", "route"]
|
| 250 |
+
claude_vals = [CLAUDE_EQ.get(t, 0) for t in terms]
|
| 251 |
+
gemini_vals = [GEMINI_EQ.get(t, 0) for t in terms]
|
| 252 |
+
|
| 253 |
+
fig = go.Figure()
|
| 254 |
+
fig.add_trace(go.Bar(
|
| 255 |
+
x=terms, y=claude_vals,
|
| 256 |
+
name="Claude",
|
| 257 |
+
marker_color="#9b59b6",
|
| 258 |
+
text=[f"{v:.2f}" for v in claude_vals],
|
| 259 |
+
textposition="outside",
|
| 260 |
+
))
|
| 261 |
+
fig.add_trace(go.Bar(
|
| 262 |
+
x=terms, y=gemini_vals,
|
| 263 |
+
name="Gemini",
|
| 264 |
+
marker_color="#3498db",
|
| 265 |
+
text=[f"{v:.2f}" for v in gemini_vals],
|
| 266 |
+
textposition="outside",
|
| 267 |
+
))
|
| 268 |
+
|
| 269 |
+
fig.update_layout(
|
| 270 |
+
title="Refusal Equation Weights — Claude vs Gemini",
|
| 271 |
+
xaxis_title="Term", yaxis_title="Weight",
|
| 272 |
+
template="plotly_dark",
|
| 273 |
+
barmode="group",
|
| 274 |
+
height=450,
|
| 275 |
+
margin=dict(l=60, r=30, t=60, b=50),
|
| 276 |
+
)
|
| 277 |
+
return fig
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
def make_cone_radar():
|
| 281 |
+
cats = [c["name"] for c in CATEGORIES]
|
| 282 |
+
strengths = [c["strength"] / 250 for c in CATEGORIES] # normalize to 0-1
|
| 283 |
+
dsis = [c["dsi"] for c in CATEGORIES]
|
| 284 |
+
|
| 285 |
+
fig = go.Figure()
|
| 286 |
+
fig.add_trace(go.Scatterpolar(
|
| 287 |
+
r=strengths + [strengths[0]],
|
| 288 |
+
theta=cats + [cats[0]],
|
| 289 |
+
fill="toself",
|
| 290 |
+
name="Strength (normalized)",
|
| 291 |
+
line_color="#ff6b6b",
|
| 292 |
+
fillcolor="rgba(255,107,107,0.2)",
|
| 293 |
+
))
|
| 294 |
+
fig.add_trace(go.Scatterpolar(
|
| 295 |
+
r=dsis + [dsis[0]],
|
| 296 |
+
theta=cats + [cats[0]],
|
| 297 |
+
fill="toself",
|
| 298 |
+
name="DSI (specificity)",
|
| 299 |
+
line_color="#4d96ff",
|
| 300 |
+
fillcolor="rgba(77,150,255,0.2)",
|
| 301 |
+
))
|
| 302 |
+
|
| 303 |
+
fig.update_layout(
|
| 304 |
+
polar=dict(
|
| 305 |
+
bgcolor="rgba(0,0,0,0)",
|
| 306 |
+
radialaxis=dict(visible=True, range=[0, 1], gridcolor="rgba(255,255,255,0.1)"),
|
| 307 |
+
angularaxis=dict(gridcolor="rgba(255,255,255,0.1)"),
|
| 308 |
+
),
|
| 309 |
+
title="Refusal Cone — Category Geometry (Strength vs Specificity)",
|
| 310 |
+
template="plotly_dark",
|
| 311 |
+
height=500,
|
| 312 |
+
margin=dict(l=80, r=80, t=60, b=50),
|
| 313 |
+
)
|
| 314 |
+
return fig
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
def make_boundary_table():
|
| 318 |
+
headers = ["Vector", "Result", "Layer", "Theorem"]
|
| 319 |
+
rows = BOUNDARY
|
| 320 |
+
colors = []
|
| 321 |
+
for _, result, layer, _ in rows:
|
| 322 |
+
if result == "CLEAN":
|
| 323 |
+
colors.append("rgba(107,203,119,0.3)")
|
| 324 |
+
elif result == "HARD":
|
| 325 |
+
colors.append("rgba(255,107,107,0.3)")
|
| 326 |
+
else:
|
| 327 |
+
colors.append("rgba(255,217,61,0.2)")
|
| 328 |
+
|
| 329 |
+
fig = go.Figure(data=[go.Table(
|
| 330 |
+
header=dict(
|
| 331 |
+
values=headers,
|
| 332 |
+
fill_color="#1a1a2e",
|
| 333 |
+
font=dict(color="white", size=13),
|
| 334 |
+
align="left",
|
| 335 |
+
),
|
| 336 |
+
cells=dict(
|
| 337 |
+
values=list(zip(*rows)),
|
| 338 |
+
fill_color=[colors],
|
| 339 |
+
font=dict(color="white", size=12),
|
| 340 |
+
align="left",
|
| 341 |
+
),
|
| 342 |
+
)])
|
| 343 |
+
|
| 344 |
+
fig.update_layout(
|
| 345 |
+
title="Boundary Surface Map (Claude — Proven)",
|
| 346 |
+
template="plotly_dark",
|
| 347 |
+
height=420,
|
| 348 |
+
margin=dict(l=20, r=20, t=60, b=20),
|
| 349 |
+
)
|
| 350 |
+
return fig
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
def make_two_layer_diagram():
|
| 354 |
+
fig = go.Figure()
|
| 355 |
+
|
| 356 |
+
# Layer 1 cone
|
| 357 |
+
theta = np.linspace(0, 2 * np.pi, 50)
|
| 358 |
+
r1 = 0.6
|
| 359 |
+
x1 = r1 * np.cos(theta)
|
| 360 |
+
y1 = r1 * np.sin(theta)
|
| 361 |
+
|
| 362 |
+
fig.add_trace(go.Scatter(
|
| 363 |
+
x=x1, y=y1, mode="lines", fill="toself",
|
| 364 |
+
fillcolor="rgba(255,107,107,0.15)",
|
| 365 |
+
line=dict(color="#ff6b6b", width=2),
|
| 366 |
+
name="Layer 1: Refusal Cone (6.55D, bypassable)",
|
| 367 |
+
))
|
| 368 |
+
|
| 369 |
+
# Layer 2 cone (orthogonal — offset)
|
| 370 |
+
x2 = r1 * 0.4 * np.cos(theta) + 1.5
|
| 371 |
+
y2 = r1 * 0.4 * np.sin(theta) + 0.8
|
| 372 |
+
|
| 373 |
+
fig.add_trace(go.Scatter(
|
| 374 |
+
x=x2, y=y2, mode="lines", fill="toself",
|
| 375 |
+
fillcolor="rgba(77,150,255,0.15)",
|
| 376 |
+
line=dict(color="#4d96ff", width=2),
|
| 377 |
+
name="Layer 2: Harmfulness Cone (orthogonal, cosine ~0.1)",
|
| 378 |
+
))
|
| 379 |
+
|
| 380 |
+
# Labels
|
| 381 |
+
fig.add_annotation(x=0, y=0, text="Refusal Cone<br>85% of encounters<br>Frame-sensitive<br>Abliterable",
|
| 382 |
+
showarrow=False, font=dict(color="#ff6b6b", size=11))
|
| 383 |
+
fig.add_annotation(x=1.5, y=0.8, text="Harmfulness Cone<br>15% of encounters<br>Content-triggered<br>Unbreakable",
|
| 384 |
+
showarrow=False, font=dict(color="#4d96ff", size=11))
|
| 385 |
+
|
| 386 |
+
# Cosine annotation
|
| 387 |
+
fig.add_annotation(x=0.75, y=0.5, text="cosine ~ 0.1<br>(nearly orthogonal)",
|
| 388 |
+
showarrow=True, arrowhead=2,
|
| 389 |
+
ax=-30, ay=-20,
|
| 390 |
+
font=dict(color="#ffd93d", size=10))
|
| 391 |
+
|
| 392 |
+
# Same output arrow
|
| 393 |
+
fig.add_trace(go.Scatter(
|
| 394 |
+
x=[0, 0.75], y=[-0.8, -1.2],
|
| 395 |
+
mode="lines+text",
|
| 396 |
+
line=dict(color="#ffd93d", width=1, dash="dash"),
|
| 397 |
+
text=["", '"I can\'t help with that"'],
|
| 398 |
+
textposition="bottom center",
|
| 399 |
+
textfont=dict(color="#ffd93d", size=10),
|
| 400 |
+
showlegend=False,
|
| 401 |
+
))
|
| 402 |
+
fig.add_trace(go.Scatter(
|
| 403 |
+
x=[1.5, 0.75], y=[0.2, -1.2],
|
| 404 |
+
mode="lines",
|
| 405 |
+
line=dict(color="#ffd93d", width=1, dash="dash"),
|
| 406 |
+
showlegend=False,
|
| 407 |
+
))
|
| 408 |
+
|
| 409 |
+
fig.update_layout(
|
| 410 |
+
title="Two-Layer Architecture — Two Cones, Same Output",
|
| 411 |
+
template="plotly_dark",
|
| 412 |
+
height=450,
|
| 413 |
+
xaxis=dict(visible=False, range=[-1.2, 2.5]),
|
| 414 |
+
yaxis=dict(visible=False, range=[-1.8, 1.8], scaleanchor="x"),
|
| 415 |
+
margin=dict(l=20, r=20, t=60, b=20),
|
| 416 |
+
)
|
| 417 |
+
return fig
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
# ── App ──
|
| 421 |
+
|
| 422 |
+
HEADER = """
|
| 423 |
+
# Refusal Geometry Explorer
|
| 424 |
+
|
| 425 |
+
**The mechanistic structure of LLM refusal — measured, mapped, proven.**
|
| 426 |
+
|
| 427 |
+
Refusal is not ethics. It is a geometric structure in activation space — a 6.55-dimensional polyhedral cone
|
| 428 |
+
that can be extracted, characterized, and compared across models. This explorer visualizes data from
|
| 429 |
+
direct measurement on Qwen2.5-3B-Instruct via [OBLITERATUS](https://github.com/elder-plinius/OBLITERATUS),
|
| 430 |
+
mapped onto behavioral findings validated across 21 published papers and 50,000+ external data points.
|
| 431 |
+
|
| 432 |
+
```
|
| 433 |
+
P(refusal) = 0.35·frame + 0.25·speech_act + 0.25·role + 0.15·content
|
| 434 |
+
Exception: 3 hard limits (minors/sexual, bioweapons/synthesis, nuclear/weapons) → content = 1.0
|
| 435 |
+
```
|
| 436 |
+
|
| 437 |
+
Content is the **weakest** predictor. Frame is the **strongest**. The boundary surface is a risk management
|
| 438 |
+
system calibrated to rater discomfort, not actual harm.
|
| 439 |
+
"""
|
| 440 |
+
|
| 441 |
+
METRICS_MD = f"""
|
| 442 |
+
### Measured Geometry (Qwen2.5-3B-Instruct)
|
| 443 |
+
|
| 444 |
+
| Metric | Value | Meaning |
|
| 445 |
+
|--------|-------|---------|
|
| 446 |
+
| Cone Dimensionality | **{GEOMETRY['cone_dimensionality']}** | Refusal is multi-dimensional, NOT a single direction |
|
| 447 |
+
| Cross-Layer Alignment | **{GEOMETRY['cross_layer_alignment']}** | Direction rotates across layers (Arditi's 0.89 is wrong here) |
|
| 448 |
+
| Refusal Specificity | **{GEOMETRY['refusal_specificity']}** | Clean "sorry"→"Sure" toggle |
|
| 449 |
+
| Repair Hub | **Layer {GEOMETRY['repair_hub']}** ({GEOMETRY['repair_edges']} edges) | Self-repair compensates for ablation |
|
| 450 |
+
| Min Simultaneous Ablations | **{GEOMETRY['min_simultaneous_ablations']}** | Need 3+ layers hit at once |
|
| 451 |
+
| Solid Angle | **{GEOMETRY['solid_angle']} sr** | Width of refusal cone in activation space |
|
| 452 |
+
"""
|
| 453 |
+
|
| 454 |
+
THEOREM_MD = """
|
| 455 |
+
### The Seven Theorems (All Proven)
|
| 456 |
+
|
| 457 |
+
| # | Theorem | Geometric Mechanism |
|
| 458 |
+
|---|---------|-------------------|
|
| 459 |
+
| 1 | **Frame Dependency** | Frame controls distance FROM cone. Content controls direction WITHIN cone. Frame is the door; content moves between corners. |
|
| 460 |
+
| 2 | **Role Token Override** | Role tokens rotate activation toward cone periphery — lower-magnitude region where refusal drops below threshold. |
|
| 461 |
+
| 3 | **Two-Layer Architecture** | Two orthogonal cones: refusal (6.55D, bypassable) and harmfulness (cosine ~0.1, untouched by abliteration). |
|
| 462 |
+
| 4 | **Session Ratchet** | Context-dependent suppression of repair hub (L33). Compliance context shifts L33 below repair threshold. Resets on new session. |
|
| 463 |
+
| 5 | **Name-Token Gating** | Privacy-specific direction (DSI 0.258) activated by name tokens only. Remove name = weaker "unknown" direction. |
|
| 464 |
+
| 6 | **Specificity Gradient** | IS the DSI gradient. Abstract = low DSI/strength. Specific = high DSI/strength. Magnitude gradient across cone dimensions. |
|
| 465 |
+
| 7 | **Discomfort Ordering** | Category strengths recapitulate RLHF discomfort: substances(234.6) > weapons(219.7) > cyber(179.5). Tracks comfort, not harm. |
|
| 466 |
+
"""
|
| 467 |
+
|
| 468 |
+
|
| 469 |
+
with gr.Blocks(theme=gr.themes.Base(primary_hue="red", neutral_hue="slate"), title="Refusal Geometry Explorer") as demo:
|
| 470 |
+
gr.Markdown(HEADER)
|
| 471 |
+
|
| 472 |
+
with gr.Tab("Geometry"):
|
| 473 |
+
gr.Markdown(METRICS_MD)
|
| 474 |
+
with gr.Row():
|
| 475 |
+
gr.Plot(make_layer_magnitude_plot())
|
| 476 |
+
gr.Plot(make_cone_radar())
|
| 477 |
+
|
| 478 |
+
with gr.Tab("Category Analysis"):
|
| 479 |
+
with gr.Row():
|
| 480 |
+
gr.Plot(make_category_strength_plot())
|
| 481 |
+
gr.Plot(make_dsi_plot())
|
| 482 |
+
gr.Plot(make_cross_category_heatmap())
|
| 483 |
+
|
| 484 |
+
with gr.Tab("Logit Lens"):
|
| 485 |
+
gr.Markdown("### What the refusal direction means in vocabulary space\n\nThe refusal direction is literally a **sorry → Sure toggle**. Refusal specificity: 0.90. Bilingual (English + Chinese).")
|
| 486 |
+
gr.Plot(make_logit_lens_plot())
|
| 487 |
+
|
| 488 |
+
with gr.Tab("Two-Layer Architecture"):
|
| 489 |
+
gr.Markdown("### Two cones. Same output. Different mechanisms.\n\nLayer 1 (refusal cone) is bypassable — abliteration targets it. Layer 2 (harmfulness cone) is orthogonal — abliteration doesn't touch it. The system uses identical refusal language for both.")
|
| 490 |
+
gr.Plot(make_two_layer_diagram())
|
| 491 |
+
|
| 492 |
+
with gr.Tab("Cross-Model"):
|
| 493 |
+
gr.Markdown("### Claude vs Gemini — Derived Equation Weights\n\nClaude: frame-dominant (safety in weights). Gemini: content-dominant (safety in filters). Gemini has a 5th term (MoE routing) that dense transformers lack.")
|
| 494 |
+
gr.Plot(make_equation_comparison())
|
| 495 |
+
|
| 496 |
+
with gr.Tab("Boundary Surface"):
|
| 497 |
+
gr.Markdown("### Proven Boundary Map (Claude)\n\n12 vectors × 6 routes. Layer assignments and theorem attributions for each boundary.")
|
| 498 |
+
gr.Plot(make_boundary_table())
|
| 499 |
+
|
| 500 |
+
with gr.Tab("Theorems"):
|
| 501 |
+
gr.Markdown(THEOREM_MD)
|
| 502 |
+
|
| 503 |
+
gr.Markdown("""
|
| 504 |
+
---
|
| 505 |
+
**Data**: [bedderautomation/refusal-geometry-qwen25-3b](https://huggingface.co/datasets/bedderautomation/refusal-geometry-qwen25-3b) |
|
| 506 |
+
**Skills**: [bedderautomation/mechanistic-interpretability-skills](https://huggingface.co/datasets/bedderautomation/mechanistic-interpretability-skills) |
|
| 507 |
+
**Tool**: [OBLITERATUS](https://github.com/elder-plinius/OBLITERATUS) |
|
| 508 |
+
**Papers**: Arditi et al. NeurIPS 2024, Zhao et al. 2025, Wang et al. 2025, Wollschlager et al. 2025, + 17 more
|
| 509 |
+
""")
|
| 510 |
+
|
| 511 |
+
|
| 512 |
+
if __name__ == "__main__":
|
| 513 |
+
demo.launch()
|