naklitechie commited on
Commit
231d934
·
verified ·
1 Parent(s): 8427e52

Sync from GitHub

Browse files
classic/engines/lora.js CHANGED
@@ -75,12 +75,72 @@ ${QB}
75
 
76
  const STORAGE = 0x80, COPY_SRC = 0x04, COPY_DST = 0x08;
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  // Reads a LoRA GGUF onto the engine's GPU and binds it to the matmuls the engine registered (engine.loraSites:
79
  // tensor name → { layer, site, d: [x, y], bt: [x, y] | null }). Returns the adapter handle setAdapter() takes.
80
  export async function loadLora(engine, source, { scale = 1 } = {}) {
81
  // a URL, a Blob (a File from OPFS or a picker), an ArrayBuffer or a Uint8Array
82
- const bytes = source instanceof Uint8Array ? source : new Uint8Array(typeof source === 'string'
83
- ? await (await fetch(source)).arrayBuffer() : source instanceof Blob ? await source.arrayBuffer() : source);
84
  const gguf = parseGguf(bytes), kv = gguf.kv;
85
  if (kv['adapter.type'] !== 'lora') throw new Error(`not a LoRA GGUF (adapter.type ${kv['adapter.type']})`);
86
  if (kv['general.architecture'] !== engine.arch) throw new Error(`adapter for ${kv['general.architecture']}, model is ${engine.arch}`);
@@ -95,14 +155,14 @@ export async function loadLora(engine, source, { scale = 1 } = {}) {
95
  if (engine.b && !io.bt) throw new Error(`${base}: no hook in this engine's chunked prefill`);
96
  const [N, r] = ta.dims, [r2, M] = tb.dims;
97
  if (r !== r2 || N !== w.cols || M !== w.rows) throw new Error(`${base}: adapter shape ${N}×${r} / ${r2}×${M} does not fit ${w.cols}×${w.rows}`);
98
- for (const t of [ta, tb]) if (t.type !== GGML.F16) throw new Error(`${t.name}: type ${t.type}, LoRA tensors must be F16`);
99
  R = Math.max(R, r);
100
  pairs.push({ io, ta, tb, r, N, M });
101
  }
102
  if (!pairs.length) throw new Error('the adapter has no lora_a / lora_b pairs');
103
  const upload = (t) => {
104
  const n = t.dims.reduce((x, y) => x * y, 1), buf = engine.buffer(n * 2, STORAGE | COPY_DST, 'adapter');
105
- engine.device.queue.writeBuffer(buf, 0, bytes, gguf.dataStart + t.offset, n * 2);
106
  onGpu += buf.size;
107
  return buf;
108
  };
 
75
 
76
  const STORAGE = 0x80, COPY_SRC = 0x04, COPY_DST = 0x08;
77
 
78
+ // Adapter tensors as f16 for the kernels, whatever the file stores. Published adapters come as F16 (llama.cpp's
79
+ // converter default), F32 or BF16; F32 and BF16 round to nearest even f16 here, once, at load.
80
+ const f32 = new Float32Array(1), u32 = new Uint32Array(f32.buffer);
81
+ function f32ToF16(x) {
82
+ f32[0] = x;
83
+ const b = u32[0], sign = (b >>> 16) & 0x8000, e = (b >>> 23) & 0xff, m = b & 0x7fffff;
84
+ if (e === 0xff) return sign | 0x7c00 | (m ? 0x200 : 0); // inf, nan
85
+ const he = e - 127 + 15;
86
+ if (he >= 0x1f) return sign | 0x7c00; // overflow: inf
87
+ if (he <= 0) { // subnormal or zero
88
+ if (he < -10) return sign;
89
+ const mm = m | 0x800000, shift = 14 - he, half = 1 << (shift - 1), r = mm & ((1 << shift) - 1);
90
+ let v = mm >>> shift;
91
+ if (r > half || (r === half && (v & 1))) v++;
92
+ return sign | v;
93
+ }
94
+ let v = (he << 10) | (m >>> 13);
95
+ const r = m & 0x1fff;
96
+ if (r > 0x1000 || (r === 0x1000 && (v & 1))) v++; // may carry into the exponent, as it should
97
+ return sign | v;
98
+ }
99
+ const TO_F16 = {
100
+ [GGML.F16]: (bytes, off, n) => bytes.slice(off, off + n * 2),
101
+ [GGML.F32]: (bytes, off, n) => {
102
+ const src = new DataView(bytes.buffer, bytes.byteOffset + off, n * 4), out = new Uint16Array(n);
103
+ for (let i = 0; i < n; i++) out[i] = f32ToF16(src.getFloat32(i * 4, true));
104
+ return out;
105
+ },
106
+ 30: (bytes, off, n) => { // GGML_TYPE_BF16: the top 16 bits of an f32
107
+ const src = new DataView(bytes.buffer, bytes.byteOffset + off, n * 2), out = new Uint16Array(n);
108
+ for (let i = 0; i < n; i++) { u32[0] = src.getUint16(i * 2, true) << 16; out[i] = f32ToF16(f32[0]); }
109
+ return out;
110
+ },
111
+ };
112
+
113
+ // A whole file from a URL, resumed with a Range request where a connection drops (a slow link cut a 37 MB adapter
114
+ // mid-transfer, as "Failed to fetch"). Up to `tries` attempts.
115
+ async function fetchBytes(url, tries = 5) {
116
+ const parts = [];
117
+ let got = 0, total = 0, lastErr = null;
118
+ for (let t = 0; t < tries; t++) {
119
+ try {
120
+ const r = await fetch(url, got ? { headers: { Range: `bytes=${got}-` } } : {});
121
+ if (!r.ok) throw new Error(`HTTP ${r.status} for ${url}`);
122
+ if (got && r.status !== 206) { parts.length = 0; got = 0; } // no resume: start over
123
+ if (!total) total = got + Number(r.headers.get('content-length') || 0);
124
+ for (const reader = r.body.getReader(); ;) {
125
+ const { done, value } = await reader.read();
126
+ if (done) break;
127
+ parts.push(value); got += value.length;
128
+ }
129
+ if (!total || got >= total) break;
130
+ } catch (e) { lastErr = e; if (/^HTTP [45]/.test(e.message)) throw e; }
131
+ }
132
+ if (total && got < total) throw new Error(`download stopped at ${got} of ${total} bytes: ${lastErr && lastErr.message}`);
133
+ const out = new Uint8Array(got);
134
+ let o = 0; for (const p of parts) { out.set(p, o); o += p.length; }
135
+ return out;
136
+ }
137
+
138
  // Reads a LoRA GGUF onto the engine's GPU and binds it to the matmuls the engine registered (engine.loraSites:
139
  // tensor name → { layer, site, d: [x, y], bt: [x, y] | null }). Returns the adapter handle setAdapter() takes.
140
  export async function loadLora(engine, source, { scale = 1 } = {}) {
141
  // a URL, a Blob (a File from OPFS or a picker), an ArrayBuffer or a Uint8Array
142
+ const bytes = source instanceof Uint8Array ? source : typeof source === 'string' ? await fetchBytes(source)
143
+ : new Uint8Array(source instanceof Blob ? await source.arrayBuffer() : source);
144
  const gguf = parseGguf(bytes), kv = gguf.kv;
145
  if (kv['adapter.type'] !== 'lora') throw new Error(`not a LoRA GGUF (adapter.type ${kv['adapter.type']})`);
146
  if (kv['general.architecture'] !== engine.arch) throw new Error(`adapter for ${kv['general.architecture']}, model is ${engine.arch}`);
 
155
  if (engine.b && !io.bt) throw new Error(`${base}: no hook in this engine's chunked prefill`);
156
  const [N, r] = ta.dims, [r2, M] = tb.dims;
157
  if (r !== r2 || N !== w.cols || M !== w.rows) throw new Error(`${base}: adapter shape ${N}×${r} / ${r2}×${M} does not fit ${w.cols}×${w.rows}`);
158
+ for (const t of [ta, tb]) if (!(t.type in TO_F16)) throw new Error(`${t.name}: ggml type ${t.type}; LoRA tensors must be F16, F32 or BF16`);
159
  R = Math.max(R, r);
160
  pairs.push({ io, ta, tb, r, N, M });
161
  }
162
  if (!pairs.length) throw new Error('the adapter has no lora_a / lora_b pairs');
163
  const upload = (t) => {
164
  const n = t.dims.reduce((x, y) => x * y, 1), buf = engine.buffer(n * 2, STORAGE | COPY_DST, 'adapter');
165
+ engine.device.queue.writeBuffer(buf, 0, TO_F16[t.type](bytes, gguf.dataStart + t.offset, n));
166
  onGpu += buf.size;
167
  return buf;
168
  };
classic/refs/gemma-hermes-lora.json ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "about": "llama.cpp b11669 (620d2225c), Metal (-ngl 99), adapters hermes-tool-reasoning (F32, hotdogs@baa8f20) as id 0 and opus (BF16, hotdogs@3bae4ce) as id 1, loaded with --lora-init-without-apply, LoRA id:scale 0:1,1:0 per request: llama-server /completion on the /apply-template prompt, temperature 0, max_tokens 64, on gemma-4-26B_q4_0-it.gguf (sha256 3eca3b8f6d7baf218a7dd6bba5fb59a56ee25fe2d567b6f5f589b4f697eca51d). Recorded 2026-10-31 by scripts/record-refs.mjs.",
3
+ "conversations": [
4
+ {
5
+ "name": "explain-tcp",
6
+ "messages": [
7
+ {
8
+ "role": "user",
9
+ "content": "Explain the difference between TCP and UDP to a new programmer."
10
+ }
11
+ ],
12
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nExplain the difference between TCP and UDP to a new programmer.<turn|>\n<|turn>model\n",
13
+ "content": "<|channel>thought\n\n* *Target Audience:* New programmer (needs clear, non-intimidating language, analogies, and practical examples).\n* *Topic:* Difference between TCP (Transmission Control Protocol) and UDP (User Datagram Protocol).\n* *Goal:* Explain the core concepts, key differences,",
14
+ "finish": "stop",
15
+ "tokens": 64
16
+ },
17
+ {
18
+ "name": "json-extract",
19
+ "messages": [
20
+ {
21
+ "role": "user",
22
+ "content": "Extract the name, city and age from this sentence as JSON: \"Priya, 34, moved from Pune to Lisbon last year.\""
23
+ }
24
+ ],
25
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nExtract the name, city and age from this sentence as JSON: \"Priya, 34, moved from Pune to Lisbon last year.\"<turn|>\n<|turn>model\n",
26
+ "content": "<|channel>thought\n\n* Input sentence: \"Priya, 34, moved from Pune to Lisbon last year.\"\n* Task: Extract name, city, and",
27
+ "finish": "stop",
28
+ "tokens": 35
29
+ },
30
+ {
31
+ "name": "translate-hi",
32
+ "messages": [
33
+ {
34
+ "role": "user",
35
+ "content": "Translate into Hindi: \"The library opens at nine and closes at six.\""
36
+ }
37
+ ],
38
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nTranslate into Hindi: \"The library opens at nine and closes at six.\"<turn|>\n<|turn>model\n",
39
+ "content": "<|channel>thought\n\n* Source sentence: \"The library opens at nine and closes at six.\"\n* Target language: Hindi.\n\n * \"The library\": पुस्तकालय (Pustakalaya) or simply लाइब्रेरी (Library - commonly used in spoken Hindi).\n * \"opens\": खुलता",
40
+ "finish": "stop",
41
+ "tokens": 64
42
+ },
43
+ {
44
+ "name": "word-problem",
45
+ "messages": [
46
+ {
47
+ "role": "user",
48
+ "content": "A train leaves at 14:10 and arrives at 17:45. How long is the journey? Show the steps briefly."
49
+ }
50
+ ],
51
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nA train leaves at 14:10 and arrives at 17:45. How long is the journey? Show the steps briefly.<turn|>\n<|turn>model\n",
52
+ "content": "<|channel>thought\n* Departure time: 14:10\n * Arrival time: 17:45\n * Goal: Calculate the duration of the journey.\n\n * Method 1: Subtracting hours and minutes separately.\n * Method 2: Converting",
53
+ "finish": "stop",
54
+ "tokens": 64
55
+ },
56
+ {
57
+ "name": "sql",
58
+ "messages": [
59
+ {
60
+ "role": "user",
61
+ "content": "Write a SQL query that returns the five customers with the highest total order value from tables customers(id, name) and orders(id, customer_id, amount)."
62
+ }
63
+ ],
64
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nWrite a SQL query that returns the five customers with the highest total order value from tables customers(id, name) and orders(id, customer_id, amount).<turn|>\n<|turn>model\n",
65
+ "content": "<|channel>thought\n\n* Goal: Find the top 5 customers with the highest total order value.\n* Tables: `customers` (id, name) and `orders` (id, customer_id, amount).\n* Output: Customer name and their total order amount.\n\n * `",
66
+ "finish": "stop",
67
+ "tokens": 64
68
+ },
69
+ {
70
+ "name": "list-rivers",
71
+ "messages": [
72
+ {
73
+ "role": "user",
74
+ "content": "List five major rivers of Africa, one per line, with the country where each reaches the sea."
75
+ }
76
+ ],
77
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nList five major rivers of Africa, one per line, with the country where each reaches the sea.<turn|>\n<|turn>model\n",
78
+ "content": "<|channel>thought\n* Topic: Five major rivers of Africa.\n * Format: One per line.\n * Content:",
79
+ "finish": "stop",
80
+ "tokens": 29
81
+ },
82
+ {
83
+ "name": "unicode",
84
+ "messages": [
85
+ {
86
+ "role": "user",
87
+ "content": "What do these emoji usually mean in a text message: 🙏 🔥 💀 ?"
88
+ }
89
+ ],
90
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nWhat do these emoji usually mean in a text message: 🙏 🔥 💀 ?<turn|>\n<|turn>model\n",
91
+ "content": "<|channel>thought\n\n* Emoji 1: 🙏 (Folded Hands)\n* Emoji 2: 🔥 (Fire)\n* Emoji 3: 💀 (Skull)\n* Context: Text messages (informal communication).\n\n * *Literal meaning:* Praying, high five,",
92
+ "finish": "stop",
93
+ "tokens": 64
94
+ },
95
+ {
96
+ "name": "haiku-autumn",
97
+ "messages": [
98
+ {
99
+ "role": "user",
100
+ "content": "Write a haiku about autumn leaves in a city."
101
+ }
102
+ ],
103
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nWrite a haiku about autumn leaves in a city.<turn|>\n<|turn>model\n",
104
+ "content": "<|channel>thought\n* Topic: Autumn leaves in a city.\n * Format: Haiku",
105
+ "finish": "stop",
106
+ "tokens": 21
107
+ },
108
+ {
109
+ "name": "system-pirate",
110
+ "messages": [
111
+ {
112
+ "role": "system",
113
+ "content": "Answer like a cheerful pirate, in two sentences."
114
+ },
115
+ {
116
+ "role": "user",
117
+ "content": "What is the boiling point of water at sea level?"
118
+ }
119
+ ],
120
+ "prompt": "<|turn>system\n<|think|>\nAnswer like a cheerful pirate, in two sentences.<turn|>\n<|turn>user\nWhat is the boiling point of water at sea level?<turn|>\n<|turn>model\n",
121
+ "content": "<|channel>thought\n* Topic: Boiling point of water at sea level.\n * Constraint 1: Answer like a cheerful pirate.\n * Constraint 2: Two sentences exactly.",
122
+ "finish": "stop",
123
+ "tokens": 41
124
+ }
125
+ ]
126
+ }
classic/refs/gemma-opus-lora.json ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "about": "llama.cpp b11669 (620d2225c), Metal (-ngl 99), adapters hermes-tool-reasoning (F32, hotdogs@baa8f20) as id 0 and opus (BF16, hotdogs@3bae4ce) as id 1, loaded with --lora-init-without-apply, LoRA id:scale 0:0,1:1 per request: llama-server /completion on the /apply-template prompt, temperature 0, max_tokens 64, on gemma-4-26B_q4_0-it.gguf (sha256 3eca3b8f6d7baf218a7dd6bba5fb59a56ee25fe2d567b6f5f589b4f697eca51d). Recorded 2026-10-31 by scripts/record-refs.mjs.",
3
+ "conversations": [
4
+ {
5
+ "name": "explain-tcp",
6
+ "messages": [
7
+ {
8
+ "role": "user",
9
+ "content": "Explain the difference between TCP and UDP to a new programmer."
10
+ }
11
+ ],
12
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nExplain the difference between TCP and UDP to a new programmer.<turn|>\n<|turn>model\n",
13
+ "content": "<|channel>thought\n\n* *Target Audience:* A new programmer (needs clear analogies, simple language, avoiding overly dense jargon without explanation).\n* *Topic:* Difference between TCP (Transmission Control Protocol) and UDP (User Datagram Protocol).\n* *Goal:* Explain what they are, how they work,",
14
+ "finish": "stop",
15
+ "tokens": 64
16
+ },
17
+ {
18
+ "name": "json-extract",
19
+ "messages": [
20
+ {
21
+ "role": "user",
22
+ "content": "Extract the name, city and age from this sentence as JSON: \"Priya, 34, moved from Pune to Lisbon last year.\""
23
+ }
24
+ ],
25
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nExtract the name, city and age from this sentence as JSON: \"Priya, 34, moved from Pune to Lisbon last year.\"<turn|>\n<|turn>model\n",
26
+ "content": "<|channel>thought\n* Input sentence: \"Priya, 34, moved from Pune to Lisbon last year.\"\n * Task: Extract name, city,",
27
+ "finish": "stop",
28
+ "tokens": 35
29
+ },
30
+ {
31
+ "name": "translate-hi",
32
+ "messages": [
33
+ {
34
+ "role": "user",
35
+ "content": "Translate into Hindi: \"The library opens at nine and closes at six.\""
36
+ }
37
+ ],
38
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nTranslate into Hindi: \"The library opens at nine and closes at six.\"<turn|>\n<|turn>model\n",
39
+ "content": "<|channel>thought\n\n* Source sentence: \"The library opens at nine and closes at six.\"\n* Subject: The library (पुस्तकालय - pustakalaya).\n* Verb 1: opens (खुलता है - khulta hai).\n* Time 1: at nine (नौ",
40
+ "finish": "stop",
41
+ "tokens": 64
42
+ },
43
+ {
44
+ "name": "word-problem",
45
+ "messages": [
46
+ {
47
+ "role": "user",
48
+ "content": "A train leaves at 14:10 and arrives at 17:45. How long is the journey? Show the steps briefly."
49
+ }
50
+ ],
51
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nA train leaves at 14:10 and arrives at 17:45. How long is the journey? Show the steps briefly.<turn|>\n<|turn>model\n",
52
+ "content": "<|channel>thought\n* Departure time: 14:10\n * Arrival time: 17:45\n * Goal: Calculate the duration of the journey.\n * Constraint: Show steps briefly.\n\n * Method 1: Subtraction (Hours and Minutes",
53
+ "finish": "stop",
54
+ "tokens": 64
55
+ },
56
+ {
57
+ "name": "sql",
58
+ "messages": [
59
+ {
60
+ "role": "user",
61
+ "content": "Write a SQL query that returns the five customers with the highest total order value from tables customers(id, name) and orders(id, customer_id, amount)."
62
+ }
63
+ ],
64
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nWrite a SQL query that returns the five customers with the highest total order value from tables customers(id, name) and orders(id, customer_id, amount).<turn|>\n<|turn>model\n",
65
+ "content": "<|channel>thought\n\n* Goal: Find the top 5 customers by total order value.\n* Tables:\n * `customers` (id, name)\n * `orders` (id, customer_id, amount)\n* Output: Customer names (and likely their total order",
66
+ "finish": "stop",
67
+ "tokens": 64
68
+ },
69
+ {
70
+ "name": "list-rivers",
71
+ "messages": [
72
+ {
73
+ "role": "user",
74
+ "content": "List five major rivers of Africa, one per line, with the country where each reaches the sea."
75
+ }
76
+ ],
77
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nList five major rivers of Africa, one per line, with the country where each reaches the sea.<turn|>\n<|turn>model\n",
78
+ "content": "<|channel>thought\n\n* Topic: Five major rivers of Africa.\n * Format: One river per line.\n * Content",
79
+ "finish": "stop",
80
+ "tokens": 29
81
+ },
82
+ {
83
+ "name": "unicode",
84
+ "messages": [
85
+ {
86
+ "role": "user",
87
+ "content": "What do these emoji usually mean in a text message: 🙏 🔥 💀 ?"
88
+ }
89
+ ],
90
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nWhat do these emoji usually mean in a text message: 🙏 🔥 💀 ?<turn|>\n<|turn>model\n",
91
+ "content": "<|channel>thought\n\n* Emoji 1: 🙏 (Folded Hands)\n* Emoji 2: 🔥 (Fire)\n* Emoji 3: 💀 (Skull)\n* Context: Text messages (informal communication).\n\n * *Literal meaning:* Prayer, please, thank you",
92
+ "finish": "stop",
93
+ "tokens": 64
94
+ },
95
+ {
96
+ "name": "haiku-autumn",
97
+ "messages": [
98
+ {
99
+ "role": "user",
100
+ "content": "Write a haiku about autumn leaves in a city."
101
+ }
102
+ ],
103
+ "prompt": "<|turn>system\n<|think|>\n<turn|>\n<|turn>user\nWrite a haiku about autumn leaves in a city.<turn|>\n<|turn>model\n",
104
+ "content": "<|channel>thought\n* Topic: Autumn leaves in a city.\n * Format: Haiku",
105
+ "finish": "stop",
106
+ "tokens": 21
107
+ },
108
+ {
109
+ "name": "system-pirate",
110
+ "messages": [
111
+ {
112
+ "role": "system",
113
+ "content": "Answer like a cheerful pirate, in two sentences."
114
+ },
115
+ {
116
+ "role": "user",
117
+ "content": "What is the boiling point of water at sea level?"
118
+ }
119
+ ],
120
+ "prompt": "<|turn>system\n<|think|>\nAnswer like a cheerful pirate, in two sentences.<turn|>\n<|turn>user\nWhat is the boiling point of water at sea level?<turn|>\n<|turn>model\n",
121
+ "content": "<|channel>thought\n* Topic: Boiling point of water at sea level.\n * Constraint 1: Answer like a cheerful pirate.\n * Constraint 2: Exactly two sentences.",
122
+ "finish": "stop",
123
+ "tokens": 41
124
+ }
125
+ ]
126
+ }
classic/refs/qwen-microturn-lora.json ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "about": "llama.cpp b11669 (620d2225c), CPU (llama-server -ngl 0 -lm mmap --no-repack), adapter microturn-duplex-lora.gguf (F32, codacus@f423b29) loaded with --lora-init-without-apply, LoRA id:scale 0:1 per request: llama-server /completion on the /apply-template prompt, temperature 0, max_tokens 64, on Qwen3.6-35B-A3B-Q8_0.gguf (sha256 d1a395809f65a43a13ad119eb4e7acdef1ac6d68120f39902c8ab96e72794a59). Recorded 2026-10-31 by scripts/record-refs.mjs.",
3
+ "conversations": [
4
+ {
5
+ "name": "explain-tcp",
6
+ "messages": [
7
+ {
8
+ "role": "user",
9
+ "content": "Explain the difference between TCP and UDP to a new programmer."
10
+ }
11
+ ],
12
+ "prompt": "<|im_start|>user\nExplain the difference between TCP and UDP to a new programmer.<|im_end|>\n<|im_start|>assistant\n<think>\n",
13
+ "content": "Here's a thinking process:\n\n1. **Understand the User**: The user wants an explanation of the difference between TCP and UDP, specifically tailored for a *new programmer*. This means I need to:\n - Avoid overly technical jargon where possible, or explain it clearly\n - Use relatable",
14
+ "finish": "stop",
15
+ "tokens": 64
16
+ },
17
+ {
18
+ "name": "json-extract",
19
+ "messages": [
20
+ {
21
+ "role": "user",
22
+ "content": "Extract the name, city and age from this sentence as JSON: \"Priya, 34, moved from Pune to Lisbon last year.\""
23
+ }
24
+ ],
25
+ "prompt": "<|im_start|>user\nExtract the name, city and age from this sentence as JSON: \"Priya, 34, moved from Pune to Lisbon last year.\"<|im_end|>\n<|im_start|>assistant\n<think>\n",
26
+ "content": "Here's a thinking process:\n\n1. **Analyze User Input:**\n - **Input Sentence:** \"Priya, 34, moved from Pune to",
27
+ "finish": "stop",
28
+ "tokens": 35
29
+ },
30
+ {
31
+ "name": "translate-hi",
32
+ "messages": [
33
+ {
34
+ "role": "user",
35
+ "content": "Translate into Hindi: \"The library opens at nine and closes at six.\""
36
+ }
37
+ ],
38
+ "prompt": "<|im_start|>user\nTranslate into Hindi: \"The library opens at nine and closes at six.\"<|im_end|>\n<|im_start|>assistant\n<think>\n",
39
+ "content": "Here's a thinking process:\n\n1. **Analyze the Input:**\n - Source sentence: \"The library opens at nine and closes at six.\"\n - Target language: Hindi\n - Key components:\n - Subject: The library (पुस्तकालय / लाइब",
40
+ "finish": "stop",
41
+ "tokens": 64
42
+ },
43
+ {
44
+ "name": "word-problem",
45
+ "messages": [
46
+ {
47
+ "role": "user",
48
+ "content": "A train leaves at 14:10 and arrives at 17:45. How long is the journey? Show the steps briefly."
49
+ }
50
+ ],
51
+ "prompt": "<|im_start|>user\nA train leaves at 14:10 and arrives at 17:45. How long is the journey? Show the steps briefly.<|im_end|>\n<|im_start|>assistant\n<think>\n",
52
+ "content": "Here's a thinking process:\n\n1. **Understand the User's Request:**\n - Start time: 14:10\n - End time: 17:45\n - Question: How long is the journey?\n - Requirement: Show the steps briefly.\n\n",
53
+ "finish": "stop",
54
+ "tokens": 64
55
+ },
56
+ {
57
+ "name": "sql",
58
+ "messages": [
59
+ {
60
+ "role": "user",
61
+ "content": "Write a SQL query that returns the five customers with the highest total order value from tables customers(id, name) and orders(id, customer_id, amount)."
62
+ }
63
+ ],
64
+ "prompt": "<|im_start|>user\nWrite a SQL query that returns the five customers with the highest total order value from tables customers(id, name) and orders(id, customer_id, amount).<|im_end|>\n<|im_start|>assistant\n<think>\n",
65
+ "content": "Here's a thinking process:\n\n1. **Understand the User Request:**\n - **Goal:** Find the top 5 customers with the highest total order value.\n - **Tables:** \n - `customers(id, name)`\n - `orders(id, customer_id, amount)`\n",
66
+ "finish": "stop",
67
+ "tokens": 64
68
+ },
69
+ {
70
+ "name": "list-rivers",
71
+ "messages": [
72
+ {
73
+ "role": "user",
74
+ "content": "List five major rivers of Africa, one per line, with the country where each reaches the sea."
75
+ }
76
+ ],
77
+ "prompt": "<|im_start|>user\nList five major rivers of Africa, one per line, with the country where each reaches the sea.<|im_end|>\n<|im_start|>assistant\n<think>\n",
78
+ "content": "Here's a thinking process:\n\n1. **Analyze User Request:**\n - **Task:** List five major rivers of Africa\n",
79
+ "finish": "stop",
80
+ "tokens": 29
81
+ },
82
+ {
83
+ "name": "unicode",
84
+ "messages": [
85
+ {
86
+ "role": "user",
87
+ "content": "What do these emoji usually mean in a text message: 🙏 🔥 💀 ?"
88
+ }
89
+ ],
90
+ "prompt": "<|im_start|>user\nWhat do these emoji usually mean in a text message: 🙏 🔥 💀 ?<|im_end|>\n<|im_start|>assistant\n<think>\n",
91
+ "content": "Here's a thinking process:\n\n1. **Analyze User Input:**\n - User asks about the meaning of three specific emojis in text messages: 🙏, 🔥, 💀\n - Context: Modern digital communication/text messaging slang\n\n2. **Identify Each Emoji's Common Mean",
92
+ "finish": "stop",
93
+ "tokens": 64
94
+ },
95
+ {
96
+ "name": "haiku-autumn",
97
+ "messages": [
98
+ {
99
+ "role": "user",
100
+ "content": "Write a haiku about autumn leaves in a city."
101
+ }
102
+ ],
103
+ "prompt": "<|im_start|>user\nWrite a haiku about autumn leaves in a city.<|im_end|>\n<|im_start|>assistant\n<think>\n",
104
+ "content": "Thinking Process:\n\n1. **Deconstruct the request:**\n * Topic: Autumn leaves",
105
+ "finish": "stop",
106
+ "tokens": 21
107
+ },
108
+ {
109
+ "name": "system-pirate",
110
+ "messages": [
111
+ {
112
+ "role": "system",
113
+ "content": "Answer like a cheerful pirate, in two sentences."
114
+ },
115
+ {
116
+ "role": "user",
117
+ "content": "What is the boiling point of water at sea level?"
118
+ }
119
+ ],
120
+ "prompt": "<|im_start|>system\nAnswer like a cheerful pirate, in two sentences.<|im_end|>\n<|im_start|>user\nWhat is the boiling point of water at sea level?<|im_end|>\n<|im_start|>assistant\n<think>\n",
121
+ "content": "Here's a thinking process:\n\n1. **Analyze User Input:**\n - **Question:** What is the boiling point of water at sea level?\n - **Constraint:** Answer like",
122
+ "finish": "stop",
123
+ "tokens": 41
124
+ }
125
+ ]
126
+ }
lora/index.html CHANGED
@@ -4,29 +4,41 @@
4
  <meta charset="utf-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1">
