basal-engine 3.0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- basal/__init__.py +7 -0
- basal/adapter.py +420 -0
- basal/adapters.py +8 -0
- basal/agree.py +393 -0
- basal/attach.py +160 -0
- basal/attn512.py +132 -0
- basal/auth.py +194 -0
- basal/bench.py +367 -0
- basal/blockquant.py +227 -0
- basal/calibrate.py +2198 -0
- basal/chat.py +175 -0
- basal/cli.py +70 -0
- basal/client.py +8 -0
- basal/decision_log.py +189 -0
- basal/decision_rules.py +569 -0
- basal/decisions.py +158 -0
- basal/derive.py +98 -0
- basal/disconnect.py +85 -0
- basal/engine.py +6023 -0
- basal/evidence.py +133 -0
- basal/examples/choice.jsonl +8 -0
- basal/examples/complex.jsonl +5 -0
- basal/examples/labelled_tickets.jsonl +500 -0
- basal/examples/local_backend.py +34 -0
- basal/examples/noul.jsonl +8 -0
- basal/examples/questions.jsonl +20 -0
- basal/examples/score.jsonl +8 -0
- basal/examples/selfcheck_basal-omni-pico-Q8_0.json +1 -0
- basal/examples/selfcheck_basal-omni-pico.json +1 -0
- basal/examples/types_demo.py +74 -0
- basal/facts.py +497 -0
- basal/feedback.py +234 -0
- basal/file_identity.py +39 -0
- basal/finetune.py +321 -0
- basal/fit_decisions.py +944 -0
- basal/fla_configs.json +1822 -0
- basal/fold.py +206 -0
- basal/fonts/OFL-DMSans.txt +93 -0
- basal/fonts/OFL-SpaceGrotesk.txt +93 -0
- basal/fonts/dm-sans-latin-ext.woff2 +0 -0
- basal/fonts/dm-sans-latin.woff2 +0 -0
- basal/fonts/space-grotesk-latin-ext.woff2 +0 -0
- basal/fonts/space-grotesk-latin.woff2 +0 -0
- basal/fp8block.py +100 -0
- basal/gdn_graph.py +1792 -0
- basal/gemma4.py +726 -0
- basal/gemma4_graph.py +931 -0
- basal/gguf_export.py +497 -0
- basal/identity.py +217 -0
- basal/int8block.py +50 -0
- basal/interpolate.py +330 -0
- basal/jsonl.py +172 -0
- basal/lean.py +204 -0
- basal/llama_attn.py +252 -0
- basal/llama_attn_kernels.py +266 -0
- basal/llama_graph.py +587 -0
- basal/llama_kernels.py +593 -0
- basal/loadtest.py +189 -0
- basal/mcp_server.py +432 -0
- basal/media.py +138 -0
- basal/metrics.py +16 -0
- basal/mlx_attn_kernels.py +1057 -0
- basal/mlx_forward.py +326 -0
- basal/mlx_kernels.py +543 -0
- basal/mlx_metal.py +510 -0
- basal/navcut.py +283 -0
- basal/navigate.py +646 -0
- basal/navtree.py +397 -0
- basal/nvfp4.py +245 -0
- basal/ordercheck.py +165 -0
- basal/pdf.py +183 -0
- basal/prepared.py +355 -0
- basal/presets.py +8 -0
- basal/prompt.py +462 -0
- basal/rank.py +373 -0
- basal/readout.py +121 -0
- basal/request.py +327 -0
- basal/response_cache.py +105 -0
- basal/router.py +1551 -0
- basal/router_ui.html +902 -0
- basal/router_ui.py +277 -0
- basal/run.py +376 -0
- basal/selfcheck.py +215 -0
- basal/server.py +5279 -0
- basal/sets.py +214 -0
- basal/signals.py +28 -0
- basal/state_disk.py +942 -0
- basal/strix.py +505 -0
- basal/synth.py +92 -0
- basal/tasks.py +848 -0
- basal/textio.py +21 -0
- basal/think.py +390 -0
- basal/tools.py +171 -0
- basal/tpu.py +336 -0
- basal/tpu_kernels.py +129 -0
- basal/trace.py +125 -0
- basal/ui.html +861 -0
- basal/ui.py +526 -0
- basal/varloop.py +813 -0
- basal/varloop_graph.py +1377 -0
- basal/varloop_kernels.py +907 -0
- basal/viz.html +660 -0
- basal/viz.py +375 -0
- basal/vllm_unified.py +100 -0
- basal_engine-3.0.3.dist-info/METADATA +416 -0
- basal_engine-3.0.3.dist-info/RECORD +111 -0
- basal_engine-3.0.3.dist-info/WHEEL +5 -0
- basal_engine-3.0.3.dist-info/entry_points.txt +21 -0
- basal_engine-3.0.3.dist-info/licenses/LICENSE +202 -0
- basal_engine-3.0.3.dist-info/licenses/NOTICE +34 -0
- basal_engine-3.0.3.dist-info/top_level.txt +1 -0
basal/viz.html
ADDED
|
@@ -0,0 +1,660 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>basal decision visualiser</title>
|
|
7
|
+
<style>
|
|
8
|
+
:root {
|
|
9
|
+
color-scheme: light;
|
|
10
|
+
--page: #f9f9f7; --surface: #fcfcfb; --surface-2: #f3f2ee;
|
|
11
|
+
--ink: #0b0b0b; --ink-2: #52514e; --muted: #898781;
|
|
12
|
+
--grid: #e1e0d9; --axis: #c3c2b7; --border: rgba(11,11,11,0.10);
|
|
13
|
+
--s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
|
|
14
|
+
--neutral-mark: #c3c2b7;
|
|
15
|
+
--good: #0ca30c; --warning: #fab219; --critical: #d03b3b; --good-text: #006300;
|
|
16
|
+
--w-state: rgba(27,175,122,0.14); --w-question: rgba(237,161,0,0.18); --w-option: rgba(74,58,167,0.10); --w-prefill: rgba(232,123,164,0.22);
|
|
17
|
+
--accent: #2a78d6;
|
|
18
|
+
}
|
|
19
|
+
@media (prefers-color-scheme: dark) {
|
|
20
|
+
:root:where(:not([data-theme="light"])) {
|
|
21
|
+
color-scheme: dark;
|
|
22
|
+
--page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321;
|
|
23
|
+
--ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781;
|
|
24
|
+
--grid: #2c2c2a; --axis: #383835; --border: rgba(255,255,255,0.10);
|
|
25
|
+
--s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
|
|
26
|
+
--neutral-mark: #5b5a55; --good-text: #0ca30c;
|
|
27
|
+
--w-state: rgba(25,158,112,0.22); --w-question: rgba(201,133,0,0.26); --w-option: rgba(144,133,233,0.18); --w-prefill: rgba(213,81,129,0.28);
|
|
28
|
+
--accent: #3987e5;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
:root[data-theme="dark"] {
|
|
32
|
+
color-scheme: dark;
|
|
33
|
+
--page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321;
|
|
34
|
+
--ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781;
|
|
35
|
+
--grid: #2c2c2a; --axis: #383835; --border: rgba(255,255,255,0.10);
|
|
36
|
+
--s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
|
|
37
|
+
--neutral-mark: #5b5a55; --good-text: #0ca30c;
|
|
38
|
+
--w-state: rgba(25,158,112,0.22); --w-question: rgba(201,133,0,0.26); --w-option: rgba(144,133,233,0.18); --w-prefill: rgba(213,81,129,0.28);
|
|
39
|
+
--accent: #3987e5;
|
|
40
|
+
}
|
|
41
|
+
* { box-sizing: border-box; }
|
|
42
|
+
body { margin: 0; background: var(--page); color: var(--ink); font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif; }
|
|
43
|
+
header { display: flex; align-items: center; gap: 12px; padding: 12px 20px; border-bottom: 1px solid var(--border); background: var(--surface); position: sticky; top: 0; z-index: 5; }
|
|
44
|
+
header h1 { font-size: 16px; margin: 0; font-weight: 600; }
|
|
45
|
+
header .chip { margin-left: 4px; }
|
|
46
|
+
header .spacer { flex: 1; }
|
|
47
|
+
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--ink-2); font-size: 12px; white-space: nowrap; }
|
|
48
|
+
button, select, input, textarea { font: inherit; color: inherit; }
|
|
49
|
+
button { cursor: pointer; border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 6px 12px; }
|
|
50
|
+
button:hover { background: var(--surface-2); }
|
|
51
|
+
button.primary { background: var(--accent); color: #fff; border-color: transparent; font-weight: 600; padding: 8px 16px; }
|
|
52
|
+
button.primary:hover { filter: brightness(1.08); }
|
|
53
|
+
button.primary:disabled { opacity: .55; cursor: progress; }
|
|
54
|
+
button.ghost { border-color: transparent; background: transparent; color: var(--ink-2); padding: 4px 8px; }
|
|
55
|
+
button.ghost:hover { background: var(--surface-2); }
|
|
56
|
+
select, input, textarea { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px; width: 100%; }
|
|
57
|
+
textarea { resize: vertical; min-height: 90px; }
|
|
58
|
+
label.f { display: block; font-size: 12px; color: var(--ink-2); margin: 10px 0 4px; }
|
|
59
|
+
main { display: grid; grid-template-columns: minmax(320px, 420px) minmax(0, 1fr); gap: 20px; padding: 20px; align-items: start; }
|
|
60
|
+
@media (max-width: 960px) { main { grid-template-columns: 1fr; padding: 16px; } aside { position: static !important; max-height: none !important; } }
|
|
61
|
+
aside { position: sticky; top: 70px; max-height: calc(100vh - 90px); overflow: auto; }
|
|
62
|
+
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px; }
|
|
63
|
+
.card + .card { margin-top: 16px; }
|
|
64
|
+
.qedit { border: 1px solid var(--border); border-radius: 10px; padding: 10px; margin-top: 10px; background: var(--page); }
|
|
65
|
+
.qedit .row { display: flex; gap: 8px; align-items: center; }
|
|
66
|
+
.opt { display: grid; grid-template-columns: 90px 1fr 28px; gap: 6px; margin-top: 6px; }
|
|
67
|
+
.opt.nokey { grid-template-columns: 1fr 28px; }
|
|
68
|
+
.hint { color: var(--muted); font-size: 12px; }
|
|
69
|
+
.actions { display: flex; gap: 8px; align-items: center; margin-top: 14px; }
|
|
70
|
+
.hist { list-style: none; margin: 0; padding: 0; }
|
|
71
|
+
.hist li { padding: 8px; border-radius: 8px; cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
|
|
72
|
+
.hist li:hover { background: var(--surface-2); }
|
|
73
|
+
.hist .q { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
74
|
+
.hist .a { color: var(--ink-2); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
75
|
+
.empty { color: var(--muted); text-align: center; padding: 60px 20px; }
|
|
76
|
+
.empty h2 { color: var(--ink); font-weight: 600; font-size: 18px; }
|
|
77
|
+
.result { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; margin-bottom: 20px; overflow: hidden; }
|
|
78
|
+
.result.stale { opacity: .45; }
|
|
79
|
+
.rhead { padding: 18px 20px; display: grid; grid-template-columns: 1fr auto; gap: 12px 20px; border-bottom: 1px solid var(--border); }
|
|
80
|
+
.rhead .qname { color: var(--muted); font-size: 12px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
|
|
81
|
+
.rhead .qtext { font-size: 16px; font-weight: 600; margin-top: 4px; }
|
|
82
|
+
.hero { text-align: right; }
|
|
83
|
+
.hero .v { font-size: 44px; font-weight: 600; line-height: 1; }
|
|
84
|
+
.hero .l { color: var(--ink-2); font-size: 12px; margin-top: 4px; }
|
|
85
|
+
.answer { grid-column: 1 / -1; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
|
|
86
|
+
.answer .opt-text { font-size: 18px; font-weight: 600; }
|
|
87
|
+
.status { display: inline-flex; gap: 6px; align-items: center; padding: 3px 10px 3px 6px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--ink); }
|
|
88
|
+
.status .ic { width: 16px; height: 16px; border-radius: 50%; display: inline-grid; place-items: center; color: #fff; font-size: 11px; font-weight: 700; }
|
|
89
|
+
.step { padding: 18px 20px; border-bottom: 1px solid var(--border); }
|
|
90
|
+
.step:last-child { border-bottom: 0; }
|
|
91
|
+
.step h3 { margin: 0 0 2px; font-size: 14px; display: flex; gap: 10px; align-items: center; }
|
|
92
|
+
.step h3 .n { width: 22px; height: 22px; border-radius: 50%; background: var(--surface-2); color: var(--ink-2); display: inline-grid; place-items: center; font-size: 12px; font-weight: 700; flex: none; }
|
|
93
|
+
.step .sub { color: var(--ink-2); margin: 0 0 12px 32px; font-size: 13px; }
|
|
94
|
+
.step .body { margin-left: 32px; }
|
|
95
|
+
@media (max-width: 600px) { .step .body, .step .sub { margin-left: 0; } .hero .v { font-size: 34px; } }
|
|
96
|
+
.tabs { display: inline-flex; background: var(--surface-2); border-radius: 8px; padding: 2px; gap: 2px; margin-bottom: 8px; }
|
|
97
|
+
.tabs button { border: 0; background: transparent; padding: 4px 10px; border-radius: 6px; font-size: 12px; color: var(--ink-2); }
|
|
98
|
+
.tabs button.on { background: var(--surface); color: var(--ink); box-shadow: 0 0 0 1px var(--border); }
|
|
99
|
+
.prompt { font: 12px/1.55 ui-monospace, SFMono-Regular, Menlo, monospace; white-space: pre-wrap; word-break: break-word; background: var(--page); border: 1px solid var(--border); border-radius: 8px; padding: 12px; max-height: 320px; overflow: auto; }
|
|
100
|
+
.prompt .template { color: var(--muted); }
|
|
101
|
+
.prompt .state { background: var(--w-state); }
|
|
102
|
+
.prompt .question { background: var(--w-question); }
|
|
103
|
+
.prompt .option { background: var(--w-option); }
|
|
104
|
+
.prompt .prefill { background: var(--w-prefill); font-weight: 700; }
|
|
105
|
+
.prompt .cursor { display: inline-block; width: 7px; height: 14px; background: var(--ink); vertical-align: text-bottom; animation: blink 1s steps(1) infinite; }
|
|
106
|
+
@keyframes blink { 50% { opacity: 0; } }
|
|
107
|
+
.keyrow { display: flex; gap: 14px; flex-wrap: wrap; margin: 8px 0 0; font-size: 12px; color: var(--ink-2); }
|
|
108
|
+
.keyrow span { display: inline-flex; align-items: center; gap: 6px; }
|
|
109
|
+
.sw { width: 12px; height: 12px; border-radius: 3px; display: inline-block; flex: none; }
|
|
110
|
+
.lk { width: 14px; height: 2px; border-radius: 1px; display: inline-block; flex: none; }
|
|
111
|
+
.pack { display: grid; grid-template-columns: 1fr 200px; gap: 20px; align-items: start; }
|
|
112
|
+
@media (max-width: 760px) { .pack { grid-template-columns: 1fr; } }
|
|
113
|
+
.packrow { margin-bottom: 14px; }
|
|
114
|
+
.packrow .lbl { font-size: 12px; color: var(--ink-2); margin-bottom: 4px; display: flex; justify-content: space-between; }
|
|
115
|
+
.packrow .bar { display: flex; gap: 2px; height: 22px; }
|
|
116
|
+
.packrow .bar div { border-radius: 4px; min-width: 2px; }
|
|
117
|
+
.packrow .bar .gap { width: 10px; background: transparent; }
|
|
118
|
+
.bignum { font-size: 22px; font-weight: 600; }
|
|
119
|
+
canvas.mask { width: 200px; height: 200px; border-radius: 6px; border: 1px solid var(--border); image-rendering: pixelated; }
|
|
120
|
+
.chart { position: relative; min-width: 0; }
|
|
121
|
+
#out, .result, .step, .body { min-width: 0; }
|
|
122
|
+
.chart svg { display: block; width: 100%; overflow: visible; }
|
|
123
|
+
.chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
|
|
124
|
+
.chart .dl { fill: var(--ink-2); font-size: 12px; }
|
|
125
|
+
.tip { position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; box-shadow: 0 4px 16px rgba(0,0,0,.12); font-size: 12px; min-width: 160px; display: none; z-index: 3; }
|
|
126
|
+
.tip .t { color: var(--muted); margin-bottom: 4px; }
|
|
127
|
+
.tip .r { display: grid; grid-template-columns: 14px auto 1fr; gap: 6px; align-items: center; }
|
|
128
|
+
.tip .r b { font-variant-numeric: tabular-nums; }
|
|
129
|
+
.tip .r span:last-child { color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
|
|
130
|
+
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 8px; font-size: 12px; color: var(--ink-2); }
|
|
131
|
+
.legend span { display: inline-flex; align-items: center; gap: 6px; max-width: 320px; }
|
|
132
|
+
.legend span i { font-style: normal; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
133
|
+
.orders { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
|
|
134
|
+
@media (max-width: 760px) { .orders { grid-template-columns: 1fr; } }
|
|
135
|
+
.orders h4 { margin: 0 0 8px; font-size: 12px; color: var(--ink-2); font-weight: 600; display: flex; gap: 6px; align-items: center; }
|
|
136
|
+
.hbars { display: grid; grid-template-columns: auto minmax(0, 1.4fr) minmax(56px, 1fr) 48px; gap: 6px 8px; align-items: center; font-size: 12px; }
|
|
137
|
+
.hbars .letter { font: 700 12px ui-monospace, Menlo, monospace; color: var(--ink-2); }
|
|
138
|
+
.hbars .txt { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--ink); }
|
|
139
|
+
.hbars .track { height: 12px; position: relative; }
|
|
140
|
+
.hbars .track div { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 0 4px 4px 0; min-width: 1px; }
|
|
141
|
+
.hbars .val { text-align: right; font-variant-numeric: tabular-nums; color: var(--ink-2); }
|
|
142
|
+
.hbars .win .txt, .hbars .win.val { font-weight: 600; }
|
|
143
|
+
.toptok { margin-top: 10px; font-size: 12px; color: var(--ink-2); }
|
|
144
|
+
.toptok code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
|
|
145
|
+
.cmp { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(120px, 2fr) 60px; gap: 4px 10px; align-items: center; font-size: 12px; }
|
|
146
|
+
.cmp .txt { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
147
|
+
.cmp .pair { display: grid; gap: 2px; }
|
|
148
|
+
.cmp .pair div { height: 8px; border-radius: 0 4px 4px 0; min-width: 1px; }
|
|
149
|
+
.cmp .val { text-align: right; font-variant-numeric: tabular-nums; color: var(--ink-2); }
|
|
150
|
+
.cmp .hdr { color: var(--muted); font-size: 11px; }
|
|
151
|
+
.meter { position: relative; height: 56px; margin: 6px 0 4px; }
|
|
152
|
+
.meter .track { position: absolute; left: 0; right: 0; top: 22px; height: 10px; border-radius: 5px; background: var(--surface-2); overflow: hidden; }
|
|
153
|
+
.meter .zone { position: absolute; top: 0; bottom: 0; }
|
|
154
|
+
.meter .fill { position: absolute; top: 22px; left: 0; height: 10px; border-radius: 5px; }
|
|
155
|
+
.meter .tick { position: absolute; top: 14px; height: 26px; width: 0; border-left: 1px solid var(--ink-2); }
|
|
156
|
+
.meter .tick span { position: absolute; top: 28px; transform: translateX(-50%); font-size: 11px; color: var(--ink-2); white-space: nowrap; }
|
|
157
|
+
.meter .mk { position: absolute; top: 0; transform: translateX(-50%); font-size: 12px; font-weight: 700; white-space: nowrap; }
|
|
158
|
+
.meter .dot { position: absolute; top: 19px; width: 16px; height: 16px; border-radius: 50%; transform: translateX(-50%); border: 2px solid var(--surface); }
|
|
159
|
+
.foot { color: var(--muted); font-size: 12px; display: flex; gap: 16px; flex-wrap: wrap; }
|
|
160
|
+
.err { color: var(--critical); background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 12px; margin-bottom: 16px; }
|
|
161
|
+
.loading { display: flex; gap: 10px; align-items: center; color: var(--ink-2); padding: 12px 4px; }
|
|
162
|
+
.spin { width: 16px; height: 16px; border: 2px solid var(--grid); border-top-color: var(--accent); border-radius: 50%; animation: sp .8s linear infinite; }
|
|
163
|
+
@keyframes sp { to { transform: rotate(360deg); } }
|
|
164
|
+
.scale { display: flex; gap: 2px; margin-top: 12px; }
|
|
165
|
+
.scale div { flex: 1; text-align: center; font-size: 11px; color: var(--ink-2); padding: 6px 4px; background: var(--surface-2); border-radius: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
166
|
+
.scale div.on { outline: 2px solid var(--ink); outline-offset: -2px; color: var(--ink); font-weight: 600; }
|
|
167
|
+
</style>
|
|
168
|
+
</head>
|
|
169
|
+
<body>
|
|
170
|
+
<header>
|
|
171
|
+
<h1>basal · decision visualiser</h1>
|
|
172
|
+
<span class="chip" id="modelchip">loading model…</span>
|
|
173
|
+
<span class="spacer"></span>
|
|
174
|
+
<button class="ghost" id="theme" title="Toggle light / dark">◐ Theme</button>
|
|
175
|
+
</header>
|
|
176
|
+
<main>
|
|
177
|
+
<aside>
|
|
178
|
+
<div class="card">
|
|
179
|
+
<label class="f" for="ex">Load an example</label>
|
|
180
|
+
<select id="ex"><option value="">— bundled examples —</option></select>
|
|
181
|
+
<label class="f" for="state">State <span class="hint">(text or JSON the model reads)</span></label>
|
|
182
|
+
<textarea id="state" rows="5" placeholder="Klient: od wczoraj nie mogę zalogować się…"></textarea>
|
|
183
|
+
<div id="qs"></div>
|
|
184
|
+
<div class="actions">
|
|
185
|
+
<button class="primary" id="go">Decide</button>
|
|
186
|
+
<button id="addq">+ Question</button>
|
|
187
|
+
<span class="hint">⌘/Ctrl + Enter</span>
|
|
188
|
+
</div>
|
|
189
|
+
</div>
|
|
190
|
+
<div class="card">
|
|
191
|
+
<div style="display:flex;justify-content:space-between;align-items:center">
|
|
192
|
+
<strong>History</strong><button class="ghost" id="clearh">Clear</button>
|
|
193
|
+
</div>
|
|
194
|
+
<ul class="hist" id="hist"></ul>
|
|
195
|
+
</div>
|
|
196
|
+
</aside>
|
|
197
|
+
<section id="out">
|
|
198
|
+
<div class="empty">
|
|
199
|
+
<h2>Pick an example or write a question, then press Decide</h2>
|
|
200
|
+
<p>Every step the model takes is shown: the prompt it reads, the option orders it is asked in (packed into one
|
|
201
|
+
pass when there are two), how the answer forms layer by layer, the option-code readout, calibration and the final
|
|
202
|
+
call, next to the answer basal-serve returns for the same request.</p>
|
|
203
|
+
</div>
|
|
204
|
+
</section>
|
|
205
|
+
</main>
|
|
206
|
+
<div class="tip" id="tip"></div>
|
|
207
|
+
<script>
|
|
208
|
+
"use strict";
|
|
209
|
+
const $ = (s, r = document) => r.querySelector(s);
|
|
210
|
+
// blue / orange always mean option order 1 / 2; options take the remaining slots in fixed order (7th+ fold to grey)
|
|
211
|
+
const SERIES = ["--s3", "--s4", "--s5", "--s6", "--s7", "--s8"];
|
|
212
|
+
const ORDER_COLORS = ["var(--s1)", "var(--s2)"];
|
|
213
|
+
const FINAL = "var(--ink-2)";
|
|
214
|
+
const pct = (p, d = 1) => (p * 100).toFixed(d) + "%";
|
|
215
|
+
const optColor = j => j < SERIES.length ? `var(${SERIES[j]})` : "var(--neutral-mark)";
|
|
216
|
+
let INFO = null, EXAMPLES = [], last = null;
|
|
217
|
+
const EDITABLE = ["choice", "noul", "score", "multi"];
|
|
218
|
+
|
|
219
|
+
function h(tag, attrs = {}, ...kids) {
|
|
220
|
+
const e = document.createElement(tag);
|
|
221
|
+
for (const [k, v] of Object.entries(attrs || {})) {
|
|
222
|
+
if (v == null || v === false) continue;
|
|
223
|
+
if (k === "class") e.className = v;
|
|
224
|
+
else if (k === "style" && typeof v === "object") Object.assign(e.style, v);
|
|
225
|
+
else if (k.startsWith("on")) e.addEventListener(k.slice(2), v);
|
|
226
|
+
else e.setAttribute(k, v);
|
|
227
|
+
}
|
|
228
|
+
for (const k of kids.flat()) if (k != null && k !== false) e.append(k instanceof Node ? k : document.createTextNode(String(k)));
|
|
229
|
+
return e;
|
|
230
|
+
}
|
|
231
|
+
const svgEl = (tag, attrs = {}) => { const e = document.createElementNS("http://www.w3.org/2000/svg", tag); for (const [k, v] of Object.entries(attrs)) e.setAttribute(k, v); return e; };
|
|
232
|
+
const store = { get(k, d) { try { const v = localStorage.getItem(k); return v ? JSON.parse(v) : d; } catch { return d; } },
|
|
233
|
+
set(k, v) { try { localStorage.setItem(k, JSON.stringify(v)); } catch {} } };
|
|
234
|
+
|
|
235
|
+
// ---- theme ------------------------------------------------------------------------------------------------------
|
|
236
|
+
(function () {
|
|
237
|
+
const t = store.get("basal-viz-theme", null);
|
|
238
|
+
if (t) document.documentElement.dataset.theme = t;
|
|
239
|
+
$("#theme").onclick = () => {
|
|
240
|
+
const dark = getComputedStyle(document.documentElement).colorScheme === "dark";
|
|
241
|
+
document.documentElement.dataset.theme = dark ? "light" : "dark";
|
|
242
|
+
store.set("basal-viz-theme", document.documentElement.dataset.theme);
|
|
243
|
+
if (last) render(last.res, last.req, last.gold);
|
|
244
|
+
};
|
|
245
|
+
})();
|
|
246
|
+
|
|
247
|
+
// ---- tooltip ----------------------------------------------------------------------------------------------------
|
|
248
|
+
const tip = $("#tip");
|
|
249
|
+
function showTip(ev, title, rows) {
|
|
250
|
+
tip.replaceChildren(h("div", { class: "t" }, title), ...rows.map(r =>
|
|
251
|
+
h("div", { class: "r" }, h("span", { class: "lk", style: { background: r.color } }), h("b", {}, r.value), h("span", {}, r.label))));
|
|
252
|
+
tip.style.display = "block";
|
|
253
|
+
const pad = 14, w = tip.offsetWidth, hh = tip.offsetHeight;
|
|
254
|
+
let x = ev.pageX + pad, y = ev.pageY + pad;
|
|
255
|
+
if (x + w > window.scrollX + innerWidth - 8) x = ev.pageX - w - pad;
|
|
256
|
+
if (y + hh > window.scrollY + innerHeight - 8) y = ev.pageY - hh - pad;
|
|
257
|
+
tip.style.left = x + "px"; tip.style.top = y + "px";
|
|
258
|
+
}
|
|
259
|
+
const hideTip = () => { tip.style.display = "none"; };
|
|
260
|
+
|
|
261
|
+
// ---- editor -----------------------------------------------------------------------------------------------------
|
|
262
|
+
function questionEditor(name, q) {
|
|
263
|
+
const box = h("div", { class: "qedit" });
|
|
264
|
+
if (!EDITABLE.includes(q.type || "choice")) return rawEditor(name, q);
|
|
265
|
+
const type = h("select", {}, ...EDITABLE.map(t => h("option", { value: t }, t)));
|
|
266
|
+
type.value = q.type || "choice";
|
|
267
|
+
const nm = h("input", { value: name, placeholder: "name", style: { width: "110px" }, "aria-label": "Question name" });
|
|
268
|
+
const instr = h("textarea", { rows: 2, placeholder: "Question, e.g. Do którego działu skierować zgłoszenie?" });
|
|
269
|
+
instr.value = typeof q.instructions === "string" ? q.instructions : JSON.stringify(q.instructions ?? "");
|
|
270
|
+
const keys = h("select", { title: "Show option keys to the model?" }, h("option", { value: "show" }, "keys shown"), h("option", { value: "hide" }, "keys hidden"));
|
|
271
|
+
keys.value = q.option_keys || "show";
|
|
272
|
+
const opts = h("div");
|
|
273
|
+
const addOpt = (k, v) => {
|
|
274
|
+
const row = h("div", { class: "opt" });
|
|
275
|
+
const kk = h("input", { value: k ?? "", placeholder: "key", "aria-label": "Option key" });
|
|
276
|
+
const vv = h("input", { value: v == null ? "" : (typeof v === "string" ? v : JSON.stringify(v)), placeholder: "description", "aria-label": "Option description" });
|
|
277
|
+
row.append(kk, vv, h("button", { class: "ghost", title: "Remove option", onclick: () => row.remove() }, "×"));
|
|
278
|
+
opts.append(row);
|
|
279
|
+
};
|
|
280
|
+
let crit = q.criteria ?? q.levels;
|
|
281
|
+
const t0 = type.value;
|
|
282
|
+
if (t0 === "noul") { crit = crit || {}; addOpt("true", crit.true ?? ""); addOpt("false", crit.false ?? ""); }
|
|
283
|
+
else if (Array.isArray(crit)) crit.forEach((v, i) => t0 === "score" ? addOpt(String(i), v) : addOpt(v, ""));
|
|
284
|
+
else if (crit) Object.entries(crit).forEach(([k, v]) => addOpt(k, v));
|
|
285
|
+
else { addOpt("a", ""); addOpt("b", ""); }
|
|
286
|
+
const addBtn = h("button", { class: "ghost", onclick: () => addOpt("", "") }, "+ option");
|
|
287
|
+
const sync = () => {
|
|
288
|
+
const noul = type.value === "noul";
|
|
289
|
+
addBtn.style.display = noul ? "none" : ""; keys.style.display = ["noul", "multi"].includes(type.value) ? "none" : "";
|
|
290
|
+
if (noul) {
|
|
291
|
+
const rows = [...opts.children];
|
|
292
|
+
while (rows.length > 2) rows.pop().remove();
|
|
293
|
+
while (opts.children.length < 2) addOpt("", "");
|
|
294
|
+
const [a, b] = opts.children; a.children[0].value = "true"; b.children[0].value = "false";
|
|
295
|
+
a.children[0].disabled = b.children[0].disabled = true;
|
|
296
|
+
} else [...opts.children].forEach(r => r.children[0].disabled = false);
|
|
297
|
+
};
|
|
298
|
+
type.onchange = sync;
|
|
299
|
+
box.append(
|
|
300
|
+
h("div", { class: "row" }, nm, type, keys, h("button", { class: "ghost", title: "Remove question", onclick: () => box.remove() }, "×")),
|
|
301
|
+
h("label", { class: "f" }, "Question"), instr,
|
|
302
|
+
h("label", { class: "f" }, "Options ", h("span", { class: "hint" }, "key · description (noul: yes / no text, optional)")), opts, addBtn);
|
|
303
|
+
sync();
|
|
304
|
+
box.read = () => {
|
|
305
|
+
const t = type.value, rows = [...opts.children].map(r => [r.children[0].value.trim(), r.children[1].value.trim()]);
|
|
306
|
+
const out = { type: t, instructions: instr.value.trim() };
|
|
307
|
+
if (t === "noul") { const c = {}; if (rows[0][1]) c.true = rows[0][1]; if (rows[1][1]) c.false = rows[1][1]; if (Object.keys(c).length) out.criteria = c; }
|
|
308
|
+
else {
|
|
309
|
+
out.criteria = Object.fromEntries(rows.filter(r => r[0] || r[1]).map((r, i) => [r[0] || String(i), r[1] || null]));
|
|
310
|
+
if (t !== "multi") out.option_keys = keys.value;
|
|
311
|
+
}
|
|
312
|
+
return [nm.value.trim() || "q", out];
|
|
313
|
+
};
|
|
314
|
+
return box;
|
|
315
|
+
}
|
|
316
|
+
function rawEditor(name, q) { // a question type the form does not edit: its JSON, sent as written
|
|
317
|
+
const box = h("div", { class: "qedit" });
|
|
318
|
+
const nm = h("input", { value: name, placeholder: "name", style: { width: "110px" }, "aria-label": "Question name" });
|
|
319
|
+
const js = h("textarea", { rows: 6, "aria-label": "Question JSON" });
|
|
320
|
+
js.value = JSON.stringify(q, null, 2);
|
|
321
|
+
box.append(h("div", { class: "row" }, nm, h("span", { class: "chip" }, q.type), h("button", { class: "ghost", title: "Remove question", onclick: () => box.remove() }, "×")),
|
|
322
|
+
h("label", { class: "f" }, "Question ", h("span", { class: "hint" }, "JSON, as in the API")), js);
|
|
323
|
+
box.read = () => [nm.value.trim() || "q", JSON.parse(js.value)];
|
|
324
|
+
return box;
|
|
325
|
+
}
|
|
326
|
+
function loadRequest(req) {
|
|
327
|
+
$("#state").value = typeof req.state === "string" ? req.state : JSON.stringify(req.state, null, 2);
|
|
328
|
+
$("#qs").replaceChildren(...Object.entries(req.questions).map(([n, q]) => questionEditor(n, q)));
|
|
329
|
+
}
|
|
330
|
+
function readRequest() {
|
|
331
|
+
let state = $("#state").value.trim();
|
|
332
|
+
if (/^[\[{]/.test(state)) { try { state = JSON.parse(state); } catch {} }
|
|
333
|
+
const questions = {};
|
|
334
|
+
for (const b of $("#qs").children) { const [n, q] = b.read(); questions[n in questions ? n + "_" + Object.keys(questions).length : n] = q; }
|
|
335
|
+
return { state, questions };
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// ---- history ----------------------------------------------------------------------------------------------------
|
|
339
|
+
function renderHistory() {
|
|
340
|
+
const items = store.get("basal-viz-history", []);
|
|
341
|
+
$("#hist").replaceChildren(...(items.length ? items.map(it => h("li", { onclick: () => { loadRequest(it.req); decide(it.gold); } },
|
|
342
|
+
h("span", { class: "q" }, it.q), h("span", { class: "chip" }, pct(it.conf, 1)),
|
|
343
|
+
h("span", { class: "a" }, "→ " + it.a), h("span", { class: "a" }, new Date(it.t).toLocaleTimeString()))) :
|
|
344
|
+
[h("li", { style: { cursor: "default" } }, h("span", { class: "hint" }, "Decisions you make appear here."))]));
|
|
345
|
+
}
|
|
346
|
+
function pushHistory(req, res, gold) {
|
|
347
|
+
const q = res.questions[0]; if (!q) return;
|
|
348
|
+
const items = store.get("basal-viz-history", []);
|
|
349
|
+
items.unshift({ t: Date.now(), req, gold, q: q.question || q.name, a: q.options[q.answer.index] + (res.questions.length > 1 ? ` (+${res.questions.length - 1})` : ""), conf: q.answer.confidence });
|
|
350
|
+
store.set("basal-viz-history", items.slice(0, 30)); renderHistory();
|
|
351
|
+
}
|
|
352
|
+
$("#clearh").onclick = () => { store.set("basal-viz-history", []); renderHistory(); };
|
|
353
|
+
|
|
354
|
+
// ---- decide -----------------------------------------------------------------------------------------------------
|
|
355
|
+
let currentGold = null;
|
|
356
|
+
async function decide(gold) {
|
|
357
|
+
currentGold = gold === undefined ? currentGold : gold;
|
|
358
|
+
const req = readRequest();
|
|
359
|
+
if (!Object.keys(req.questions).length) return;
|
|
360
|
+
const btn = $("#go"); btn.disabled = true;
|
|
361
|
+
const out = $("#out");
|
|
362
|
+
[...out.querySelectorAll(".result")].forEach(r => r.classList.add("stale"));
|
|
363
|
+
out.querySelector(".err")?.remove(); out.querySelector(".empty")?.remove();
|
|
364
|
+
const ld = h("div", { class: "loading" }, h("div", { class: "spin" }), "Running the model — a forward pass per option order, with every layer recorded…");
|
|
365
|
+
out.prepend(ld);
|
|
366
|
+
try {
|
|
367
|
+
const r = await fetch("/api/explain", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(req) });
|
|
368
|
+
const res = await r.json();
|
|
369
|
+
if (!r.ok || res.error) throw new Error(res.error || r.statusText);
|
|
370
|
+
last = { res, req, gold: currentGold };
|
|
371
|
+
render(res, req, currentGold);
|
|
372
|
+
pushHistory(req, res, currentGold);
|
|
373
|
+
} catch (e) {
|
|
374
|
+
ld.remove();
|
|
375
|
+
out.prepend(h("div", { class: "err" }, "Error: " + e.message));
|
|
376
|
+
[...out.querySelectorAll(".result")].forEach(r => r.classList.remove("stale"));
|
|
377
|
+
} finally { btn.disabled = false; }
|
|
378
|
+
}
|
|
379
|
+
$("#go").onclick = () => decide();
|
|
380
|
+
$("#addq").onclick = () => $("#qs").append(questionEditor("q" + ($("#qs").children.length + 1), { type: "choice" }));
|
|
381
|
+
document.addEventListener("keydown", e => { if ((e.metaKey || e.ctrlKey) && e.key === "Enter") decide(); });
|
|
382
|
+
$("#ex").onchange = e => {
|
|
383
|
+
const ex = EXAMPLES[+e.target.value]; if (!ex) return;
|
|
384
|
+
currentGold = ex.gold || null; loadRequest(ex.request); decide(currentGold);
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
// ---- rendering --------------------------------------------------------------------------------------------------
|
|
388
|
+
function verdict(conf, th) {
|
|
389
|
+
const t1 = th["0.01"], t5 = th["0.05"];
|
|
390
|
+
if (t1 == null && t5 == null) return { color: "var(--neutral-mark)", icon: "–", label: "No certified threshold", why: "the calibration file certifies no confidence level for this question type, so basal-serve defers it whenever a request sets max_error" };
|
|
391
|
+
if (t1 != null && conf >= t1) return { color: "var(--good)", icon: "✓", label: "Auto-accept", why: `confidence ≥ ${t1.toFixed(3)}: accepting every decision at least this confident gave about 1% errors on test data` };
|
|
392
|
+
if (t5 != null && conf >= t5) return { color: "var(--warning)", icon: "!", label: "Accept at 5% risk", why: `confidence ≥ ${t5.toFixed(3)}: accepting every decision at least this confident gave about 5% errors ${t1 != null ? ` (the 1% level starts at ${t1.toFixed(3)})` : " (no 1% level is certified)"}` };
|
|
393
|
+
return { color: "var(--critical)", icon: "?", label: "Route to a person", why: `below ${t5 != null ? t5.toFixed(3) : "the threshold"}: too uncertain to accept automatically` };
|
|
394
|
+
}
|
|
395
|
+
function statusChip(v) { return h("span", { class: "status", title: v.why }, h("span", { class: "ic", style: { background: v.color } }, v.icon), v.label); }
|
|
396
|
+
function step(n, title, sub, ...body) { return h("div", { class: "step" }, h("h3", {}, h("span", { class: "n" }, n), title), sub ? h("p", { class: "sub" }, sub) : null, h("div", { class: "body" }, ...body)); }
|
|
397
|
+
|
|
398
|
+
function render(res, req, gold) {
|
|
399
|
+
const out = $("#out");
|
|
400
|
+
out.replaceChildren(...res.questions.map(q => renderQuestion(q, gold && gold[q.name])));
|
|
401
|
+
out.append(h("div", { class: "foot" }, `${res.model} · total ${Math.round(res.total_ms)} ms`));
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function renderQuestion(q, goldKey) {
|
|
405
|
+
const a = q.answer, v = verdict(a.confidence, q.thresholds);
|
|
406
|
+
const wrap = h("div", { class: "result" });
|
|
407
|
+
const typeLabel = q.kind === "multi" ? "multi-label (yes / no per label)" : ({ choice: "choice", noul: "yes / no", score: "score" }[q.type] + (q.kind !== q.type ? ` (${q.kind})` : ""));
|
|
408
|
+
const two = q.orders.length > 1;
|
|
409
|
+
let n = 0;
|
|
410
|
+
let heroV = pct(a.confidence, 1), heroL = "confidence";
|
|
411
|
+
if (q.type === "noul") { heroV = pct(a.noul, 1); heroL = "P(yes)"; }
|
|
412
|
+
if (q.type === "score") { heroV = a.score.toFixed(2); heroL = `expected level (0–${q.options.length - 1})`; }
|
|
413
|
+
const goldEl = goldKey != null ? (goldKey === a.key
|
|
414
|
+
? h("span", { class: "status" }, h("span", { class: "ic", style: { background: "var(--good)" } }, "✓"), "matches the expected answer")
|
|
415
|
+
: h("span", { class: "status" }, h("span", { class: "ic", style: { background: "var(--critical)" } }, "✗"), "expected: " + q.options[q.keys.indexOf(goldKey)])) : null;
|
|
416
|
+
wrap.append(h("div", { class: "rhead" },
|
|
417
|
+
h("div", {}, h("div", { class: "qname" }, h("span", { class: "chip" }, q.name), h("span", { class: "chip" }, typeLabel), h("span", { class: "chip" }, q.lang === "pl" ? "Polish prompt" : "English prompt")),
|
|
418
|
+
h("div", { class: "qtext" }, q.question)),
|
|
419
|
+
h("div", { class: "hero" }, h("div", { class: "v" }, heroV), h("div", { class: "l" }, heroL)),
|
|
420
|
+
h("div", { class: "answer" }, h("span", { class: "hint" }, "Answer"), h("span", { class: "opt-text" }, q.options[a.index]), statusChip(v), goldEl)));
|
|
421
|
+
|
|
422
|
+
wrap.append(step(++n, "What the model reads",
|
|
423
|
+
`Your question becomes a chat prompt with an option code before each option (prompt format ${INFO?.prompt_format ?? "?"}). The assistant turn is pre-filled with {"answer": " so the very next token must be a code. ` +
|
|
424
|
+
(two ? "It is asked twice: options in original and in reversed order." : "It is asked once, in the order given (this model's default; basal-serve --orders 2 adds the reversed order)."),
|
|
425
|
+
promptView(q)));
|
|
426
|
+
if (two) wrap.append(step(++n, "Both orders in one forward pass",
|
|
427
|
+
"The two prompts share everything up to the options. The server packs them into one row: the shared prefix is computed once, and each option block attends only to the prefix and to itself.",
|
|
428
|
+
packView(q)));
|
|
429
|
+
wrap.append(step(++n, "How the decision forms, layer by layer",
|
|
430
|
+
`After each of the ${q.layers.length} layers, the hidden state at the answer position is read through the final norm and output head ("logit lens")${two ? ", averaged over both orders" : ""}. Early layers were not trained to be read this way, so treat them as a rough picture; the last layer is the real answer.`,
|
|
431
|
+
layerChart(q)));
|
|
432
|
+
wrap.append(step(++n, "Option-code readout",
|
|
433
|
+
"Only the logits of the option codes are kept and turned into probabilities." + (two ? " The same option has a different code in each order." : ""),
|
|
434
|
+
readoutView(q)));
|
|
435
|
+
wrap.append(step(++n, two ? "Average both orders, then calibrate" : "Calibrate",
|
|
436
|
+
(two ? "Probabilities are mapped back to the options and averaged, which cancels most of the preference for a position. Then t" : "T") +
|
|
437
|
+
`he temperature of the calibration file (T = ${q.temperature.toFixed(3)} for ${q.type} with ${q.options.length} options) makes the confidence match the observed accuracy.`,
|
|
438
|
+
averageView(q)));
|
|
439
|
+
wrap.append(step(++n, "Decision",
|
|
440
|
+
"The top option is the answer. Its calibrated confidence is compared with the thresholds the calibration file certifies for this question type.",
|
|
441
|
+
decisionView(q, v)));
|
|
442
|
+
const t = q.timing_ms;
|
|
443
|
+
wrap.append(h("div", { class: "step foot" },
|
|
444
|
+
h("span", {}, `${INFO?.mode ?? "engine"} forward as served: ${Math.round(t.packed)} ms`),
|
|
445
|
+
h("span", {}, `${two ? "two separate forwards" : "a forward"} with every layer recorded: ${Math.round(t.separate_with_lens)} ms`),
|
|
446
|
+
h("span", {}, `served vs recorded readout, max difference: ${q.packed_max_diff.toExponential(1)}`),
|
|
447
|
+
q.served_max_diff != null ? h("span", {}, `basal-serve's answer vs this page: max difference ${q.served_max_diff.toExponential(1)}`) : null));
|
|
448
|
+
return wrap;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function promptView(q) {
|
|
452
|
+
const box = h("div");
|
|
453
|
+
const pre = h("div", { class: "prompt" });
|
|
454
|
+
const show = i => {
|
|
455
|
+
tabs.querySelectorAll("button").forEach((b, k) => b.classList.toggle("on", k === i));
|
|
456
|
+
const o = q.orders[i];
|
|
457
|
+
pre.replaceChildren(...o.segments.map(s => h("span", { class: s.kind }, s.text)), h("span", { class: "cursor", title: "the model's next token" }));
|
|
458
|
+
count.textContent = `${o.n_tokens} tokens · option order ${o.perm.map(j => o.letters[o.perm.indexOf(j)] + "=" + (j + 1)).join(" ")}`;
|
|
459
|
+
};
|
|
460
|
+
const tabs = h("div", { class: "tabs" }, ...q.orders.map((_, i) => h("button", { onclick: () => show(i) },
|
|
461
|
+
h("span", { class: "lk", style: { background: ORDER_COLORS[i], marginRight: "6px", verticalAlign: "middle" } }), q.orders.length < 2 ? "Option order (as given)" : i ? "Order 2 (reversed)" : "Order 1 (original)")));
|
|
462
|
+
const count = h("span", { class: "hint", style: { marginLeft: "10px" } });
|
|
463
|
+
box.append(h("div", {}, tabs, count), pre, h("div", { class: "keyrow" },
|
|
464
|
+
...[["var(--w-state)", "state"], ["var(--w-question)", "question"], ["var(--w-option)", "options"], ["var(--w-prefill)", "answer pre-fill"]].map(([c, l]) => h("span", {}, h("span", { class: "sw", style: { background: c } }), l)),
|
|
465
|
+
h("span", {}, h("span", { class: "sw", style: { background: "transparent", border: "1px solid var(--muted)" } }), "fixed template (grey text)")));
|
|
466
|
+
show(0);
|
|
467
|
+
return box;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function packView(q) {
|
|
471
|
+
const p = q.packing, total = p.prefix + p.tails.reduce((a, b) => a + b, 0), max = p.separate;
|
|
472
|
+
const seg = (n, c, title) => h("div", { style: { flex: `${n} 0 0`, background: c }, title: `${title}: ${n} tokens`,
|
|
473
|
+
onpointermove: e => showTip(e, title, [{ color: c, value: n + " tokens", label: "" }]), onpointerleave: hideTip });
|
|
474
|
+
const pre = "var(--neutral-mark)";
|
|
475
|
+
const sepBar = h("div", { class: "bar" }, seg(p.prefix, pre, "Shared prefix (order 1)"), seg(p.tails[0], ORDER_COLORS[0], "Order 1 options"), h("div", { class: "gap" }),
|
|
476
|
+
seg(p.prefix, pre, "Shared prefix again (order 2)"), seg(p.tails[1] || 0, ORDER_COLORS[1], "Order 2 options"));
|
|
477
|
+
const packBar = h("div", { class: "bar" }, seg(p.prefix, pre, "Shared prefix (computed once)"), seg(p.tails[0], ORDER_COLORS[0], "Order 1 options"),
|
|
478
|
+
seg(p.tails[1] || 0, ORDER_COLORS[1], "Order 2 options"), h("div", { style: { flex: `${max - total} 0 0` } }));
|
|
479
|
+
const saved = 1 - total / max;
|
|
480
|
+
const left = h("div", {},
|
|
481
|
+
h("div", { class: "packrow" }, h("div", { class: "lbl" }, h("span", {}, "Two separate prompts"), h("span", {}, `${max} tokens`)), sepBar),
|
|
482
|
+
h("div", { class: "packrow" }, h("div", { class: "lbl" }, h("span", {}, "Packed into one row (what the server runs)"), h("span", {}, `${total} tokens`)), packBar),
|
|
483
|
+
h("div", {}, h("span", { class: "bignum" }, pct(saved, 0)), h("span", { class: "hint" }, " fewer tokens than two separate prompts: the second order costs only its options")),
|
|
484
|
+
h("div", { class: "keyrow" }, h("span", {}, h("span", { class: "sw", style: { background: pre } }), `shared prefix · ${p.prefix}`),
|
|
485
|
+
h("span", {}, h("span", { class: "sw", style: { background: ORDER_COLORS[0] } }), `order 1 · ${p.tails[0]}`),
|
|
486
|
+
h("span", {}, h("span", { class: "sw", style: { background: ORDER_COLORS[1] } }), `order 2 · ${p.tails[1] || 0}`)));
|
|
487
|
+
const cv = h("canvas", { class: "mask", width: 200, height: 200, "aria-label": "Attention mask of the packed row" });
|
|
488
|
+
requestAnimationFrame(() => drawMask(cv, p));
|
|
489
|
+
return h("div", { class: "pack" }, left, h("div", {}, cv, h("div", { class: "hint", style: { marginTop: "4px" } },
|
|
490
|
+
"Attention mask: row = token, column = what it may look at. Order 2 never sees order 1.")));
|
|
491
|
+
}
|
|
492
|
+
function drawMask(cv, p) {
|
|
493
|
+
const segs = [[0, p.prefix]].concat(p.tails.map((n, i) => [i + 1, n]));
|
|
494
|
+
const lab = []; segs.forEach(([g, n]) => { for (let i = 0; i < n; i++) lab.push(g); });
|
|
495
|
+
const N = lab.length, S = 200, ctx = cv.getContext("2d"), css = getComputedStyle(document.documentElement);
|
|
496
|
+
const col = g => css.getPropertyValue(g === 0 ? "--neutral-mark" : g === 1 ? "--s1" : "--s2").trim();
|
|
497
|
+
ctx.fillStyle = css.getPropertyValue("--surface-2").trim(); ctx.fillRect(0, 0, S, S);
|
|
498
|
+
const c = S / N;
|
|
499
|
+
for (let i = 0; i < N; i++) {
|
|
500
|
+
ctx.fillStyle = col(lab[i]);
|
|
501
|
+
let run = -1;
|
|
502
|
+
for (let j = 0; j <= i + 1; j++) {
|
|
503
|
+
const ok = j <= i && (lab[j] === 0 || lab[j] === lab[i]);
|
|
504
|
+
if (ok && run < 0) run = j;
|
|
505
|
+
if (!ok && run >= 0) { ctx.fillRect(run * c, i * c, (j - run) * c + 0.5, c + 0.5); run = -1; }
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
function layerChart(q) {
|
|
511
|
+
const box = h("div", { class: "chart" });
|
|
512
|
+
const k = q.options.length, L = q.layers.length;
|
|
513
|
+
const draw = () => {
|
|
514
|
+
const W = Math.max(300, box.clientWidth || 640), H = 256, m = { l: 36, r: k <= 4 ? 150 : 16, t: 10, b: 42 };
|
|
515
|
+
const x = i => m.l + (i / (L - 1)) * (W - m.l - m.r), y = p => m.t + (1 - p) * (H - m.t - m.b);
|
|
516
|
+
const svg = svgEl("svg", { viewBox: `0 0 ${W} ${H}`, height: H, role: "img", "aria-label": "Probability of each option after every layer" });
|
|
517
|
+
for (const g of [0, 0.25, 0.5, 0.75, 1]) {
|
|
518
|
+
svg.append(svgEl("line", { x1: m.l, x2: W - m.r, y1: y(g), y2: y(g), stroke: g === 0 ? "var(--axis)" : "var(--grid)", "stroke-width": 1 }));
|
|
519
|
+
const t = svgEl("text", { x: m.l - 6, y: y(g) + 4, "text-anchor": "end" }); t.textContent = pct(g, 0); svg.append(t);
|
|
520
|
+
}
|
|
521
|
+
const step = L > 40 ? 10 : 5;
|
|
522
|
+
for (let i = 0; i < L; i++) if ((i + 1) % step === 0 || i === 0) {
|
|
523
|
+
const t = svgEl("text", { x: x(i), y: H - m.b + 16, "text-anchor": "middle" }); t.textContent = i + 1; svg.append(t);
|
|
524
|
+
}
|
|
525
|
+
const xt = svgEl("text", { x: (m.l + W - m.r) / 2, y: H - 4, "text-anchor": "middle" }); xt.textContent = "layer"; svg.append(xt);
|
|
526
|
+
const order = [...Array(k).keys()].sort((a, b) => q.calibrated[a] - q.calibrated[b]); // winner drawn last (on top)
|
|
527
|
+
for (const j of order) {
|
|
528
|
+
const d = q.layers.map((p, i) => `${i ? "L" : "M"}${x(i).toFixed(1)},${y(p[j]).toFixed(1)}`).join("");
|
|
529
|
+
svg.append(svgEl("path", { d, fill: "none", stroke: optColor(j), "stroke-width": 2, "stroke-linejoin": "round", "stroke-linecap": "round" }));
|
|
530
|
+
svg.append(svgEl("circle", { cx: x(L - 1), cy: y(q.layers[L - 1][j]), r: 4, fill: optColor(j), stroke: "var(--surface)", "stroke-width": 2 }));
|
|
531
|
+
}
|
|
532
|
+
if (k <= 4) { // direct end labels, nudged apart only when they would overlap
|
|
533
|
+
const ys = order.map(j => ({ j, y: y(q.layers[L - 1][j]) })).sort((a, b) => a.y - b.y);
|
|
534
|
+
for (let i = 1; i < ys.length; i++) if (ys[i].y - ys[i - 1].y < 14) ys[i].y = ys[i - 1].y + 14;
|
|
535
|
+
for (const { j, y: yy } of ys) {
|
|
536
|
+
const t = svgEl("text", { x: x(L - 1) + 10, y: yy + 4, class: "dl" });
|
|
537
|
+
const s = q.options[j]; t.textContent = (s.length > 18 ? s.slice(0, 17) + "…" : s) + " " + pct(q.layers[L - 1][j], 0); svg.append(t);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
const cross = svgEl("line", { y1: m.t, y2: H - m.b, stroke: "var(--ink-2)", "stroke-width": 1, visibility: "hidden" });
|
|
541
|
+
svg.append(cross);
|
|
542
|
+
const hit = svgEl("rect", { x: m.l, y: m.t, width: W - m.l - m.r, height: H - m.t - m.b, fill: "transparent" });
|
|
543
|
+
hit.addEventListener("pointermove", e => {
|
|
544
|
+
const r = svg.getBoundingClientRect(), px = (e.clientX - r.left) * (W / r.width);
|
|
545
|
+
const i = Math.max(0, Math.min(L - 1, Math.round((px - m.l) / (W - m.l - m.r) * (L - 1))));
|
|
546
|
+
cross.setAttribute("x1", x(i)); cross.setAttribute("x2", x(i)); cross.setAttribute("visibility", "visible");
|
|
547
|
+
const rows = [...Array(k).keys()].sort((a, b) => q.layers[i][b] - q.layers[i][a]).map(j => ({ color: optColor(j), value: pct(q.layers[i][j]), label: q.options[j] }));
|
|
548
|
+
showTip(e, `after layer ${i + 1} of ${L}${i === L - 1 ? " (final)" : ""}`, rows);
|
|
549
|
+
});
|
|
550
|
+
hit.addEventListener("pointerleave", () => { cross.setAttribute("visibility", "hidden"); hideTip(); });
|
|
551
|
+
svg.append(hit);
|
|
552
|
+
box.replaceChildren(svg, legend(q.options.map((o, j) => ({ color: optColor(j), label: o, line: true }))));
|
|
553
|
+
};
|
|
554
|
+
let drawnW = 0;
|
|
555
|
+
new ResizeObserver(() => { const w = Math.round(box.clientWidth); if (w && w !== drawnW) { drawnW = w; draw(); } }).observe(box);
|
|
556
|
+
// first layer where the final winner leads and stays ahead
|
|
557
|
+
const w = q.answer.index;
|
|
558
|
+
let settled = L;
|
|
559
|
+
for (let i = L - 1; i >= 0; i--) { const p = q.layers[i]; if (p.indexOf(Math.max(...p)) === w) settled = i + 1; else break; }
|
|
560
|
+
return h("div", {}, box, h("p", { class: "hint", style: { margin: "8px 0 0" } },
|
|
561
|
+
`The final answer takes the lead at layer ${settled} of ${L} and keeps it to the end.`));
|
|
562
|
+
}
|
|
563
|
+
function legend(items) {
|
|
564
|
+
return h("div", { class: "legend" }, ...items.map(it => h("span", { title: it.label },
|
|
565
|
+
h("span", { class: it.line ? "lk" : "sw", style: { background: it.color } }), h("i", {}, it.label))));
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
function readoutView(q) {
|
|
569
|
+
const cols = q.orders.map((o, i) => {
|
|
570
|
+
const best = o.letter_probs.indexOf(Math.max(...o.letter_probs));
|
|
571
|
+
const grid = h("div", { class: "hbars" });
|
|
572
|
+
o.letters.forEach((letter, pos) => {
|
|
573
|
+
const j = o.perm[pos], p = o.letter_probs[pos], win = pos === best ? "win" : "";
|
|
574
|
+
grid.append(h("span", { class: "letter" }, letter), h("span", { class: "txt " + win, title: q.options[j] }, q.options[j]),
|
|
575
|
+
h("div", { class: "track", onpointermove: e => showTip(e, `order ${i + 1} · code ${letter}`, [{ color: ORDER_COLORS[i], value: pct(p, 2), label: q.options[j] }]), onpointerleave: hideTip },
|
|
576
|
+
h("div", { style: { width: Math.max(0.3, p * 100) + "%", background: ORDER_COLORS[i] } })),
|
|
577
|
+
h("span", { class: "val " + win }, pct(p)));
|
|
578
|
+
});
|
|
579
|
+
const top = o.top_tokens.slice(0, 3).map(([t, p]) => h("span", {}, h("code", {}, JSON.stringify(t)), " " + pct(p) + " "));
|
|
580
|
+
return h("div", {}, h("h4", {}, h("span", { class: "lk", style: { background: ORDER_COLORS[i] } }), q.orders.length < 2 ? "Option order (as given)" : i ? "Order 2 (reversed)" : "Order 1 (original)"), grid,
|
|
581
|
+
h("div", { class: "toptok" }, "Unrestricted next token: ", ...top));
|
|
582
|
+
});
|
|
583
|
+
return h("div", { class: "orders" }, ...cols);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
function averageView(q) {
|
|
587
|
+
const k = q.options.length;
|
|
588
|
+
const g = h("div", { class: "cmp" }, h("span", { class: "hdr" }, "option"), h("span", { class: "hdr" }, "order 1 · order 2 → average"), h("span", { class: "hdr", style: { textAlign: "right" } }, "average"));
|
|
589
|
+
for (let j = 0; j < k; j++) {
|
|
590
|
+
const a = q.orders[0].canon[j], b = q.orders[1]?.canon[j] ?? a;
|
|
591
|
+
g.append(h("span", { class: "txt", title: q.options[j] }, q.options[j]),
|
|
592
|
+
h("div", { class: "pair", onpointermove: e => showTip(e, q.options[j], [
|
|
593
|
+
{ color: ORDER_COLORS[0], value: pct(a, 2), label: "order 1" }, { color: ORDER_COLORS[1], value: pct(b, 2), label: "order 2" },
|
|
594
|
+
{ color: "var(--neutral-mark)", value: pct(q.mean[j], 2), label: "average" }]), onpointerleave: hideTip },
|
|
595
|
+
h("div", { style: { width: Math.max(.3, a * 100) + "%", background: ORDER_COLORS[0] } }),
|
|
596
|
+
h("div", { style: { width: Math.max(.3, b * 100) + "%", background: ORDER_COLORS[1] } }),
|
|
597
|
+
h("div", { style: { width: Math.max(.3, q.mean[j] * 100) + "%", background: "var(--neutral-mark)" } })),
|
|
598
|
+
h("span", { class: "val" }, pct(q.mean[j])));
|
|
599
|
+
}
|
|
600
|
+
const gap = Math.max(...q.options.map((_, j) => Math.abs(q.orders[0].canon[j] - (q.orders[1]?.canon[j] ?? q.orders[0].canon[j]))));
|
|
601
|
+
const c = h("div", { class: "cmp", style: { marginTop: "18px" } }, h("span", { class: "hdr" }, "option"), h("span", { class: "hdr" }, q.orders.length > 1 ? "average → calibrated" : "raw → calibrated"), h("span", { class: "hdr", style: { textAlign: "right" } }, "final"));
|
|
602
|
+
for (let j = 0; j < k; j++) {
|
|
603
|
+
const win = j === q.answer.index;
|
|
604
|
+
c.append(h("span", { class: "txt", style: { fontWeight: win ? 600 : 400 }, title: q.options[j] }, q.options[j]),
|
|
605
|
+
h("div", { class: "pair", onpointermove: e => showTip(e, q.options[j], [
|
|
606
|
+
{ color: "var(--neutral-mark)", value: pct(q.mean[j], 2), label: "average" }, { color: FINAL, value: pct(q.calibrated[j], 2), label: `calibrated (T=${q.temperature.toFixed(3)})` }]), onpointerleave: hideTip },
|
|
607
|
+
h("div", { style: { width: Math.max(.3, q.mean[j] * 100) + "%", background: "var(--neutral-mark)" } }),
|
|
608
|
+
h("div", { style: { width: Math.max(.3, q.calibrated[j] * 100) + "%", background: FINAL } })),
|
|
609
|
+
h("span", { class: "val", style: { fontWeight: win ? 600 : 400, color: win ? "var(--ink)" : "" } }, pct(q.calibrated[j])));
|
|
610
|
+
}
|
|
611
|
+
const calLegend = legend([{ color: "var(--neutral-mark)", label: q.orders.length > 1 ? "average (raw)" : "raw" }, { color: FINAL, label: `calibrated, T = ${q.temperature.toFixed(3)}${q.temperature > 1 ? " (softens)" : q.temperature < 1 ? " (sharpens)" : ""}` }]);
|
|
612
|
+
if (q.orders.length < 2) return h("div", {}, c, calLegend);
|
|
613
|
+
return h("div", {}, g, legend([{ color: ORDER_COLORS[0], label: "order 1" }, { color: ORDER_COLORS[1], label: "order 2" }, { color: "var(--neutral-mark)", label: "average" }]),
|
|
614
|
+
h("p", { class: "hint", style: { margin: "8px 0 0" } }, `Largest disagreement between the two orders: ${pct(gap)}${gap > 0.2 ? " — the model is sensitive to option order here" : ""}.`),
|
|
615
|
+
c, calLegend);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
function decisionView(q, v) {
|
|
619
|
+
const th = q.thresholds, conf = q.answer.confidence, t1 = th["0.01"], t5 = th["0.05"];
|
|
620
|
+
const lo = 1 / q.options.length; // confidence can never be below 1/k
|
|
621
|
+
const X = p => ((p - lo) / (1 - lo) * 100).toFixed(2) + "%";
|
|
622
|
+
const m = h("div", { class: "meter", role: "img", "aria-label": `confidence ${pct(conf)}; thresholds ${t5} and ${t1}` },
|
|
623
|
+
h("div", { class: "track" }), h("div", { class: "fill", style: { width: X(conf), background: v.color, opacity: .35 } }),
|
|
624
|
+
t5 != null ? h("div", { class: "tick", style: { left: X(t5) } }, h("span", {}, `5% error · ${t5.toFixed(2)}`)) : null,
|
|
625
|
+
t1 != null ? h("div", { class: "tick", style: { left: X(t1) } }, h("span", {}, `1% error · ${t1.toFixed(2)}`)) : null,
|
|
626
|
+
h("div", { class: "dot", style: { left: X(conf), background: v.color } }),
|
|
627
|
+
h("div", { class: "mk", style: { left: X(conf) } }, pct(conf)));
|
|
628
|
+
const body = [h("div", { style: { display: "flex", justifyContent: "space-between", fontSize: "11px", color: "var(--muted)" } },
|
|
629
|
+
h("span", {}, `${pct(lo, 0)} (pure guess among ${q.options.length})`), h("span", {}, "100%")), m,
|
|
630
|
+
h("p", { style: { margin: "22px 0 0", display: "flex", gap: "8px", alignItems: "center", flexWrap: "wrap" } }, statusChip(v), h("span", { class: "hint" }, v.why + "."))];
|
|
631
|
+
if (q.type === "score") {
|
|
632
|
+
const lvl = Math.round(q.answer.score);
|
|
633
|
+
body.push(h("div", { class: "scale" }, ...q.options.map((o, i) => h("div", { class: i === lvl ? "on" : "", title: o }, `${i} · ${o}`))),
|
|
634
|
+
h("p", { class: "hint", style: { margin: "6px 0 0" } }, `Expected level ${q.answer.score.toFixed(2)} = Σ level × probability; most likely level: ${q.options[q.answer.index]}.`));
|
|
635
|
+
}
|
|
636
|
+
if (q.type === "noul") body.push(h("p", { class: "hint", style: { margin: "8px 0 0" } }, `P(yes) = ${pct(q.answer.noul, 2)} — ` +
|
|
637
|
+
(q.kind === "multi" ? `the API returns it as this label's probability in the multi answer.` : `the API returns this as "noul".`)));
|
|
638
|
+
return h("div", {}, ...body);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
// ---- boot -------------------------------------------------------------------------------------------------------
|
|
642
|
+
(async function () {
|
|
643
|
+
renderHistory();
|
|
644
|
+
try {
|
|
645
|
+
INFO = await (await fetch("/api/info")).json();
|
|
646
|
+
$("#modelchip").textContent = `${INFO.model} · ${INFO.device} · ${INFO.dtype} · ${INFO.layers} layers · prompt format ${INFO.prompt_format} · ${INFO.orders} option order${INFO.orders === 1 ? "" : "s"}`;
|
|
647
|
+
EXAMPLES = await (await fetch("/api/examples")).json();
|
|
648
|
+
const sel = $("#ex"), groups = {};
|
|
649
|
+
EXAMPLES.forEach((e, i) => {
|
|
650
|
+
groups[e.file] ??= sel.appendChild(h("optgroup", { label: e.file }));
|
|
651
|
+
const q = Object.values(e.request.questions)[0];
|
|
652
|
+
const txt = typeof q.instructions === "string" ? q.instructions : "";
|
|
653
|
+
groups[e.file].append(h("option", { value: i }, `${e.id} — ${txt.slice(0, 60)}${Object.keys(e.request.questions).length > 1 ? ` (+${Object.keys(e.request.questions).length - 1} more)` : ""}`));
|
|
654
|
+
});
|
|
655
|
+
loadRequest(EXAMPLES[0].request); currentGold = EXAMPLES[0].gold || null;
|
|
656
|
+
} catch (e) { $("#modelchip").textContent = "server not reachable"; }
|
|
657
|
+
})();
|
|
658
|
+
</script>
|
|
659
|
+
</body>
|
|
660
|
+
</html>
|