NakliTechie commited on
Commit
bff65e9
Β·
1 Parent(s): 2cc0b39

Sync from GitHub 2026-10-22T03:13:47Z

Browse files
Files changed (1) hide show
  1. index.html +520 -8
index.html CHANGED
@@ -2681,6 +2681,7 @@
2681
  <li><strong>MiniCPM-V 4.7 35B-A3B &middot; SSD-streamed (experimental)</strong> (~20 GB) &mdash; text and images, with reasoning. OpenBMB's MiniCPM-V 4.7 (released 2026-10): a 35B mixture-of-experts (256 experts, 8 active per token, about 3B parameters per token) built on the same architecture as Qwen3.6 35B-A3B, from OpenBMB's own 4-bit (Q4_0) GGUF. Attention, the recurrent layers, routers and KV cache stay on the GPU while the routed experts stream from on-device storage (OPFS) as the router picks them. The first load copies the model into this browser's storage once (~20 GB of disk). Attach an image and it reads it: the first image downloads the vision encoder (1.1 GB) into this browser's storage once. A large image is read as an overview plus up to 9 tiles of about 448 &times; 448 (up to about 650 image tokens), as the model was trained. It can use tools (calculator, time, memory, reminders): it called the right tool on 6 of 6 test prompts and answered 5 of 5 plain questions without one.</li>
2682
  <li><strong>Gemma 4 26B-A4B &middot; SSD-streamed (experimental)</strong> (~14 GB) &mdash; text, with reasoning. Google's Gemma 4 26B-A4B mixture-of-experts (128 experts, 8 active per token) from its official 4-bit QAT GGUF (14.4 GB) on the same from-scratch WebGPU engine family: attention, the dense MLP, routers and KV cache stay on the GPU (~1.6 GB plus a 4 GB expert pool) while the 3,840 routed experts stream from on-device storage (OPFS) as the router picks them. The first load copies the model into this browser's storage once (~14 GB of disk; about 12 minutes on a fast connection). About 23 tokens/s on an M4 Pro with 24 GB, and prompts are read in chunks at about 55 tokens/s. Greedy output matches llama.cpp. Flip <strong>Show reasoning</strong> for its thinking.</li>
2683
  <li><strong>LFM2.5 24B-A2B &middot; SSD-streamed (experimental)</strong> (~13.6 GB) &mdash; text, reasoning-only. Liquid AI's LFM2.5-24B-A2B (released 2026-10-13): a 24B mixture-of-experts (64 experts, 4 active per token, about 2.4B parameters per token) with short-convolution and attention layers, from Liquid's own 4-bit GGUF. Short-convolution and attention layers, routers and KV cache stay on the GPU while the 2,432 routed experts stream from on-device storage (OPFS) as the router picks them. The first load copies the model into this browser's storage once (~13.6 GB of disk). About 16 tokens/s on an M4 Pro with 24 GB, and prompts are read in chunks at about 50 tokens/s. Greedy output matches llama.cpp (8 of 8 prompts). It always reasons before it answers, so every reply opens with its reasoning. It can use tools (calculator, time, memory, reminders): it called the right tool on 6 of 6 test prompts and answered 5 of 5 plain questions without one.</li>
 
2684
  </ul>
2685
  </details>
2686
  </div>
@@ -2996,6 +2997,7 @@
2996
  <option value="minicpm-v47-35b-a3b-ssd">MiniCPM-V 4.7 35B-A3B &middot; SSD-streamed, experimental, text and images (~20 GB)</option>
2997
  <option value="gemma4-26b-a4b-ssd">Gemma 4 26B-A4B &middot; SSD-streamed, experimental (~14 GB)</option>
2998
  <option value="lfm25-24b-a2b-ssd">LFM2.5 24B-A2B &middot; SSD-streamed, experimental (~13.6 GB)</option>
 
2999
  </select>
3000
  <!-- Model picker (Chunk L layer 2): renders #modelSelect's options grouped by what is
3001
  already in this browser; picking sets the select's value and fires its change event. -->