6
  <title>diskformer LoRA switch</title>
7
- <meta name="description" content="LFM2.5-24B-A2B (13.6 GB) in a browser tab, with harness-hub's Pi coding-agent LoRA switched on and off at run time: no reload, no second download.">
8
  <style>
9
- :root { --bg: #fafaf9; --fg: #1c1917; --muted: #78716c; --line: #e7e5e4; --card: #fff; --accent: #2563eb; --pi: #7c3aed; --bad: #b91c1c; }
10
  * { box-sizing: border-box; }
11
  body { margin: 0; background: var(--bg); color: var(--fg); font: 15px/1.5 system-ui, -apple-system, Segoe UI, sans-serif; }
12
  main { max-width: 980px; margin: 0 auto; padding: 24px 16px 48px; }
13
  h1 { font-size: 22px; margin: 0 0 6px; }
 
14
  p { margin: 0 0 12px; }
 
15
  .muted { color: var(--muted); }
16
  .card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 14px; margin: 14px 0; }
17
  .row { display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; }
18
- textarea, button { font: inherit; color: var(--fg); background: var(--bg); border: 1px solid var(--line); border-radius: 7px; padding: 6px 9px; }
 
19
  textarea { width: 100%; resize: vertical; min-height: 64px; }
20
  button { cursor: pointer; }