@@ -9364,6 +9366,23 @@ self.addEventListener('message', async (e) => {
9364
  contextSize: 4096,
9365
  genConfig: { max_new_tokens: 2048 },
9366
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9367
  'minicpm5-2b-gguf': {
9368
  id: 'https://huggingface.co/openbmb/MiniCPM5-2B-GGUF/resolve/main/MiniCPM5-2B-Q4_K_M.gguf',
9369
  label: 'MiniCPM5 2B Β· GGUF',
@@ -17680,6 +17699,7 @@ self.addEventListener('message', async (e) => {
17680
  'minicpmv-ssd': { engine: 'qwen35_moe_ssd.js', exportName: 'MiniCpmV47Ssd', label: 'MiniCPM-V 4.7 35B-A3B (Q4_0)' },
17681
  'gemma4moe-ssd': { engine: 'gemma4_moe_ssd.js', exportName: 'Gemma4MoeSsd', label: 'Gemma 4 26B-A4B (Q4_0)' },
17682
  'lfm2moe-ssd': { engine: 'lfm2_moe_ssd.js', exportName: 'Lfm2MoeSsd', label: 'LFM2.5 24B-A2B (Q4_0)' },
 
17683
  'hyv3-ssd': { engine: 'hy_v3_moe_ssd.js', exportName: 'HyV3MoeSsd', label: 'Hy-Memory 30B-A3B (Q8_0)' }, // the memory writer only (WRITER_MODELS)
17684
  'qwen35dense': { engine: 'qwen35_dense.js', exportName: 'Qwen35Dense', label: 'Underdog Saluki 27B (IQ2-mix)' },
17685
  })[backend] || null;
@@ -19294,7 +19314,7 @@ self.addEventListener('message', async (e) => {
19294
  const ENGINE_STORES = {
19295
  'transformers': ['transformers-cache'], 'wllama': ['wllama'], 'lfm2-webgpu': ['gguf-v1'],
19296
  'bonsai2-webgpu': ['gguf-cache-v1'], 'gemma4-webgpu': ['safetensors-cache-v1', 'bonsai-pipeline-v1'],
19297
- 'qwen35moe-ssd': ['on disk (OPFS)'], 'minicpmv-ssd': ['on disk (OPFS)'], 'gemma4moe-ssd': ['on disk (OPFS)'], 'lfm2moe-ssd': ['on disk (OPFS)'], 'qwen35dense': ['on disk (OPFS)'],
19298
  };
19299
  function parseSizeBytes(size) {
19300
  const m = /([\d.]+)\s*(GB|MB)/i.exec(String(size || ''));
@@ -23911,6 +23931,494 @@ export function codedError(e, { signal, fallback = 'LOAD_FAILED', what = 'load'
23911
  return new DiskformerError(fallback, `${what} failed: ${(e && e.message) || e}`, { cause: e });
23912
  }
23913
  </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23914
  <script type="text/plain" data-module="gemma-4-e2b.js">// Gemma-4 E2B (QAT mobile) WebGPU chat bundle. Import { Gemma4Mobile } from this file.
23915
  // Built by LocalMind scripts/build-gemma-4-e2b.mjs from webml-community/gemma-4-webgpu-kernels@158f16a:
23916
  // adds load(id, { ple }) to serve the per-layer embedding table from OPFS (see ple-opfs.js).
@@ -30774,7 +31282,7 @@ const HY_SPLITS = [
30774
  /[δΈ€-ιΎ₯぀-γ‚Ÿγ‚ -γƒΏ]+/gu,
30775
  /[!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~][A-Za-z]+|[^\r\n\p{L}\p{P}\p{S}]?[\p{L}\p{M}]+| ?[\p{P}\p{S}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+/gu,
30776
  ];
30777
- function isolate(pieces, re) {
30778
  const out = [];
30779
  for (const s of pieces) {
30780
  let last = 0;
@@ -38165,6 +38673,8 @@ export class Lfm2MoeSsd extends Gemma4MoeSsd {
38165
  // Kernel names for the matrix and expert weights: Q4_0 here; an engine reading a Q8_0 file (hy_v3_moe_ssd.js) swaps
38166
  // in the Q8_0 kernels, which take the same bindings and dispatch sizes.
38167
  get kq() { return { mm: 'matmulQ4', mmT: 'matmulQ4T', ex: 'expertQ4', exB: 'expertQ4B' }; }
 
 
38168
 
38169
  chatPrompt(messages) { return lfm2ChatPrompt(messages); }
38170
  get stopTokenIds() { return [this.cfg.eos, this.tokenizer.ids.get('<|im_end|>'), this.tokenizer.ids.get('<|endoftext|>')].filter((t) => t !== undefined); }
@@ -38386,9 +38896,10 @@ export class Lfm2MoeSsd extends Gemma4MoeSsd {
38386
  d(this.kq.mm, g.q, Math.ceil(QN / 4)); d(this.kq.mm, g.k, Math.ceil(KVN / 4)); d(this.kq.mm, g.v, Math.ceil(KVN / 4));
38387
  d('qkNormRope', g.ropeQ, c.heads); d('qkNormRope', g.ropeK, c.kvHeadsAttn);
38388
  d('kvStore', g.kv, Math.ceil(KVN / 256));
38389
- d('attnScore', g.score, Math.ceil(c.heads * seqLen / 256));
38390
- d('softmax', g.soft, c.heads);
38391
- d('attnOut', g.attOut, Math.ceil(QN / 256));
 
38392
  d(this.kq.mm, g.o, Math.ceil(H / 4));
38393
  } else {
38394
  d(this.kq.mm, g.inProj, Math.ceil(3 * H / 4));
@@ -38446,9 +38957,10 @@ export class Lfm2MoeSsd extends Gemma4MoeSsd {
38446
  d(this.kq.mmT, g.q, mm(QN), tt); d(this.kq.mmT, g.k, mm(KVN), tt); d(this.kq.mmT, g.v, mm(KVN), tt);
38447
  d('qkNormRopeB', g.ropeQ, c.heads, T); d('qkNormRopeB', g.ropeK, c.kvHeadsAttn, T);
38448
  d('kvStoreB', g.kv, Math.ceil(KVN / 256), T);
38449
- d('attnScoreB', g.score, Math.ceil(c.heads * T * S / 256));
38450
- d('softmaxB', g.soft, c.heads * T);
38451
- d('attnOutB', g.attOut, Math.ceil(QN / 256), T);
 
38452
  d(this.kq.mmT, g.o, mm(H), tt);
38453
  } else {
38454
  d(this.kq.mmT, g.inProj, mm(3 * H), tt);
 
2681
  <li><strong>MiniCPM-V 4.7 35B-A3B &middot; SSD-streamed (experimental)</strong> (~20 GB) &mdash; text and images, with reasoning. OpenBMB's MiniCPM-V 4.7 (released 2026-10): a 35B mixture-of-experts (256 experts, 8 active per token, about 3B parameters per token) built on the same architecture as Qwen3.6 35B-A3B, from OpenBMB's own 4-bit (Q4_0) GGUF. Attention, the recurrent layers, routers and KV cache stay on the GPU while the routed experts stream from on-device storage (OPFS) as the router picks them. The first load copies the model into this browser's storage once (~20 GB of disk). Attach an image and it reads it: the first image downloads the vision encoder (1.1 GB) into this browser's storage once. A large image is read as an overview plus up to 9 tiles of about 448 &times; 448 (up to about 650 image tokens), as the model was trained. It can use tools (calculator, time, memory, reminders): it called the right tool on 6 of 6 test prompts and answered 5 of 5 plain questions without one.</li>
2682
  <li><strong>Gemma 4 26B-A4B &middot; SSD-streamed (experimental)</strong> (~14 GB) &mdash; text, with reasoning. Google's Gemma 4 26B-A4B mixture-of-experts (128 experts, 8 active per token) from its official 4-bit QAT GGUF (14.4 GB) on the same from-scratch WebGPU engine family: attention, the dense MLP, routers and KV cache stay on the GPU (~1.6 GB plus a 4 GB expert pool) while the 3,840 routed experts stream from on-device storage (OPFS) as the router picks them. The first load copies the model into this browser's storage once (~14 GB of disk; about 12 minutes on a fast connection). About 23 tokens/s on an M4 Pro with 24 GB, and prompts are read in chunks at about 55 tokens/s. Greedy output matches llama.cpp. Flip <strong>Show reasoning</strong> for its thinking.</li>
2683
  <li><strong>LFM2.5 24B-A2B &middot; SSD-streamed (experimental)</strong> (~13.6 GB) &mdash; text, reasoning-only. Liquid AI's LFM2.5-24B-A2B (released 2026-10-13): a 24B mixture-of-experts (64 experts, 4 active per token, about 2.4B parameters per token) with short-convolution and attention layers, from Liquid's own 4-bit GGUF. Short-convolution and attention layers, routers and KV cache stay on the GPU while the 2,432 routed experts stream from on-device storage (OPFS) as the router picks them. The first load copies the model into this browser's storage once (~13.6 GB of disk). About 16 tokens/s on an M4 Pro with 24 GB, and prompts are read in chunks at about 50 tokens/s. Greedy output matches llama.cpp (8 of 8 prompts). It always reasons before it answers, so every reply opens with its reasoning. It can use tools (calculator, time, memory, reminders): it called the right tool on 6 of 6 test prompts and answered 5 of 5 plain questions without one.</li>
2684
+ <li><strong>Flint 40B-A2.5B &middot; SSD-streamed (experimental)</strong> (~26 GB) &mdash; text, coding, reasoning. Recursive's Flint (released 2026-10-21, Apache-2.0): a 39.8B mixture-of-experts coding model (256 experts, 6 active per token, about 2.5B parameters per token), from Recursive's own 4-bit (Q4_1) GGUF, whose card asks for 48 GB of memory under llama.cpp. Attention, routers, the token table and KV cache stay on the GPU while the 12,032 routed experts stream from on-device storage (OPFS) as the router picks them. The first load copies the model into this browser's storage once (~26 GB of disk). About 8.7 tokens/s on an M4 Pro with 24 GB, and prompts are read at about 27 tokens/s. Its output stays as close to llama.cpp's Metal backend as llama.cpp's own CPU backend does. It reasons before it answers, so replies open with its reasoning. Tools are off for now.</li>
2685
  </ul>
2686
  </details>
2687
  </div>
 
2997
  <option value="minicpm-v47-35b-a3b-ssd">MiniCPM-V 4.7 35B-A3B &middot; SSD-streamed, experimental, text and images (~20 GB)</option>
2998
  <option value="gemma4-26b-a4b-ssd">Gemma 4 26B-A4B &middot; SSD-streamed, experimental (~14 GB)</option>
2999
  <option value="lfm25-24b-a2b-ssd">LFM2.5 24B-A2B &middot; SSD-streamed, experimental (~13.6 GB)</option>
3000
+ <option value="flint-40b-a2b-ssd">Flint 40B-A2.5B &middot; SSD-streamed, experimental, coding (~26 GB)</option>
3001
  </select>
3002
  <!-- Model picker (Chunk L layer 2): renders #modelSelect's options grouped by what is
3003
  already in this browser; picking sets the select's value and fires its change event. -->
 
9366
  contextSize: 4096,
9367
  genConfig: { max_new_tokens: 2048 },
9368
  },
9369
+ // backend:'flint-ssd' β†’ the custom-WGSL FlintMoeSsd engine (flint_moe_ssd.js, on the LFM2.5 rung): Recursive's Flint
9370
+ // (2026-10-21, a 39.8B coding MoE, ~2.5B active) from Recursive's own Q4_1 GGUF (26 GB, not gated; the card asks for
9371
+ // 48 GB of memory under llama.cpp). Attention, routers, the F32 token table and KV stay on the GPU; the 12,032
9372
+ // routed experts stream from OPFS.
9373
+ 'flint-40b-a2b-ssd': {
9374
+ id: 'RecursiveSI/flint-int4-gguf',
9375
+ label: 'Flint 40B-A2.5B Β· SSD-streamed (experimental)',
9376
+ backend: 'flint-ssd',
9377
+ size: '~26 GB',
9378
+ gguf: { file: 'flint-int4-q4_1.gguf', size: 25992966976 }, // FLINT_40B_A2B in flint_moe_ssd.js
9379
+ type: 'causal',
9380
+ multimodal: false,
9381
+ agentCapable: false,
9382
+ thinking: true,
9383
+ contextSize: 8192,
9384
+ genConfig: { max_new_tokens: 4096 },
9385
+ },
9386
  'minicpm5-2b-gguf': {
9387
  id: 'https://huggingface.co/openbmb/MiniCPM5-2B-GGUF/resolve/main/MiniCPM5-2B-Q4_K_M.gguf',
9388
  label: 'MiniCPM5 2B Β· GGUF',
 
17699
  'minicpmv-ssd': { engine: 'qwen35_moe_ssd.js', exportName: 'MiniCpmV47Ssd', label: 'MiniCPM-V 4.7 35B-A3B (Q4_0)' },
17700
  'gemma4moe-ssd': { engine: 'gemma4_moe_ssd.js', exportName: 'Gemma4MoeSsd', label: 'Gemma 4 26B-A4B (Q4_0)' },
17701
  'lfm2moe-ssd': { engine: 'lfm2_moe_ssd.js', exportName: 'Lfm2MoeSsd', label: 'LFM2.5 24B-A2B (Q4_0)' },
17702
+ 'flint-ssd': { engine: 'flint_moe_ssd.js', exportName: 'FlintMoeSsd', label: 'Flint 40B-A2.5B (Q4_1)' },
17703
  'hyv3-ssd': { engine: 'hy_v3_moe_ssd.js', exportName: 'HyV3MoeSsd', label: 'Hy-Memory 30B-A3B (Q8_0)' }, // the memory writer only (WRITER_MODELS)
17704
  'qwen35dense': { engine: 'qwen35_dense.js', exportName: 'Qwen35Dense', label: 'Underdog Saluki 27B (IQ2-mix)' },
17705
  })[backend] || null;
 
19314
  const ENGINE_STORES = {
19315
  'transformers': ['transformers-cache'], 'wllama': ['wllama'], 'lfm2-webgpu': ['gguf-v1'],
19316
  'bonsai2-webgpu': ['gguf-cache-v1'], 'gemma4-webgpu': ['safetensors-cache-v1', 'bonsai-pipeline-v1'],
19317
+ 'qwen35moe-ssd': ['on disk (OPFS)'], 'minicpmv-ssd': ['on disk (OPFS)'], 'gemma4moe-ssd': ['on disk (OPFS)'], 'lfm2moe-ssd': ['on disk (OPFS)'], 'flint-ssd': ['on disk (OPFS)'], 'qwen35dense': ['on disk (OPFS)'],
19318
  };
19319
  function parseSizeBytes(size) {
19320
  const m = /([\d.]+)\s*(GB|MB)/i.exec(String(size || ''));
 
23931
  return new DiskformerError(fallback, `${what} failed: ${(e && e.message) || e}`, { cause: e });
23932
  }
23933
  </script>
23934
+ <script type="text/plain" data-module="flint_moe_ssd.js">/* flint_moe_ssd.js β€” Recursive Flint (GGUF arch `flint`, 39.8B total, ~2.5B active) with its routed experts on disk:
23935
+ * rung 2g of the SSD-streaming ladder. Flint is a coding-agent model (released 2026-10-21, Apache-2.0). As an engine it
23936
+ * is the Hy-Memory graph (hy_v3_moe_ssd.js, on the LFM2.5 rung) with attention sinks, a sliding window and no RoPE on
23937
+ * its full-attention layers, read from Q4_1 weights.
23938
+ *
23939
+ * The math follows llama.cpp src/models/flint.cpp (a37cc461). 49 layers, each:
23940
+ * x += attn(rms_norm(x) Β· attn_norm): Q/K/V (32 / 4 / 4 heads Γ— 128; Q is 4096 wide), per-head weighted RMS norm on
23941
+ * Q and K, then NeoX RoPE (base 10000, all 128 dims) on the 35 sliding layers and none on the 14 full layers
23942
+ * (rope_pattern). Scores QKα΅€/√128; the sliding layers see the current token and the 512 before it (window 513,
23943
+ * llama.cpp LLAMA_SWA_TYPE_STANDARD); softmax with one learned sink logit per head in the denominator
23944
+ * (attn_sinks); output projection 4096 β†’ 2048.
23945
+ * x += ffn(rms_norm(x) Β· ffn_norm): a dense SwiGLU (6144) on layers 0–1; on layers 2–48 a MoE of 256 experts, top 6
23946
+ * by sigmoid(router) (no selection bias in this file), weights = the picked sigmoids over their sum (scale 1),
23947
+ * SwiGLU experts (512), plus one always-on shared SwiGLU expert (1024, no gate).
23948
+ * Embedding: F32 rows (no scale); head: rms_norm(x) Β· output_norm, then output.weight (Q4_1, untied).
23949
+ *
23950
+ * File: Recursive's own Q4_1 GGUF (RecursiveSI/flint-int4-gguf: the MLX export's trained INT4 codes with f16 scales and
23951
+ * offsets): every matrix and expert Q4_1; norms, sinks, router and the token table F32. Q4_1 blocks are 20 bytes (f16
23952
+ * d, f16 m, 16 bytes of nibbles), a whole number of u32 words, so the store keeps them as they are in the GGUF and the
23953
+ * kernels read value = dΒ·q + m straight from the blocks. The store's scale planes (the `s` binding and the pool's
23954
+ * guS / dS parts) are unused here.
23955
+ */
23956
+
23957
+ import { Qwen3MoeSsd, GGML, TOK, STORAGE } from './qwen3_moe_ssd.js';
23958
+ import { Gemma4MoeSsd } from './gemma4_moe_ssd.js';
23959
+ import { Lfm2MoeSsd } from './lfm2_moe_ssd.js';
23960
+ import { HyV3Tokenizer, isolate } from './hy_v3_moe_ssd.js';
23961
+
23962
+ const Q41_BLOCK = 20; // f16 d + f16 m + 32 Γ— 4-bit
23963
+ const GGML_Q4_1 = 3;
23964
+
23965
+ export function configFromGgufFlint(kv) {
23966
+ const a = kv['general.architecture'];
23967
+ if (a !== 'flint') throw new Error(`expected a flint GGUF, got ${a}`);
23968
+ const g = (k) => kv[`${a}.${k}`];
23969
+ if (g('expert_gating_func') !== 2) throw new Error(`flint expert_gating_func ${g('expert_gating_func')}: this engine implements sigmoid (2)`);
23970
+ if (g('expert_weights_norm') === false) throw new Error('flint without expert_weights_norm: not implemented');
23971
+ const modelLayers = g('block_count'), lead = g('leading_dense_block_count'), layers = modelLayers - lead;
23972
+ const H = g('embedding_length'), heads = g('attention.head_count'), kvH = g('attention.head_count_kv');
23973
+ const hd = g('attention.key_length') || H / heads, theta = g('rope.freq_base');
23974
+ const swaPat = g('attention.sliding_window_pattern'), ropePat = g('attention.rope_pattern');
23975
+ if (swaPat.length !== modelLayers || ropePat.length !== modelLayers) throw new Error('flint: per-layer patterns do not match block_count');
23976
+ return {
23977
+ arch: a, modelLayers, lead, layers,
23978
+ attn: Array(modelLayers).fill(true),
23979
+ hidden: H, denseFf: g('feed_forward_length'), heads, kvHeadsAttn: kvH, attnHeadDim: hd,
23980
+ experts: g('expert_count'), topK: g('expert_used_count'), expertFf: g('expert_feed_forward_length'),
23981
+ shexpFf: g('expert_shared_feed_forward_length'), expScale: g('expert_weights_scale') ?? 1,
23982
+ eps: g('attention.layer_norm_rms_epsilon'), contextLength: g('context_length'),
23983
+ vocab: kv['tokenizer.ggml.tokens'].length,
23984
+ bos: kv['tokenizer.ggml.bos_token_id'], eos: kv['tokenizer.ggml.eos_token_id'],
23985
+ // Per model layer: the attention window (0 = all positions) and whether Q and K take RoPE.
23986
+ win: swaPat.map((s) => (s ? g('attention.sliding_window') : 0)), rope: ropePat.map(Boolean),
23987
+ // The shape Gemma4MoeSsd's RoPE tables read, per MoE layer: one RoPE table (the NoPE layers bind an identity one).
23988
+ swa: Array(layers).fill(false), headDim: Array(layers).fill(hd), ropeTheta: Array(layers).fill(theta), softcap: 0,
23989
+ kvHeads: Array(layers).fill(kvH),
23990
+ };
23991
+ }
23992
+
23993
+ // The OPFS layout from the header alone: the LFM2.5 store shape with Q4_1 blocks kept whole. One expert record = the
23994
+ // unused guS and dS (256 bytes each, first, so every record ends in written bytes and the file reaches its full
23995
+ // length), gate rows then up rows (guQ), down rows (dQ); the store holds the 47 MoE layers, indexed from 0. Dense
23996
+ // tensors are raw copies (Q4_1 blocks or F32).
23997
+ export function planLayoutFlint(gguf) {
23998
+ const cfg = configFromGgufFlint(gguf.kv), H = cfg.hidden, F = cfg.expertFf;
23999
+ const names = new Set(gguf.tensors.map((t) => t.name));
24000
+ let lead = 0;
24001
+ while (names.has(`blk.${lead}.ffn_gate.weight`)) lead++;
24002
+ if (lead !== cfg.lead || !names.has(`blk.${lead}.ffn_gate_inp.weight`)) throw new Error(`flint: ${lead} dense leading layers, expected ${cfg.lead}`);
24003
+ const blk = (rows, cols) => rows * (cols / 32) * Q41_BLOCK, gu = blk(2 * F, H), dn = blk(H, F);
24004
+ const parts = {
24005
+ guS: { off: 0, bytes: 256 }, dS: { off: 256, bytes: 256 },
24006
+ guQ: { off: 512, bytes: gu }, dQ: { off: 512 + gu, bytes: dn },
24007
+ };
24008
+ const record = parts.dQ.off + parts.dQ.bytes;
24009
+ const dense = {};
24010
+ let off = 0;
24011
+ const take = (n) => { const o = off; off += Math.ceil(n / 256) * 256; return o; };
24012
+ for (const t of gguf.tensors) {
24013
+ if (/_exps\.weight$/.test(t.name)) continue;
24014
+ const n = t.dims.reduce((x, y) => x * y, 1);
24015
+ if (t.type === GGML_Q4_1) dense[t.name] = { type: 'q41', dims: t.dims, raw: { off: take(n / 32 * Q41_BLOCK), bytes: n / 32 * Q41_BLOCK } };
24016
+ else if (t.type === GGML.F32) dense[t.name] = { type: 'f32', dims: t.dims, raw: { off: take(n * 4), bytes: n * 4 } };
24017
+ else throw new Error(`dense tensor ${t.name}: unsupported type ${t.type} (this engine reads Recursive's Q4_1 file)`);
24018
+ }
24019
+ return {
24020
+ config: cfg,
24021
+ experts: { file: 'experts.bin', record, parts, layers: cfg.layers, perLayer: cfg.experts, bytes: record * cfg.layers * cfg.experts },
24022
+ dense: { file: 'dense.bin', bytes: off, tensors: dense },
24023
+ };
24024
+ }
24025
+
24026
+ // Every unit is a raw copy. Expert slices go to their record; dense tensors in bands of at most 8 MB (the F32 token
24027
+ // table is 1.23 GB, larger than one range request).
24028
+ export function planUnitsFlint(gguf, layout) {
24029
+ const units = [];
24030
+ const { record, parts, perLayer } = layout.experts, lead = layout.config.lead, F = layout.config.expertFf, H = layout.config.hidden;
24031
+ const gateBytes = F * (H / 32) * Q41_BLOCK, band = 8 << 20;
24032
+ for (const t of gguf.tensors) {
24033
+ const abs = gguf.dataStart + t.offset;
24034
+ const m = /^blk\.(\d+)\.ffn_(gate|up|down)_exps\.weight$/.exec(t.name);
24035
+ if (m) {
24036
+ if (t.type !== GGML_Q4_1) throw new Error(`${t.name}: experts must be Q4_1`);
24037
+ const layer = Number(m[1]) - lead, which = m[2];
24038
+ const slice = t.dims.reduce((x, y) => x * y, 1) / 32 * Q41_BLOCK / perLayer;
24039
+ const dst = which === 'down' ? parts.dQ.off : parts.guQ.off + (which === 'up' ? gateBytes : 0);
24040
+ for (let e = 0; e < perLayer; e++) units.push({ src: abs + e * slice, len: slice, file: 'experts', raw: (layer * perLayer + e) * record + dst });
24041
+ continue;
24042
+ }
24043
+ const d = layout.dense.tensors[t.name];
24044
+ for (let o = 0; o < d.raw.bytes; o += band) units.push({ src: abs + o, len: Math.min(band, d.raw.bytes - o), file: 'dense', raw: d.raw.off + o });
24045
+ }
24046
+ units.sort((a, b) => a.src - b.src);
24047
+ return units;
24048
+ }
24049
+
24050
+ export const FLINT_INGEST_PLAN = { layout: planLayoutFlint, units: planUnitsFlint };
24051
+
24052
+ // Recursive's own Q4_1 GGUF (rev 9c47151, 2026-10-21); sha256 = the LFS object id.
24053
+ export const FLINT_40B_A2B = {
24054
+ repo: 'RecursiveSI/flint-int4-gguf',
24055
+ file: 'flint-int4-q4_1.gguf',
24056
+ revision: '9c47151036bc2c34331142dda04488bceab2bc9f',
24057
+ sha256: '43c07baccdba42eb7528a9630ee15699b1942c297291dc1b39a061f508ee1a4b',
24058
+ size: 25992966976,
24059
+ };
24060
+
24061
+ // ── Tokenizer: byte-level BPE; tokenizer.json's pre-tokenizer is a Sequence of three Isolated splits (llama.cpp
24062
+ // LLAMA_VOCAB_PRE_TYPE_FLINT has the same three regexes; `(?i:'s|…)` spelled out case by case): digit runs of up to
24063
+ // 3, CJK / kana runs, then the word split. Same mechanics as Hy-Memory's (HyV3Tokenizer), other word split.
24064
+ const CONTR = "(?:'[sS]|'[tT]|'[rR][eE]|'[vV][eE]|'[mM]|'[lL][lL]|'[dD])?";
24065
+ const FLINT_SPLITS = [
24066
+ /\p{N}{1,3}/gu,
24067
+ /[δΈ€-ιΎ₯぀-γ‚Ÿγ‚ -γƒΏ]+/gu,
24068
+ new RegExp(`[^\\r\\n\\p{L}\\p{N}]?[\\p{Lu}\\p{Lt}\\p{Lm}\\p{Lo}\\p{M}]*[\\p{Ll}\\p{Lm}\\p{Lo}\\p{M}]+${CONTR}|[^\\r\\n\\p{L}\\p{N}]?[\\p{Lu}\\p{Lt}\\p{Lm}\\p{Lo}\\p{M}]+[\\p{Ll}\\p{Lm}\\p{Lo}\\p{M}]*${CONTR}| ?[\\p{P}\\p{S}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+`, 'gu'),
24069
+ ];
24070
+ export class FlintTokenizer extends HyV3Tokenizer {
24071
+ constructor(kv) {
24072
+ super(kv);
24073
+ this.preRe = { [Symbol.matchAll]: (s) => FLINT_SPLITS.reduce(isolate, [s]).map((p) => [p])[Symbol.iterator]() };
24074
+ }
24075
+ }
24076
+
24077
+ // The chat template (chat_template.jinja), no native tools: <|system|>…, <|user|>…, <|assistant|><think>reasoning
24078
+ // </think>…, <|observation|><tool_response>…</tool_response>, with no separators; the generation prompt is
24079
+ // <|assistant|>. User, system and tool text loses NUL, <think> and </think>, and its role markers are escaped as the
24080
+ // template does. Earlier replies keep their reasoning, as the model card asks (LocalMind's <|think|> … <|/think|>
24081
+ // markers or the model's own </think>).
24082
+ const FLINT_MARKERS = ['<|begin_of_text|>', '<|end_of_text|>', '<|system|>', '<|developer|>', '<|user|>', '<|assistant|>', '<|observation|>'];
24083
+ function flintText(t) {
24084
+ let s = String(t ?? '').replace(/\x00/g, '').replace(/<think>/g, '').replace(/<\/think>/g, '');
24085
+ for (const m of FLINT_MARKERS) s = s.split(m).join('&lt;' + m.slice(1, -1) + '&gt;');
24086
+ return s;
24087
+ }
24088
+ export function flintChatPrompt(messages) {
24089
+ let s = '';
24090
+ for (const m of messages) {
24091
+ if (m.role === 'system') s += '<|system|>' + flintText(m.content);
24092
+ else if (m.role === 'developer') s += '<|developer|>' + flintText(m.content);
24093
+ else if (m.role === 'user') s += '<|user|>' + flintText(m.content);
24094
+ else if (m.role === 'tool') s += '<|observation|><tool_response>' + flintText(m.content) + '</tool_response>';
24095
+ else if (m.role === 'assistant') {
24096
+ let c = String(m.content ?? ''), r = String(m.reasoning_content ?? '');
24097
+ c = c.replace(/<\|think\|>([\s\S]*?)<\|\/think\|>/g, (_, x) => { r += x; return ''; });
24098
+ if (c.includes('</think>')) { const i = c.lastIndexOf('</think>'); r += c.slice(0, i).replace(/<think>/g, ''); c = c.slice(i + 8); }
24099
+ r = r.trim(); c = c.trim();
24100
+ s += '<|assistant|>' + (r ? `<think>${r}</think>` : '') + c;
24101
+ }
24102
+ }
24103
+ const last = messages[messages.length - 1];
24104
+ return last && last.role === 'assistant' ? s : s + '<|assistant|>';
24105
+ }
24106
+
24107
+ // ── WGSL ────────────────────────────────────────────────────────────────────
24108
+ // Q4_1 straight from the GGUF blocks: block (row r, block b) is words (rΒ·nb + b)Β·5 …+4 of `qw`; word 0 = f16 d | f16 m
24109
+ // << 16, words 1–4 the 16 nibble bytes in ggml's order (low nibbles = values 0..15, high = 16..31). Value = dΒ·q + m,
24110
+ // so a block's dot is dΒ·Ξ£ qΒ·x + mΒ·Ξ£ x. `aux` (binding 2) keeps the Q4_0 kernels' bindings; it is read once and unused.
24111
+ const Q41DOT = `
24112
+ fn q41dot(wd: u32, dm: vec2<f32>, xb: u32) -> f32 {
24113
+ let lo = f32(wd & 15u) * xin[xb] + f32((wd >> 8u) & 15u) * xin[xb + 1u]
24114
+ + f32((wd >> 16u) & 15u) * xin[xb + 2u] + f32((wd >> 24u) & 15u) * xin[xb + 3u];
24115
+ let hi = f32((wd >> 4u) & 15u) * xin[xb + 16u] + f32((wd >> 12u) & 15u) * xin[xb + 17u]
24116
+ + f32((wd >> 20u) & 15u) * xin[xb + 18u] + f32((wd >> 28u) & 15u) * xin[xb + 19u];
24117
+ let sx = xin[xb] + xin[xb + 1u] + xin[xb + 2u] + xin[xb + 3u] + xin[xb + 16u] + xin[xb + 17u] + xin[xb + 18u] + xin[xb + 19u];
24118
+ return dm.x * (lo + hi) + dm.y * sx;
24119
+ }`;
24120
+ const Q41_BINDINGS = `@group(0) @binding(0) var<storage, read> xin: array<f32>;
24121
+ @group(0) @binding(1) var<storage, read> qw: array<u32>;
24122
+ @group(0) @binding(2) var<storage, read> aux: array<u32>;
24123
+ @group(0) @binding(3) var<storage, read_write> y: array<f32>;`;
24124
+ // Four rows per workgroup; rows past M repeat the last one and are not written.
24125
+ const Q41_ROWS = (rowBase, xoff) => `
24126
+ var a0 = 0.0; var a1 = 0.0; var a2 = 0.0; var a3 = 0.0;
24127
+ let r0 = ${rowBase} + min(m0, M - 1u); let r1 = ${rowBase} + min(m0 + 1u, M - 1u); let r2 = ${rowBase} + min(m0 + 2u, M - 1u); let r3 = ${rowBase} + min(m0 + 3u, M - 1u);
24128
+ for (var w = t; w < nw; w += 256u) {
24129
+ let b = w >> 2u; let i = 1u + (w & 3u); let xb = ${xoff} + b * 32u + (w & 3u) * 4u;
24130
+ let k0 = (r0 * nb + b) * 5u; let k1 = (r1 * nb + b) * 5u; let k2 = (r2 * nb + b) * 5u; let k3 = (r3 * nb + b) * 5u;
24131
+ a0 += q41dot(qw[k0 + i], unpack2x16float(qw[k0]), xb);
24132
+ a1 += q41dot(qw[k1 + i], unpack2x16float(qw[k1]), xb);
24133
+ a2 += q41dot(qw[k2 + i], unpack2x16float(qw[k2]), xb);
24134
+ a3 += q41dot(qw[k3 + i], unpack2x16float(qw[k3]), xb);
24135
+ }
24136
+ part[t] = a0; part[256u + t] = a1; part[512u + t] = a2; part[768u + t] = a3;
24137
+ workgroupBarrier();
24138
+ for (var s = 128u; s > 0u; s >>= 1u) {
24139
+ if (t < s) { part[t] += part[t + s]; part[256u + t] += part[256u + t + s]; part[512u + t] += part[512u + t + s]; part[768u + t] += part[768u + t + s]; }
24140
+ workgroupBarrier();
24141
+ }`;
24142
+ const SOFTMAX_SINK = (rowExpr, headExpr, nExpr) => `
24143
+ let n = ${nExpr}; let base = ${rowExpr} * n; let t = l.x; let sink = sinks[${headExpr}];
24144
+ var mx = -3.4e38;
24145
+ for (var i = t; i < n; i += 256u) { mx = max(mx, sc[base + i]); }
24146
+ red[t] = mx; workgroupBarrier();
24147
+ for (var s = 128u; s > 0u; s >>= 1u) { if (t < s) { red[t] = max(red[t], red[t + s]); } workgroupBarrier(); }
24148
+ let m = max(red[0], sink); workgroupBarrier();
24149
+ var sum = 0.0;
24150
+ for (var i = t; i < n; i += 256u) { let e = exp(sc[base + i] - m); sc[base + i] = e; sum += e; }
24151
+ red[t] = sum; workgroupBarrier();
24152
+ for (var s = 128u; s > 0u; s >>= 1u) { if (t < s) { red[t] += red[t + s]; } workgroupBarrier(); }
24153
+ let inv = 1.0 / (red[0] + exp(sink - m));
24154
+ for (var i = t; i < n; i += 256u) { sc[base + i] = sc[base + i] * inv; }
24155
+ }`;
24156
+ const WIN = 'fn winStart(n: u32, win: u32) -> u32 { return select(0u, n - win, win > 0u && n > win); }';
24157
+ const QB = 'struct Q { T: u32, pos0: u32, S: u32, pad: u32 }';
24158
+
24159
+ const FL = {
24160
+ matmulQ41: `
24161
+ struct P { M: u32, N: u32 }
24162
+ ${Q41_BINDINGS}
24163
+ @group(0) @binding(4) var<uniform> p: P;
24164
+ ${Q41DOT}
24165
+ var<workgroup> part: array<f32, 1024>;
24166
+ @compute @workgroup_size(256) fn main(@builtin(workgroup_id) wg: vec3<u32>, @builtin(num_workgroups) ng: vec3<u32>, @builtin(local_invocation_id) l: vec3<u32>) {
24167
+ _ = aux[0];
24168
+ let m0 = (wg.y * ng.x + wg.x) * 4u; let t = l.x; let M = p.M; let nw = p.N / 8u; let nb = p.N / 32u;
24169
+ if (m0 >= M) { return; }${Q41_ROWS('0u', '0u')}
24170
+ if (t < 4u && m0 + t < M) { y[m0 + t] = part[t * 256u]; }
24171
+ }`,
24172
+ // Routed experts, all k at once (workgroup z = top-k position); slots[z] picks the expert's rows in the pool.
24173
+ expertQ41: `
24174
+ struct P { M: u32, N: u32, rowsPerExpert: u32, inputPerSlot: u32 }
24175
+ ${Q41_BINDINGS}
24176
+ @group(0) @binding(4) var<uniform> p: P;
24177
+ @group(0) @binding(5) var<storage, read> slots: array<u32>;
24178
+ ${Q41DOT}
24179
+ var<workgroup> part: array<f32, 1024>;
24180
+ @compute @workgroup_size(256) fn main(@builtin(workgroup_id) wg: vec3<u32>, @builtin(num_workgroups) ng: vec3<u32>, @builtin(local_invocation_id) l: vec3<u32>) {
24181
+ _ = aux[0];
24182
+ let z = wg.z; let m0 = (wg.y * ng.x + wg.x) * 4u; let t = l.x; let M = p.M; let nw = p.N / 8u; let nb = p.N / 32u;
24183
+ if (m0 >= M) { return; }
24184
+ let rb = slots[z] * p.rowsPerExpert; let xo = select(0u, z * p.N, p.inputPerSlot == 1u);${Q41_ROWS('rb', 'xo')}
24185
+ if (t < 4u && m0 + t < M) { y[z * M + m0 + t] = part[t * 256u]; }
24186
+ }`,
24187
+ // The F32 token table: x = row tok.token.
24188
+ embedF32: `
24189
+ ${TOK}
24190
+ struct P { n: u32 }
24191
+ @group(0) @binding(0) var<storage, read> emb: array<f32>;
24192
+ @group(0) @binding(1) var<storage, read_write> y: array<f32>;
24193
+ @group(0) @binding(2) var<uniform> tok: Tok;
24194
+ @group(0) @binding(3) var<uniform> p: P;
24195
+ @compute @workgroup_size(256) fn main(@builtin(global_invocation_id) g: vec3<u32>) {
24196
+ let i = g.x; if (i < p.n) { y[i] = emb[tok.token * p.n + i]; }
24197
+ }`,
24198
+ // Windowed scores (positions [start, seqLen)) times the 1/√hd scale.
24199
+ attnScoreSW: `enable f16;
24200
+ ${TOK}
24201
+ struct P { heads: u32, kvHeads: u32, hd: u32, win: u32, scale: f32, p0: u32, p1: u32, p2: u32 }
24202
+ @group(0) @binding(0) var<storage, read> q: array<f32>;
24203
+ @group(0) @binding(1) var<storage, read> kc: array<f16>;
24204
+ @group(0) @binding(2) var<storage, read_write> sc: array<f32>;
24205
+ @group(0) @binding(3) var<uniform> tok: Tok;
24206
+ @group(0) @binding(4) var<uniform> p: P;
24207
+ ${WIN}
24208
+ @compute @workgroup_size(256) fn main(@builtin(global_invocation_id) g: vec3<u32>) {
24209
+ let st = winStart(tok.seqLen, p.win); let n = tok.seqLen - st; let idx = g.x;
24210
+ if (idx >= p.heads * n) { return; }
24211
+ let h = idx / n; let j = idx % n; let kvh = h / (p.heads / p.kvHeads);
24212
+ let qo = h * p.hd; let ko = (st + j) * p.kvHeads * p.hd + kvh * p.hd;
24213
+ var acc = 0.0;
24214
+ for (var d = 0u; d < p.hd; d++) { acc += q[qo + d] * f32(kc[ko + d]); }
24215
+ sc[h * n + j] = acc * p.scale;
24216
+ }`,
24217
+ // Softmax over the window with the head's sink logit in the denominator (ggml_soft_max_ext with sinks).
24218
+ softmaxSinkW: `
24219
+ ${TOK}
24220
+ struct P { heads: u32, kvHeads: u32, hd: u32, win: u32 }
24221
+ @group(0) @binding(0) var<storage, read_write> sc: array<f32>;
24222
+ @group(0) @binding(1) var<storage, read> sinks: array<f32>;
24223
+ @group(0) @binding(2) var<uniform> tok: Tok;
24224
+ @group(0) @binding(3) var<uniform> p: P;
24225
+ ${WIN}
24226
+ var<workgroup> red: array<f32, 256>;
24227
+ @compute @workgroup_size(256) fn main(@builtin(workgroup_id) wg: vec3<u32>, @builtin(local_invocation_id) l: vec3<u32>) {${SOFTMAX_SINK('wg.x', 'wg.x', 'tok.seqLen - winStart(tok.seqLen, p.win)')}`,
24228
+ };
24229
+
24230
+ const FLB = {
24231
+ // Tiled Q4_1 GEMM (matmulQ4T's tiling): y[tΒ·M + m] = Ξ£_k x[tΒ·N + k]Β·W[m][k], 16 rows Γ— 16 tokens per workgroup.
24232
+ matmulQ41T: `
24233
+ struct P { M: u32, N: u32 }
24234
+ ${QB}
24235
+ @group(0) @binding(0) var<storage, read> x: array<f32>;
24236
+ @group(0) @binding(1) var<storage, read> qw: array<u32>;
24237
+ @group(0) @binding(2) var<storage, read> aux: array<u32>;
24238
+ @group(0) @binding(3) var<storage, read_write> y: array<f32>;
24239
+ @group(0) @binding(4) var<uniform> p: P;
24240
+ @group(0) @binding(5) var<uniform> qd: Q;
24241
+ var<workgroup> ws: array<f32, 512>;
24242
+ var<workgroup> xs: array<f32, 512>;
24243
+ @compute @workgroup_size(256) fn main(@builtin(workgroup_id) wg: vec3<u32>, @builtin(local_invocation_id) l: vec3<u32>) {
24244
+ _ = aux[0];
24245
+ let m0 = wg.x * 16u; let t0 = wg.y * 16u; let r = l.x / 16u; let c = l.x % 16u;
24246
+ let nb = p.N / 32u; var acc = 0.0;
24247
+ for (var b = 0u; b < nb; b++) {
24248
+ for (var i = l.x; i < 512u; i += 256u) {
24249
+ let rr = i / 32u; let kk = i % 32u;
24250
+ let m = m0 + rr; var wv = 0.0;
24251
+ if (m < p.M) {
24252
+ let k = (m * nb + b) * 5u; let dm = unpack2x16float(qw[k]);
24253
+ let wd = qw[k + 1u + ((kk & 15u) >> 2u)];
24254
+ wv = f32((wd >> ((kk & 3u) * 8u + (kk >> 4u) * 4u)) & 15u) * dm.x + dm.y;
24255
+ }
24256
+ ws[i] = wv;
24257
+ let t = t0 + rr; var xv = 0.0;
24258
+ if (t < qd.T) { xv = x[t * p.N + b * 32u + kk]; }
24259
+ xs[i] = xv;
24260
+ }
24261
+ workgroupBarrier();
24262
+ for (var kk = 0u; kk < 32u; kk++) { acc += ws[r * 32u + kk] * xs[c * 32u + kk]; }
24263
+ workgroupBarrier();
24264
+ }
24265
+ let m = m0 + r; let t = t0 + c;
24266
+ if (m < p.M && t < qd.T) { y[t * p.M + m] = acc; }
24267
+ }`,
24268
+ // Routed experts for every (token, pick) pair: z = tΒ·K + j. Input: the token's row (gate/up, inputMode 0) or the
24269
+ // pair's own row (down, inputMode 1).
24270
+ expertQ41B: `
24271
+ struct P { M: u32, N: u32, rowsPerExpert: u32, inputMode: u32, K: u32 }
24272
+ ${Q41_BINDINGS}
24273
+ @group(0) @binding(4) var<uniform> p: P;
24274
+ @group(0) @binding(5) var<storage, read> slots: array<u32>;
24275
+ ${Q41DOT}
24276
+ var<workgroup> part: array<f32, 1024>;
24277
+ @compute @workgroup_size(256) fn main(@builtin(workgroup_id) wg: vec3<u32>, @builtin(num_workgroups) ng: vec3<u32>, @builtin(local_invocation_id) l: vec3<u32>) {
24278
+ _ = aux[0];
24279
+ let z = wg.z; let m0 = (wg.y * ng.x + wg.x) * 4u; let t = l.x; let M = p.M; let nw = p.N / 8u; let nb = p.N / 32u;
24280
+ if (m0 >= M) { return; }
24281
+ let rb = slots[z] * p.rowsPerExpert; let xo = select((z / p.K) * p.N, z * p.N, p.inputMode == 1u);${Q41_ROWS('rb', 'xo')}
24282
+ if (t < 4u && m0 + t < M) { y[z * M + m0 + t] = part[t * 256u]; }
24283
+ }`,
24284
+ embedF32B: `
24285
+ struct P { n: u32 }
24286
+ ${QB}
24287
+ @group(0) @binding(0) var<storage, read> emb: array<f32>;
24288
+ @group(0) @binding(1) var<storage, read> ids: array<u32>;
24289
+ @group(0) @binding(2) var<storage, read_write> y: array<f32>;
24290
+ @group(0) @binding(3) var<uniform> p: P;
24291
+ @group(0) @binding(4) var<uniform> qd: Q;
24292
+ @compute @workgroup_size(256) fn main(@builtin(global_invocation_id) g: vec3<u32>) {
24293
+ let i = g.x; let t = g.y; if (i >= p.n || t >= qd.T) { return; }
24294
+ y[t * p.n + i] = emb[ids[t] * p.n + i];
24295
+ }`,
24296
+ // Causal (and, on sliding layers, windowed) scores for T queries over S = pos0 + T keys, times the scale.
24297
+ attnScoreSWB: `enable f16;
24298
+ struct P { heads: u32, kvHeads: u32, hd: u32, win: u32, scale: f32, p0: u32, p1: u32, p2: u32 }
24299
+ ${QB}
24300
+ @group(0) @binding(0) var<storage, read> q: array<f32>;
24301
+ @group(0) @binding(1) var<storage, read> kc: array<f16>;
24302
+ @group(0) @binding(2) var<storage, read_write> sc: array<f32>;
24303
+ @group(0) @binding(3) var<uniform> p: P;
24304
+ @group(0) @binding(4) var<uniform> qd: Q;
24305
+ @compute @workgroup_size(256) fn main(@builtin(global_invocation_id) g: vec3<u32>, @builtin(num_workgroups) ng: vec3<u32>) {
24306
+ let idx = g.y * ng.x * 256u + g.x; let S = qd.S; let T = qd.T;
24307
+ if (idx >= p.heads * T * S) { return; }
24308
+ let h = idx / (T * S); let rem = idx % (T * S); let t = rem / S; let pos = rem % S; let qp = qd.pos0 + t;
24309
+ if (pos > qp || (p.win > 0u && qp - pos >= p.win)) { sc[idx] = -3.4e38; return; }
24310
+ let kvh = h / (p.heads / p.kvHeads);
24311
+ let qo = t * p.heads * p.hd + h * p.hd; let ko = pos * p.kvHeads * p.hd + kvh * p.hd;
24312
+ var acc = 0.0;
24313
+ for (var d = 0u; d < p.hd; d++) { acc += q[qo + d] * f32(kc[ko + d]); }
24314
+ sc[idx] = acc * p.scale;
24315
+ }`,
24316
+ // Row wg.x = hΒ·T + t over S keys (masked ones are -3.4e38 and come out 0), with the head's sink.
24317
+ softmaxSinkB: `
24318
+ ${QB}
24319
+ @group(0) @binding(0) var<storage, read_write> sc: array<f32>;
24320
+ @group(0) @binding(1) var<storage, read> sinks: array<f32>;
24321
+ @group(0) @binding(2) var<uniform> qd: Q;
24322
+ var<workgroup> red: array<f32, 256>;
24323
+ @compute @workgroup_size(256) fn main(@builtin(workgroup_id) wg: vec3<u32>, @builtin(local_invocation_id) l: vec3<u32>) {${SOFTMAX_SINK('wg.x', 'wg.x / qd.T', 'qd.S')}`,
24324
+ };
24325
+
24326
+ // ── Engine ──────────────────────────────────────────────────────────────────
24327
+ export class FlintMoeSsd extends Lfm2MoeSsd {
24328
+ static configFrom(kv) { return configFromGgufFlint(kv); }
24329
+ static get ingestPlan() { return FLINT_INGEST_PLAN; }
24330
+ static tokenizerFrom(kv) { return new FlintTokenizer(kv); }
24331
+ static load(modelId, opts = {}) { return Qwen3MoeSsd.load.call(this, modelId, { ...opts, source: opts.source || FLINT_40B_A2B }); }
24332
+
24333
+ chatPrompt(messages) { return flintChatPrompt(messages); }
24334
+ get stopTokenIds() {
24335
+ const t = this.tokenizer.ids;
24336
+ return [this.cfg.eos, t.get('<|user|>'), t.get('<|observation|>'), t.get('<|developer|>')].filter((x) => x !== undefined);
24337
+ }
24338
+ get thinkOpenTokenId() { return this.tokenizer.ids.get('<think>') ?? null; }
24339
+ get thinkCloseTokenId() { return this.tokenizer.ids.get('</think>') ?? null; }
24340
+ get thinkInPrompt() { return false; }
24341
+ get alwaysThinks() { return false; }
24342
+ get thinkPreamble() { return ''; }
24343
+
24344
+ get kq() { return { mm: 'matmulQ41', mmT: 'matmulQ41T', ex: 'expertQ41', exB: 'expertQ41B' }; }
24345
+ get attnK() { return { score: 'attnScoreSW', soft: 'softmaxSinkW', out: 'attnOutW', scoreB: 'attnScoreSWB', softB: 'softmaxSinkB', outB: 'attnOutB' }; }
24346
+ get embedKernel() { return 'embedF32'; }
24347
+ get embedKernelB() { return 'embedF32B'; }
24348
+ get lmKernel() { return 'matmulQ41'; }
24349
+ get kernels() {
24350
+ return (this._flKernels ||= { ...super.kernels, ...FL, ...(this.batchPrefill ? FLB : {}) });
24351
+ }
24352
+
24353
+ // Q4_1 and F32 tensors arrive raw. A Q4_1 matrix binds its blocks as `q` and a 16-byte unused buffer as `s`; each MoE
24354
+ // layer gets a zero router bias (this file has none; topkSig adds one before picking).
24355
+ async uploadDense(onProgress) {
24356
+ const dt = this.manifest.dense.tensors;
24357
+ for (const [name, d] of Object.entries(dt)) if (d.type !== 'f32' && d.type !== 'q41') throw new Error(`${name}: ${d.type} in a Q4_1 flint file`);
24358
+ await Qwen3MoeSsd.prototype.uploadDense.call(this, onProgress);
24359
+ this.ropeFreqs = null;
24360
+ this.aux = this.buffer(16, STORAGE, 'dense');
24361
+ for (const [name, d] of Object.entries(dt)) if (d.type === 'q41') Object.assign(this.w[name], { q: this.w[name].raw, s: this.aux });
24362
+ const c = this.cfg, zero = this.buffer(c.experts * 4, STORAGE, 'dense');
24363
+ for (let L = c.lead; L < c.modelLayers; L++) this.w[`blk.${L}.exp_probs_b`] = { raw: zero };
24364
+ }
24365
+
24366
+ // The base binds, then Flint's own: the token table and head, an identity RoPE table for the NoPE layers, and per
24367
+ // layer the windowed, scaled scores and the softmax with sinks.
24368
+ initBuffers() {
24369
+ const c = this.cfg, H = c.hidden, hd = c.attnHeadDim, W = this.w;
24370
+ this.ropeId = this.buffer(hd * 4, STORAGE | 0x8 /* COPY_DST */);
24371
+ this.device.queue.writeBuffer(this.ropeId, 0, identityRope(hd, 1));
24372
+ super.initBuffers();
24373
+ const a = this.a, qk = this.uniform([hd, { f: c.eps }]), scale = { f: Math.fround(1 / Math.sqrt(hd)) };
24374
+ this.g.embed = this.bind('embedF32', [W['token_embd.weight'].raw, a.x, this.tok, this.uniform([H])]);
24375
+ this.g.lm = this.bind('matmulQ41', [a.xn, W['output.weight'].q, W['output.weight'].s, a.logits, this.uniform([c.vocab, H])]);
24376
+ for (let L = 0; L < c.modelLayers; L++) {
24377
+ const ly = this.mlayers[L], p = (n) => W[`blk.${L}.${n}`];
24378
+ if (!c.rope[L]) {
24379
+ ly.ropeQ = this.bind('qkNormRope', [a.q, p('attn_q_norm.weight').raw, this.ropeId, qk]);
24380
+ ly.ropeK = this.bind('qkNormRope', [a.k, p('attn_k_norm.weight').raw, this.ropeId, qk]);
24381
+ }
24382
+ const win = this.uniform([c.heads, c.kvHeadsAttn, hd, c.win[L]]);
24383
+ ly.score = this.bind('attnScoreSW', [a.q, this.kc[L], a.sc, this.tok, this.uniform([c.heads, c.kvHeadsAttn, hd, c.win[L], scale, 0, 0, 0])]);
24384
+ ly.soft = this.bind('softmaxSinkW', [a.sc, p('attn_sinks.weight').raw, this.tok, win]);
24385
+ ly.attOut = this.bind('attnOutW', [a.sc, this.vc[L], a.att, this.tok, win]);
24386
+ }
24387
+ }
24388
+ initBatch() {
24389
+ super.initBatch();
24390
+ const c = this.cfg, B = this.chunkTokens, H = c.hidden, hd = c.attnHeadDim, W = this.w, b = this.b, qb = this.qb;
24391
+ const QN = c.heads * hd, KVN = c.kvHeadsAttn * hd, eps = { f: c.eps }, scale = { f: Math.fround(1 / Math.sqrt(hd)) };
24392
+ this.ropeIdB = this.buffer(B * hd * 4, STORAGE | 0x8, 'batch');
24393
+ this.device.queue.writeBuffer(this.ropeIdB, 0, identityRope(hd, B));
24394
+ this.gB.embed = this.bind('embedF32B', [W['token_embd.weight'].raw, b.ids, b.x, this.uniform([H]), qb]);
24395
+ const rq = this.uniform([hd, QN, eps]), rk = this.uniform([hd, KVN, eps]), attOut = this.uniform([c.heads, c.kvHeadsAttn, hd, 0]);
24396
+ for (let L = 0; L < c.modelLayers; L++) {
24397
+ const B_ = this.mlayers[L].B, p = (n) => W[`blk.${L}.${n}`];
24398
+ if (!c.rope[L]) {
24399
+ B_.ropeQ = this.bind('qkNormRopeB', [b.q, p('attn_q_norm.weight').raw, this.ropeIdB, rq]);
24400
+ B_.ropeK = this.bind('qkNormRopeB', [b.k, p('attn_k_norm.weight').raw, this.ropeIdB, rk]);
24401
+ }
24402
+ B_.score = this.bind('attnScoreSWB', [b.q, this.kc[L], b.sc, this.uniform([c.heads, c.kvHeadsAttn, hd, c.win[L], scale, 0, 0, 0]), qb]);
24403
+ B_.soft = this.bind('softmaxSinkB', [b.sc, p('attn_sinks.weight').raw, qb]);
24404
+ B_.attOut = this.bind('attnOutB', [b.sc, this.vc[L], b.att, attOut, qb]);
24405
+ }
24406
+ }
24407
+
24408
+ // No recurrent state: thinking follows the model (LFM2.5 forces it on).
24409
+ async *generateTurn(messages, opts = {}) { yield* Gemma4MoeSsd.prototype.generateTurn.call(this, messages, opts); }
24410
+ }
24411
+
24412
+ // n rows of [cos | sin] = [1 … 1 | 0 … 0]: qkNormRope with this table only normalises.
24413
+ function identityRope(hd, n) {
24414
+ const r = new Float32Array(n * hd);
24415
+ for (let t = 0; t < n; t++) r.fill(1, t * hd, t * hd + hd / 2);
24416
+ return r;
24417
+ }
24418
+
24419
+ // For kernel tests (scripts): the WGSL this module adds.
24420
+ export { FL as FLINT_KERNELS, FLB as FLINT_BATCH_KERNELS };
24421
+ </script>
24422
  <script type="text/plain" data-module="gemma-4-e2b.js">// Gemma-4 E2B (QAT mobile) WebGPU chat bundle. Import { Gemma4Mobile } from this file.
24423
  // Built by LocalMind scripts/build-gemma-4-e2b.mjs from webml-community/gemma-4-webgpu-kernels@158f16a:
24424
  // adds load(id, { ple }) to serve the per-layer embedding table from OPFS (see ple-opfs.js).
 
31282
  /[δΈ€-ιΎ₯぀-γ‚Ÿγ‚ -γƒΏ]+/gu,
31283
  /[!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~][A-Za-z]+|[^\r\n\p{L}\p{P}\p{S}]?[\p{L}\p{M}]+| ?[\p{P}\p{S}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+/gu,
31284
  ];
31285
+ export function isolate(pieces, re) {
31286
  const out = [];
31287
  for (const s of pieces) {
31288
  let last = 0;
 
38673
  // Kernel names for the matrix and expert weights: Q4_0 here; an engine reading a Q8_0 file (hy_v3_moe_ssd.js) swaps
38674
  // in the Q8_0 kernels, which take the same bindings and dispatch sizes.
38675
  get kq() { return { mm: 'matmulQ4', mmT: 'matmulQ4T', ex: 'expertQ4', exB: 'expertQ4B' }; }
38676
+ // Attention kernel names (decode, then chunk); an engine with sinks or a window (flint_moe_ssd.js) rebinds its own.
38677
+ get attnK() { return { score: 'attnScore', soft: 'softmax', out: 'attnOut', scoreB: 'attnScoreB', softB: 'softmaxB', outB: 'attnOutB' }; }
38678
 
38679
  chatPrompt(messages) { return lfm2ChatPrompt(messages); }
38680
  get stopTokenIds() { return [this.cfg.eos, this.tokenizer.ids.get('<|im_end|>'), this.tokenizer.ids.get('<|endoftext|>')].filter((t) => t !== undefined); }
 
38896
  d(this.kq.mm, g.q, Math.ceil(QN / 4)); d(this.kq.mm, g.k, Math.ceil(KVN / 4)); d(this.kq.mm, g.v, Math.ceil(KVN / 4));
38897
  d('qkNormRope', g.ropeQ, c.heads); d('qkNormRope', g.ropeK, c.kvHeadsAttn);
38898
  d('kvStore', g.kv, Math.ceil(KVN / 256));
38899
+ const ak = this.attnK;
38900
+ d(ak.score, g.score, Math.ceil(c.heads * seqLen / 256));
38901
+ d(ak.soft, g.soft, c.heads);
38902
+ d(ak.out, g.attOut, Math.ceil(QN / 256));
38903
  d(this.kq.mm, g.o, Math.ceil(H / 4));
38904
  } else {
38905
  d(this.kq.mm, g.inProj, Math.ceil(3 * H / 4));
 
38957
  d(this.kq.mmT, g.q, mm(QN), tt); d(this.kq.mmT, g.k, mm(KVN), tt); d(this.kq.mmT, g.v, mm(KVN), tt);
38958
  d('qkNormRopeB', g.ropeQ, c.heads, T); d('qkNormRopeB', g.ropeK, c.kvHeadsAttn, T);
38959
  d('kvStoreB', g.kv, Math.ceil(KVN / 256), T);
38960
+ const ak = this.attnK;
38961
+ d(ak.scoreB, g.score, Math.ceil(c.heads * T * S / 256));
38962
+ d(ak.softB, g.soft, c.heads * T);
38963
+ d(ak.outB, g.attOut, Math.ceil(QN / 256), T);
38964
  d(this.kq.mmT, g.o, mm(H), tt);
38965
  } else {
38966
  d(this.kq.mmT, g.inProj, mm(3 * H), tt);