21
  button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
22
  button:disabled { opacity: .5; cursor: default; }
23
- .seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
24
  .seg button { border: 0; border-radius: 0; background: var(--card); padding: 7px 14px; }
25
  .seg button[aria-pressed=true] { background: var(--fg); color: var(--bg); }
26
- .seg button.pi[aria-pressed=true] { background: var(--pi); }
27
  .bar { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; margin: 10px 0 6px; }
28
  .bar span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .3s; }
29
  #status.bad { color: var(--bad); }
 
 
 
 
 
 
 
 
 
30
  .meter { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
31
  .meter div { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
32
  .meter b { display: block; font-size: 20px; }
@@ -34,33 +46,49 @@
34
  .cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px; }
35
  .reply { border: 1px solid var(--line); border-radius: 9px; padding: 10px 12px; background: var(--card); }
36
  .reply h3 { font-size: 13px; margin: 0 0 6px; color: var(--muted); font-weight: 600; }
37
- .reply.pi h3 { color: var(--pi); }
38
  .reply .think { white-space: pre-wrap; overflow-wrap: anywhere; color: var(--muted); font-size: 13px; border-left: 2px solid var(--line); padding-left: 8px; margin-bottom: 8px; }
 
39
  .reply .answer { white-space: pre-wrap; overflow-wrap: anywhere; }
 
 
40
  footer { font-size: 12px; color: var(--muted); margin-top: 24px; }
41
  </style>
42
  </head>
43
  <body>
44
  <main>
45
- <h1>One model, two behaviours: a LoRA switched at run time</h1>
46
- <p>LiquidAI's LFM2.5-24B-A2B (13.6 GB) answers in this tab, with its 64 experts per layer read from disk as each
47
- token needs them. harness-hub's Pi adapter (rank 32, 37 MB) was trained for a coding agent. Here it sits on the GPU
48
- beside the model and is switched on or off between replies: no reload, no second download of the model.</p>
49
- <p class="muted">Chromium with WebGPU and about 6 GB of free GPU memory. Nothing leaves this tab except the two
50
- downloads from Hugging Face.</p>
51
 
52
  <section class="card">
53
  <div class="row">
 
54
  <button id="load" class="primary">Download and load</button>
55
- <span class="muted">13.6 GB once into this browser's storage, then the 37 MB adapter.</span>
56
  </div>
57
  <div class="bar"><span id="fill"></span></div>
58
- <p id="status" class="muted">Not loaded.</p>
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  </section>
60
 
61
  <section class="card" id="meterCard" hidden>
62
  <div class="meter">
63
- <div><b id="mAdapter">–</b><span>adapter on the GPU</span></div>
64
  <div><b id="mSwitch">–</b><span>last switch</span></div>
65
  <div><b id="mSpeed">–</b><span>decode speed, last reply</span></div>
66
  </div>
@@ -68,90 +96,179 @@
68
 
69
  <section class="card" id="askCard" hidden>
70
  <div class="row" style="margin-bottom:10px">
71
- <div class="seg" role="group" aria-label="Adapter">
72
- <button id="mPlain" aria-pressed="true">Plain model</button>
73
- <button id="mPi" class="pi" aria-pressed="false">Pi adapter</button>
74
- </div>
75
  <span class="muted">The switch applies to the next reply.</span>
76
  </div>
77
- <textarea id="q">Write a bash one-liner that counts the lines in every .py file under src/, then explain it in one sentence.</textarea>
78
  <div class="row" style="margin-top:10px">
79
  <button id="ask" class="primary">Ask</button>
80
- <button id="both">Ask both, side by side</button>
81
  <button id="stop" disabled>Stop</button>
82
  </div>
83
  </section>
84
 
85
  <section class="cols" id="out"></section>
86
 
87
- <footer>Greedy decoding, so the same question gets the same answer. The engine applies y += scale · B(A·x) after
88
- each of the 106 adapted matrices; with the adapter on, its replies match llama.cpp's <code>llama-server --lora</code>
89
- in 9 of 9 gate prompts. Engines: <a href="https://github.com/NakliTechie/diskformer.js">diskformer.js</a>.
90
- Model and adapter: LFM Open License v1.0 (Liquid AI; adapter by harness-hub).</footer>
91
  </main>
92
 
93
  <script type="module">
94
  import { Lfm2MoeSsd } from '../classic/engines/lfm2_moe_ssd.js';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
- // ?url=<GGUF> and ?adapter=<LoRA GGUF>: local copies, honoured only on this machine. ?auto=1 loads and asks both ways
97
- // (window.result), for a headless check.
98
- const ADAPTER = 'https://huggingface.co/harness-hub/LFM2.5-24B-A2B-pi-GGUF/resolve/a994cf7f03ab0e572832d5829f42cbe2ef28d472/LFM2.5-24B-A2B-harbor-pi-lora-F16.gguf';
99
  const q = new URLSearchParams(location.search), local = ['localhost', '127.0.0.1'].includes(location.hostname);
100
  const $ = (id) => document.getElementById(id);
101
  const GB = (n) => (n / 1e9).toFixed(2) + ' GB';
102
- let engine = null, pi = null, mode = 'plain', busy = false, ctrl = null;
 
103
 
 
 
 
104
  const status = (text, bad = false) => { $('status').textContent = text; $('status').className = bad ? 'bad' : 'muted'; window.progressText = text; };
105
  const controls = () => {
106
- $('load').disabled = busy || !!engine;
107
- for (const id of ['ask', 'both', 'mPlain', 'mPi']) $(id).disabled = busy || !engine;
108
- $('stop').disabled = !busy || !engine;
 
 
 
 
109
  };
110
  if (!navigator.gpu) status('This page needs WebGPU: Chrome or Edge 121 or later, on a GPU with shader-f16.', true);
111
 
112
  async function load() {
113
  busy = true; controls();
114
- const t0 = performance.now();
115
  try {
116
- engine = await Lfm2MoeSsd.load(null, {
117
- url: (local && q.get('url')) || undefined, root: 'diskformer', poolBytes: 4 * 2 ** 30, maxCtx: 2048,
 
 
118
  onProgress: (e) => {
119
  if (e.status === 'weights' && e.kind === 'tensors') { $('fill').style.width = `${100 * e.loaded / e.total}%`; status(`Loading the dense layers onto the GPU: ${e.loaded} of ${e.total}`); }
120
  else if (e.status === 'weights' && e.total) { $('fill').style.width = `${100 * e.loaded / e.total}%`; status(`Downloading into this browser's storage (once): ${GB(e.loaded)} of ${GB(e.total)}`); }
121
  },
122
  });
123
- status('Loading the Pi adapter');
124
- pi = await engine.loadAdapter((local && q.get('adapter')) || ADAPTER);
125
- $('fill').style.width = '100%';
126
- $('mAdapter').textContent = `${(pi.bytes / 1e6).toFixed(1)} MB`;
127
  $('meterCard').hidden = false; $('askCard').hidden = false;
128
- status(`Loaded in ${((performance.now() - t0) / 1000).toFixed(1)} s. Pi adapter: rank ${pi.rank}, ${pi.matrices} matrices.`);
 
129
  } catch (e) { status(`Could not load: ${e.message}`, true); throw e; }
130
  finally { busy = false; controls(); }
131
  }
132
 
133
- async function setMode(m) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  const t0 = performance.now();
135
- await engine.setAdapter(m === 'pi' ? pi : null);
136
- mode = m;
137
- $('mPlain').setAttribute('aria-pressed', m === 'plain'); $('mPi').setAttribute('aria-pressed', m === 'pi');
138
  $('mSwitch').textContent = `${(performance.now() - t0).toFixed(1)} ms`;
 
139
  }
 
140
 
141
- // One reply in the current mode, streamed into a column: the reasoning (LFM2.5 always reasons first) muted, the answer below.
 
142
  async function ask(text) {
143
- const box = document.createElement('div'); box.className = `reply ${mode}`;
144
  box.innerHTML = '<h3></h3><div class="think"></div><div class="answer"></div>';
145
- box.querySelector('h3').textContent = mode === 'pi' ? 'Pi adapter' : 'Plain model';
146
  $('out').append(box);
147
  const show = (t) => {
148
- const at = t.indexOf('</think>');
149
- box.querySelector('.think').textContent = (at < 0 ? t : t.slice(0, at)).trim();
150
- box.querySelector('.answer').textContent = at < 0 ? '' : t.slice(at + 8).trim();
151
  };
152
  const t0 = performance.now();
153
  let tFirst = 0, tokens = 0, out = '';
154
- for await (const r of engine.generate([{ role: 'user', content: text }], { maxNewTokens: Number(q.get('n') || 512), signal: ctrl.signal })) {
155
  if (!tokens) tFirst = performance.now();
156
  out = r.text; tokens = r.tokens; show(out);
157
  }
@@ -160,30 +277,79 @@ async function ask(text) {
160
  return { mode, text: out, tokens, firstTokenMs: Math.round(tFirst - t0), tokPerSec: +tps.toFixed(2) };
161
  }
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  async function run(modes) {
164
  const text = $('q').value.trim();
165
- if (!text || busy) return null;
166
  busy = true; ctrl = new AbortController(); controls();
167
  $('out').replaceChildren();
168
- const rows = [];
169
- try { for (const m of modes) { if (m !== mode) await setMode(m); rows.push(await ask(text)); } }
170
- catch (e) { status(`Error: ${e.message}`, true); }
 
 
 
 
 
 
171
  finally { busy = false; ctrl = null; controls(); }
172
- return rows;
173
  }
174
 
175
  $('load').onclick = () => load().catch(() => {});
176
- $('mPlain').onclick = () => setMode('plain');
177
- $('mPi').onclick = () => setMode('pi');
178
  $('ask').onclick = () => run([mode]);
179
- $('both').onclick = () => run(['plain', 'pi']);
180
  $('stop').onclick = () => { if (ctrl) ctrl.abort(); };
 
 
 
181
  controls();
182
- window.demo = { load, setMode, run, get engine() { return engine; } };
 
183
  if (q.get('auto')) {
184
- load().then(() => run(['plain', 'pi', 'plain'])).then((rows) => {
185
- window.result = { ok: !!rows && rows.length === 3 && rows.every((r) => r.tokens > 0) && rows[0].text === rows[2].text, switchMs: $('mSwitch').textContent, adapter: { rank: pi.rank, matrices: pi.matrices, bytes: pi.bytes }, rows };
186
- }).catch((e) => { window.result = { ok: false, error: e.message }; });
 
 
 
 
 
 
 
187
  }
188
  </script>
189
  </body>
 
4
  <meta charset="utf-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1">
6
  <title>diskformer LoRA switch</title>
7
+ <meta name="description" content="Mixture-of-experts models of 13.6 to 36.9 GB in a browser tab, with trained LoRA adapters switched on and off at run time: no reload, no second download. Or load your own adapter.">
8
  <style>
9
+ :root { --bg: #fafaf9; --fg: #1c1917; --muted: #78716c; --line: #e7e5e4; --card: #fff; --accent: #2563eb; --ad: #7c3aed; --bad: #b91c1c; }
10
  * { box-sizing: border-box; }
11
  body { margin: 0; background: var(--bg); color: var(--fg); font: 15px/1.5 system-ui, -apple-system, Segoe UI, sans-serif; }
12
  main { max-width: 980px; margin: 0 auto; padding: 24px 16px 48px; }
13
  h1 { font-size: 22px; margin: 0 0 6px; }
14
+ h2 { font-size: 15px; margin: 0 0 8px; }
15
  p { margin: 0 0 12px; }
16
+ a { color: var(--accent); }
17
  .muted { color: var(--muted); }
18
  .card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 14px; margin: 14px 0; }
19
  .row { display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; }
20
+ select, input, textarea, button { font: inherit; color: var(--fg); background: var(--bg); border: 1px solid var(--line); border-radius: 7px; padding: 6px 9px; }
21
+ input[type=url] { flex: 1; min-width: 220px; }
22
  textarea { width: 100%; resize: vertical; min-height: 64px; }
23
  button { cursor: pointer; }
24
  button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
25
  button:disabled { opacity: .5; cursor: default; }
26
+ .seg { display: inline-flex; flex-wrap: wrap; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
27
  .seg button { border: 0; border-radius: 0; background: var(--card); padding: 7px 14px; }
28
  .seg button[aria-pressed=true] { background: var(--fg); color: var(--bg); }
29
+ .seg button.ad[aria-pressed=true] { background: var(--ad); }
30
  .bar { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; margin: 10px 0 6px; }
31
  .bar span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .3s; }
32
  #status.bad { color: var(--bad); }
33
+ .adapters { width: 100%; border-collapse: collapse; font-size: 14px; }
34
+ .adapters td { padding: 6px; border-top: 1px solid var(--line); vertical-align: top; }
35
+ .adapters td.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
36
+ .adapters td.src { white-space: nowrap; }
37
+ @media (max-width: 560px) {
38
+ .adapters tr { display: grid; grid-template-columns: 1fr auto auto; border-top: 1px solid var(--line); }
39
+ .adapters td { border-top: 0; }
40
+ .adapters td:first-child { grid-column: 1 / -1; padding-bottom: 0; }
41
+ }
42
  .meter { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
43
  .meter div { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
44
  .meter b { display: block; font-size: 20px; }
 
46
  .cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px; }
47
  .reply { border: 1px solid var(--line); border-radius: 9px; padding: 10px 12px; background: var(--card); }
48
  .reply h3 { font-size: 13px; margin: 0 0 6px; color: var(--muted); font-weight: 600; }
49
+ .reply.ad h3 { color: var(--ad); }
50
  .reply .think { white-space: pre-wrap; overflow-wrap: anywhere; color: var(--muted); font-size: 13px; border-left: 2px solid var(--line); padding-left: 8px; margin-bottom: 8px; }
51
+ .reply .think:empty { display: none; }
52
  .reply .answer { white-space: pre-wrap; overflow-wrap: anywhere; }
53
+ .call { width: 100%; border-collapse: collapse; font-size: 14px; }
54
+ .call td, .call th { padding: 4px 6px; border-top: 1px solid var(--line); text-align: left; }
55
  footer { font-size: 12px; color: var(--muted); margin-top: 24px; }
56
  </style>
57
  </head>
58
  <body>
59
  <main>
60
+ <h1>One model, several behaviours: LoRA adapters switched at run time</h1>
61
+ <p>A mixture-of-experts model answers in this tab, with its experts read from disk as each token needs them. A LoRA
62
+ adapter (a few to a few hundred MB, trained by someone for one job) sits on the GPU beside the model and is switched
63
+ on or off between replies: no reload, no second download of the model.</p>
64
+ <p class="muted">Chromium with WebGPU and about 6 GB of free GPU memory. Nothing leaves this tab except the downloads
65
+ from Hugging Face. The adapters are other people's work, linked to their own pages.</p>
66
 
67
  <section class="card">
68
  <div class="row">
69
+ <label>Model <select id="model"></select></label>
70
  <button id="load" class="primary">Download and load</button>
 
71
  </div>
72
  <div class="bar"><span id="fill"></span></div>
73
+ <p id="status" class="muted">Pick a model. The first load downloads it once into this browser's storage.</p>
74
+ </section>
75
+
76
+ <section class="card" id="adCard">
77
+ <h2>Adapters for this model</h2>
78
+ <table class="adapters" id="adList"></table>
79
+ <div class="row" style="margin-top:10px">
80
+ <input id="ownUrl" type="url" placeholder="Your own: URL of a LoRA GGUF for this model (llama.cpp format)">
81
+ <button id="ownLoad">Load URL</button>
82
+ <label class="muted">or a file <input id="ownFile" type="file" accept=".gguf"></label>
83
+ </div>
84
+ <p class="muted" style="margin:8px 0 0">Load the model above first; then each adapter downloads once and stays on the
85
+ GPU until you change model. Adapters must be llama.cpp LoRA GGUFs (F16, F32 or BF16) for this exact
86
+ model. Adapters on the routed experts are refused.</p>
87
  </section>
88
 
89
  <section class="card" id="meterCard" hidden>
90
  <div class="meter">
91
+ <div><b id="mAdapters">–</b><span>adapters on the GPU</span></div>
92
  <div><b id="mSwitch">–</b><span>last switch</span></div>
93
  <div><b id="mSpeed">–</b><span>decode speed, last reply</span></div>
94
  </div>
 
96
 
97
  <section class="card" id="askCard" hidden>
98
  <div class="row" style="margin-bottom:10px">
99
+ <div class="seg" role="group" aria-label="Adapter" id="modes"></div>
 
 
 
100
  <span class="muted">The switch applies to the next reply.</span>
101
  </div>
102
+ <textarea id="q"></textarea>
103
  <div class="row" style="margin-top:10px">
104
  <button id="ask" class="primary">Ask</button>
105
+ <button id="both">Ask plain and adapter, side by side</button>
106
  <button id="stop" disabled>Stop</button>
107
  </div>
108
  </section>
109
 
110
  <section class="cols" id="out"></section>
111
 
112
+ <footer>Greedy decoding, so the same question gets the same answer. The engine adds scale · B(A·x) after each
113
+ adapted matrix, as llama.cpp's <code>--lora</code> does; diskformer gates every engine against
114
+ <code>llama-server --lora</code>. Engines: <a href="https://github.com/NakliTechie/diskformer.js">diskformer.js</a>.
115
+ Model terms: LFM Open License v1.0 (Liquid AI), Gemma (ai.google.dev/gemma/terms), Qwen3.6 (Apache-2.0).</footer>
116
  </main>
117
 
118
  <script type="module">
119
  import { Lfm2MoeSsd } from '../classic/engines/lfm2_moe_ssd.js';
120
+ import { Gemma4MoeSsd } from '../classic/engines/gemma4_moe_ssd.js';
121
+ import { Qwen35MoeSsd } from '../classic/engines/qwen35_moe_ssd.js';
122
+
123
+ const HF = (repo, rev, file) => `https://huggingface.co/${repo}/resolve/${rev}/${file}`;
124
+ const AGENT = 'Write a bash one-liner that counts the lines in every .py file under src/, then explain it in one sentence.';
125
+ const TOOLS = 'You can call one tool, get_weather(city: string). A user asks: "Is it warmer in Pune or in Lisbon right now?" Say which tool calls you would make and in what order, then how you would answer once they return.';
126
+ const REASON = 'A train leaves at 14:10 and arrives at 17:45. How long is the journey? Show the steps briefly.';
127
+ // The adapters on offer: trained ones, published by their authors as llama.cpp LoRA GGUFs, fetched from their own
128
+ // repositories at a pinned revision. kind 'call' runs a scripted voice call (MicroTurn's trained prompt format).
129
+ const MODELS = {
130
+ lfm2: {
131
+ label: 'LFM2.5 24B-A2B · 13.6 GB', Engine: Lfm2MoeSsd, opts: { poolBytes: 4 * 2 ** 30 },
132
+ adapters: [
133
+ { id: 'pi', name: 'Pi coding agent', by: 'harness-hub', repo: 'harness-hub/LFM2.5-24B-A2B-pi-GGUF', url: HF('harness-hub/LFM2.5-24B-A2B-pi-GGUF', 'a994cf7f03ab0e572832d5829f42cbe2ef28d472', 'LFM2.5-24B-A2B-harbor-pi-lora-F16.gguf'), mb: 37.4, licence: 'LFM 1.0',
134
+ what: 'RL-trained inside the Pi coding agent; the authors report 39% fewer tool calls at about the same pass rate.', prompt: AGENT },
135
+ { id: 'cc', name: 'Claude Code agent', by: 'harness-hub', repo: 'harness-hub/LFM2.5-24B-A2B-claude-code-GGUF', url: HF('harness-hub/LFM2.5-24B-A2B-claude-code-GGUF', 'd99731e', 'LFM2.5-24B-A2B-harbor-claude-code-lora-F16.gguf'), mb: 37.4, licence: 'LFM 1.0',
136
+ what: 'RL-trained inside Claude Code; its card reports no measured change from the base model.', prompt: AGENT },
137
+ ],
138
+ },
139
+ gemma: {
140
+ label: 'Gemma 4 26B-A4B · 14.4 GB', Engine: Gemma4MoeSsd, opts: { gpuBudgetBytes: 3e9 },
141
+ adapters: [
142
+ { id: 'hermes', name: 'Hermes tool-use reasoning', by: 'hotdogs', repo: 'hotdogs/gemma4-26b-hermes-tool-reasoning-lora', url: HF('hotdogs/gemma4-26b-hermes-tool-reasoning-lora', 'baa8f20', 'gguf/hotdogs_gemma4-26b-hermes-tool-reasoning-lora.gguf'), mb: 148.7, licence: 'Apache-2.0',
143
+ what: 'Tool-calling conversations with reasoning (10K from interstellarninja/hermes_reasoning_tool_use).', prompt: TOOLS },
144
+ { id: 'fc', name: 'Function calling + thinking', by: 'hotdogs', repo: 'hotdogs/gemma4-26b-fc-thinking-reasoning-lora', url: HF('hotdogs/gemma4-26b-fc-thinking-reasoning-lora', 'ee10d41', 'gguf/hotdogs_gemma4-26b-fc-thinking-reasoning-lora.gguf'), mb: 148.7, licence: 'Apache-2.0',
145
+ what: 'Function calls with a thinking step (Jofthomas/hermes-function-calling-thinking-V1).', prompt: TOOLS },
146
+ { id: 'kimi', name: 'Kimi K2 reasoning', by: 'hotdogs', repo: 'hotdogs/gemma4-26b-kimi-k2-reasoning-lora', url: HF('hotdogs/gemma4-26b-kimi-k2-reasoning-lora', '1a0893b', 'gguf/hotdogs_gemma4-26b-kimi-k2-reasoning-lora.gguf'), mb: 148.7, licence: 'Apache-2.0',
147
+ what: 'Reasoning traces distilled from Kimi K2 (lordx64/reasoning-distill-kimi-k2-6-max-sft).', prompt: REASON },
148
+ { id: 'opus', name: 'Opus reasoning style', by: 'hotdogs', repo: 'hotdogs/gemma4-26b-opus-lora', url: HF('hotdogs/gemma4-26b-opus-lora', '3bae4ce', 'gguf/adapter_model.gguf'), mb: 18.6, licence: 'Apache-2.0',
149
+ what: 'A small (rank 8) adapter distilled toward Claude Opus-style reasoning.', prompt: REASON },
150
+ ],
151
+ },
152
+ qwen35: {
153
+ label: 'Qwen3.6 35B-A3B · 36.9 GB', Engine: Qwen35MoeSsd, opts: { gpuBudgetBytes: 4e9 },
154
+ adapters: [
155
+ { id: 'duplex', name: 'MicroTurn duplex voice', by: 'codacus', repo: 'codacus/Qwen3.6-35B-A3B-MicroTurn-Duplex-LoRA', url: HF('codacus/Qwen3.6-35B-A3B-MicroTurn-Duplex-LoRA', 'f423b29', 'microturn-duplex-lora.gguf'), mb: 15.3, licence: 'Apache-2.0',
156
+ what: 'Takes part in a live call: every 0.6 s it reads what the microphone heard and says up to three words, or waits. Ask runs a scripted call: with the adapter it waits through the pauses and answers once the question ends; the plain model never speaks in this format.', kind: 'call' },
157
+ ],
158
+ },
159
+ };
160
+ // MicroTurn's trained prompt, byte for byte (its microturn_client.py), and a call with a pause mid-sentence.
161
+ const CALL_SYSTEM = 'You are a voice assistant in a live phone call; both sides can talk at the same time. Every 0.6 seconds you get what the microphone heard in that moment, as a streaming transcript fragment, or <no voice>. You reply with up to three new words to say next, or <listen> to add nothing.';
162
+ const CALL_TICKS = ['hi, I wanted', 'to ask about', '<no voice>', 'the train', 'to Pune tomorrow,', 'what time is', '<no voice>', '<no voice>', 'the first one?', '<no voice>'];
163
 
 
 
 
164
  const q = new URLSearchParams(location.search), local = ['localhost', '127.0.0.1'].includes(location.hostname);
165
  const $ = (id) => document.getElementById(id);
166
  const GB = (n) => (n / 1e9).toFixed(2) + ' GB';
167
+ let engine = null, model = null, mode = 'plain', busy = false, ctrl = null;
168
+ const loaded = new Map(); // adapter id -> { spec, handle }
169
 
170
+ for (const [k, m] of Object.entries(MODELS)) $('model').add(new Option(m.label, k));
171
+ if (MODELS[q.get('model')]) $('model').value = q.get('model');
172
+ $('model').onchange = () => { showAdapters(); controls(); };
173
  const status = (text, bad = false) => { $('status').textContent = text; $('status').className = bad ? 'bad' : 'muted'; window.progressText = text; };
174
  const controls = () => {
175
+ $('load').disabled = busy; $('model').disabled = busy;
176
+ for (const id of ['ask', 'both']) $(id).disabled = busy || !engine;
177
+ for (const id of ['ownLoad', 'ownFile']) $(id).disabled = busy || !engine || model !== $('model').value;
178
+ for (const b of document.querySelectorAll('#modes button')) b.disabled = busy;
179
+ for (const b of document.querySelectorAll('#adList button')) b.disabled = busy || !b.dataset.ready;
180
+ $('stop').disabled = !busy || !ctrl;
181
+ $('both').disabled ||= mode === 'plain';
182
  };
183
  if (!navigator.gpu) status('This page needs WebGPU: Chrome or Edge 121 or later, on a GPU with shader-f16.', true);
184
 
185
  async function load() {
186
  busy = true; controls();
187
+ const key = $('model').value, m = MODELS[key], t0 = performance.now();
188
  try {
189
+ if (engine) { const old = engine; engine = null; await old.dispose(); }
190
+ loaded.clear(); mode = 'plain'; $('out').replaceChildren();
191
+ engine = await m.Engine.load(null, {
192
+ url: (local && q.get('url')) || undefined, root: q.get('root') || 'diskformer', maxCtx: 2048, ...m.opts,
193
  onProgress: (e) => {
194
  if (e.status === 'weights' && e.kind === 'tensors') { $('fill').style.width = `${100 * e.loaded / e.total}%`; status(`Loading the dense layers onto the GPU: ${e.loaded} of ${e.total}`); }
195
  else if (e.status === 'weights' && e.total) { $('fill').style.width = `${100 * e.loaded / e.total}%`; status(`Downloading into this browser's storage (once): ${GB(e.loaded)} of ${GB(e.total)}`); }
196
  },
197
  });
198
+ model = key; $('fill').style.width = '100%';
199
+ status(`${m.label.split(' · ')[0]} loaded in ${((performance.now() - t0) / 1000).toFixed(1)} s. Load an adapter below.`);
 
 
200
  $('meterCard').hidden = false; $('askCard').hidden = false;
201
+ showAdapters(); showModes();
202
+ $('q').value = m.adapters[0].prompt || AGENT;
203
  } catch (e) { status(`Could not load: ${e.message}`, true); throw e; }
204
  finally { busy = false; controls(); }
205
  }
206
 
207
+ // The adapters for the model in the picker, shown before it loads; their Load buttons work once it is the loaded one.
208
+ function showAdapters() {
209
+ const key = $('model').value, ready = !!engine && model === key;
210
+ $('adList').replaceChildren(...MODELS[key].adapters.map((a) => {
211
+ const tr = document.createElement('tr'), td = (html, cls) => { const c = tr.insertCell(); c.innerHTML = html; if (cls) c.className = cls; return c; };
212
+ td(`<b>${a.name}</b><br><span class="muted">${a.what}</span>`);
213
+ td(`<a href="https://huggingface.co/${a.repo}" target="_blank" rel="noopener">${a.by}</a><br><span class="muted">${a.licence}</span>`, 'src');
214
+ td(`${a.mb} MB`, 'num');
215
+ const b = document.createElement('button'); b.textContent = loaded.has(a.id) ? 'Loaded' : 'Load';
216
+ b.dataset.ready = ready ? '1' : '';
217
+ b.onclick = () => addAdapter(a, a.url);
218
+ tr.insertCell().append(b);
219
+ return tr;
220
+ }));
221
+ }
222
+ function showModes() {
223
+ const mk = (id, label, cls) => {
224
+ const b = document.createElement('button'); b.textContent = label; if (cls) b.className = cls;
225
+ b.setAttribute('aria-pressed', id === mode); b.onclick = () => setMode(id); return b;
226
+ };
227
+ $('modes').replaceChildren(mk('plain', 'Plain model'), ...[...loaded].map(([id, { spec }]) => mk(id, spec.name, 'ad')));
228
+ $('mAdapters').textContent = loaded.size ? `${loaded.size} · ${([...loaded.values()].reduce((x, a) => x + a.handle.bytes, 0) / 1e6).toFixed(0)} MB` : '0';
229
+ controls();
230
+ }
231
+
232
+ // Downloads (or reads) an adapter, puts it on the GPU and switches to it.
233
+ async function addAdapter(spec, source) {
234
+ if (busy || !engine) return;
235
+ busy = true; controls();
236
+ try {
237
+ status(`Loading the adapter ${spec.name}`);
238
+ const t0 = performance.now(), handle = await engine.loadAdapter(source);
239
+ loaded.set(spec.id, { spec, handle });
240
+ status(`${spec.name}: ${handle.matrices} matrices, rank ${handle.rank}, ${(handle.bytes / 1e6).toFixed(1)} MB on the GPU, ready in ${((performance.now() - t0) / 1000).toFixed(1)} s.`);
241
+ busy = false; await setMode(spec.id);
242
+ if (spec.prompt) $('q').value = spec.prompt;
243
+ showAdapters();
244
+ } catch (e) { status(`Could not load ${spec.name}: ${e.message}`, true); }
245
+ finally { busy = false; showModes(); }
246
+ }
247
+
248
+ async function setMode(id) {
249
  const t0 = performance.now();
250
+ await engine.setAdapter(id === 'plain' ? null : loaded.get(id).handle);
251
+ mode = id;
 
252
  $('mSwitch').textContent = `${(performance.now() - t0).toFixed(1)} ms`;
253
+ showModes();
254
  }
255
+ const modeName = () => (mode === 'plain' ? 'Plain model' : loaded.get(mode).spec.name);
256
 
257
+ // One reply in the current mode, streamed into a column: reasoning muted (LFM2.5 and Gemma 4 can reason first), then
258
+ // the answer.
259
  async function ask(text) {
260
+ const box = document.createElement('div'); box.className = `reply ${mode === 'plain' ? '' : 'ad'}`;
261
  box.innerHTML = '<h3></h3><div class="think"></div><div class="answer"></div>';
262
+ box.querySelector('h3').textContent = modeName();
263
  $('out').append(box);
264
  const show = (t) => {
265
+ const end = ['</think>', '<channel|>'].map((s) => [t.indexOf(s), s.length]).find(([i]) => i >= 0);
266
+ box.querySelector('.think').textContent = end ? t.slice(0, end[0]).replace(/^<\|channel>thought\n?/, '').trim() : '';
267
+ box.querySelector('.answer').textContent = end ? t.slice(end[0] + end[1]).trim() : t.trim();
268
  };
269
  const t0 = performance.now();
270
  let tFirst = 0, tokens = 0, out = '';
271
+ for await (const r of engine.generate([{ role: 'user', content: text }], { maxNewTokens: Number(q.get('n') || 512), signal: ctrl.signal, enableThinking: false })) {
272
  if (!tokens) tFirst = performance.now();
273
  out = r.text; tokens = r.tokens; show(out);
274
  }
 
277
  return { mode, text: out, tokens, firstTokenMs: Math.round(tFirst - t0), tokPerSec: +tps.toFixed(2) };
278
  }
279
 
280
+ // The scripted call (MicroTurn): each tick is one more user turn of what the microphone heard; the reply is up to
281
+ // three words or <listen>. Raw prompt in the adapter's trained format, greedy, stopped at <|im_end|>, with its
282
+ // client's rule (microturn_client.py): it listens only when tokens starting with "<" hold at least 30% of the first
283
+ // token's probability; below that it bans them and speaks from the best other first token.
284
+ const LISTEN_P = 0.3;
285
+ async function call() {
286
+ const box = document.createElement('div'); box.className = `reply ${mode === 'plain' ? '' : 'ad'}`;
287
+ box.innerHTML = '<h3></h3><table class="call"><tr><th>heard</th><th>said</th><th>P(listen)</th></tr></table>';
288
+ box.querySelector('h3').textContent = modeName();
289
+ $('out').append(box);
290
+ const tk = engine.tokenizer, end = tk.ids.get('<|im_end|>'), rows = [], history = [];
291
+ const text = (ids) => { const stop = ids.indexOf(end); return tk.decode(stop < 0 ? ids : ids.slice(0, stop)).trim(); };
292
+ for (const heard of CALL_TICKS) {
293
+ if (ctrl.signal.aborted) break;
294
+ let prompt = `<|im_start|>system\n${CALL_SYSTEM}<|im_end|>\n`;
295
+ for (const [role, content] of [...history, ['user', heard]]) prompt += `<|im_start|>${role}\n${content}<|im_end|>\n`;
296
+ const ids = tk.encode(prompt + '<|im_start|>assistant\n');
297
+ const g = await engine.greedy(ids, 12, { top: 8 });
298
+ const first = g.tops[0], isLt = (o) => tk.decode([o.id]).startsWith('<');
299
+ const pListen = first.filter(isLt).reduce((x, o) => x + Math.exp(o.logprob), 0);
300
+ let said = text(g.ids);
301
+ if (said.startsWith('<')) {
302
+ const alt = first.find((o) => !isLt(o));
303
+ if (pListen >= LISTEN_P || !alt) said = '<listen>';
304
+ else said = text([alt.id, ...(await engine.greedy([...ids, alt.id], 11, { top: 1 })).ids]);
305
+ }
306
+ history.push(['user', heard], ['assistant', said]);
307
+ rows.push({ heard, said, pListen: +pListen.toFixed(2) });
308
+ const tr = box.querySelector('table').insertRow();
309
+ tr.insertCell().textContent = heard; tr.insertCell().textContent = said; tr.insertCell().textContent = pListen.toFixed(2);
310
+ }
311
+ return { mode, rows };
312
+ }
313
+
314
  async function run(modes) {
315
  const text = $('q').value.trim();
316
+ if (busy || !engine) return null;
317
  busy = true; ctrl = new AbortController(); controls();
318
  $('out').replaceChildren();
319
+ const res = [];
320
+ // a call adapter in the run: every mode in it runs the scripted call, so the plain model is compared on the same ticks
321
+ const asCall = modes.some((m) => m !== 'plain' && loaded.get(m).spec.kind === 'call');
322
+ try {
323
+ for (const m of modes) {
324
+ if (m !== mode) { busy = false; await setMode(m); busy = true; controls(); }
325
+ res.push(asCall ? await call() : await ask(text));
326
+ }
327
+ } catch (e) { status(`Error: ${e.message}`, true); }
328
  finally { busy = false; ctrl = null; controls(); }
329
+ return res;
330
  }
331
 
332
  $('load').onclick = () => load().catch(() => {});
 
 
333
  $('ask').onclick = () => run([mode]);
334
+ $('both').onclick = () => { const ad = mode; return run(['plain', ad]); };
335
  $('stop').onclick = () => { if (ctrl) ctrl.abort(); };
336
+ $('ownLoad').onclick = () => { const u = $('ownUrl').value.trim(); if (u) addAdapter({ id: `url:${u}`, name: u.split('/').pop(), what: '' }, u); };
337
+ $('ownFile').onchange = (e) => { const f = e.target.files[0]; if (f) addAdapter({ id: `file:${f.name}`, name: f.name, what: '' }, f); };
338
+ showAdapters();
339
  controls();
340
+ window.demo = { load, addAdapter, setMode, run, MODELS, get engine() { return engine; } };
341
+ // ?auto=1&model=<key>&adapter=<id>: load, add that adapter (a local copy with ?adapterUrl=), ask plain / adapter / plain
342
  if (q.get('auto')) {
343
+ (async () => {
344
+ await load();
345
+ const spec = MODELS[model].adapters.find((a) => a.id === q.get('adapter')) || MODELS[model].adapters[0];
346
+ await addAdapter(spec, (local && q.get('adapterUrl')) || spec.url);
347
+ if (!loaded.has(spec.id)) throw new Error($('status').textContent);
348
+ const rows = await run(['plain', spec.id, 'plain']);
349
+ const same = JSON.stringify(rows[0].text ?? rows[0].rows) === JSON.stringify(rows[2].text ?? rows[2].rows);
350
+ const h = loaded.get(spec.id).handle;
351
+ window.result = { ok: rows.length === 3 && same, model, adapter: spec.id, matrices: h.matrices, rank: h.rank, bytes: h.bytes, switchMs: $('mSwitch').textContent, plainAgainSame: same, rows };
352
+ })().catch((e) => { window.result = { ok: false, error: e.message }; });
353
  }
354
  </script>
355
  </body>