NakliTechie commited on
Commit
23e2354
Β·
1 Parent(s): a8786a2

Sync from GitHub 2026-10-29T07:59:06Z

Browse files
Files changed (1) hide show
  1. index.html +81 -7
index.html CHANGED
@@ -2753,6 +2753,10 @@
2753
  <label style="display:flex;align-items:center;gap:8px;font-weight:400;cursor:pointer">
2754
  <input type="checkbox" id="ragRerankToggle"> Rerank retrieved memory <span style="font-weight:400;color:var(--gray-500)">(bge-reranker-v2-m3 re-scores the memories found for each message and for the memory tools, for sharper recall &mdash; a ~570&nbsp;MB on-device model, downloaded on first use)</span>
2755
  </label>
 
 
 
 
2756
  <label style="display:flex;align-items:center;gap:8px;margin-top:8px;font-weight:400;cursor:pointer">
2757
  <input type="checkbox" id="memoryWriterAutoToggle"> Write memories after each reply <span style="font-weight:400;color:var(--gray-500)">(when a reply finishes, the loaded model reads that exchange, saves new facts about you and updates or removes memories they change; it waits for the reply, and stops when you send the next message. Memory panel &rarr; &ldquo;Write memories from this chat&rdquo; does the same for the whole chat)</span>
2758
  </label>
@@ -10346,6 +10350,9 @@ self.addEventListener('message', async (e) => {
10346
  const KEYLESS_WEB_KEY = 'lm_keyless_web';
10347
  // On by default (Chirag, 2026-10-29: "keyless web tools should be on by default"); each lookup still asks first
10348
  // through the approval card (approvalHost), and switching the setting off hides both tools.
 
 
 
10349
  function keylessWebOn() { try { return localStorage.getItem(KEYLESS_WEB_KEY) !== 'off'; } catch { return true; } }
10350
  async function fetchToolJson(url) {
10351
  const res = await fetch(url, { signal: AbortSignal.timeout(15000) });
@@ -12495,6 +12502,11 @@ self.addEventListener('message', async (e) => {
12495
  try { localStorage.setItem('lm_rag_rerank', ragRerankToggle.checked ? '1' : '0'); } catch {}
12496
  });
12497
  }
 
 
 
 
 
12498
 
12499
  // ── JavaScript API: chip, toggle, activity log ──────────────
12500
  const apiChip = document.getElementById('apiChip');
@@ -17043,6 +17055,14 @@ self.addEventListener('message', async (e) => {
17043
  if (self._embReady) return;
17044
  if (self._embWorker) return self._ensureEmbWorker(); // another caller started it while the dialog was open
17045
  self._embWorker = createEmbeddingWorker();
 
 
 
 
 
 
 
 
17046
  return new Promise((resolve, reject) => {
17047
  const handler = (e) => {
17048
  if (e.data.type === 'ready') {
@@ -17057,7 +17077,7 @@ self.addEventListener('message', async (e) => {
17057
  }
17058
  };
17059
  self._embWorker.addEventListener('message', handler);
17060
- self._embWorker.postMessage({ type: 'load' });
17061
  });
17062
  });
17063
  },
@@ -17256,6 +17276,7 @@ import {
17256
  env,
17257
  pipeline,
17258
  AutoTokenizer,
 
17259
  AutoModelForSequenceClassification,
17260
  } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@4.3.2/+esm';
17261
 
@@ -17264,6 +17285,41 @@ env.localModelPath = '/models/';
17264
  env.allowRemoteModels = true;
17265
 
17266
  let embedder = null;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17267
  let reranker = null; // lazy cross-encoder (SequenceClassification) for rerank
17268
  let rerankTokenizer = null;
17269
  let rerankerId = null; // model id the reranker was loaded with
@@ -17279,12 +17335,21 @@ self.addEventListener('message', async (e) => {
17279
  // smallest ONNX variant is ~541 MB (a per-user download on every RAG use),
17280
  // chat-model-sized for an embedder. The reranker below is the precision
17281
  // lever instead. See ROADMAP Β§"RAG embedder" if we revisit it.
17282
- embedder = await pipeline('feature-extraction', 'Xenova/multilingual-e5-small', {
17283
- device: 'wasm',
17284
- dtype: 'q8',
17285
- progress_callback: (p) => self.postMessage({ type: 'progress', data: p }),
17286
- });
17287
- self.postMessage({ type: 'ready' });
 
 
 
 
 
 
 
 
 
17288
  } catch (err) {
17289
  self.postMessage({ type: 'error', message: err.message || String(err) });
17290
  }
@@ -17293,6 +17358,15 @@ self.addEventListener('message', async (e) => {
17293
  const prefix = (kind === 'query') ? 'query: ' : 'passage: ';
17294
  const results = [];
17295
  for (const text of texts) {
 
 
 
 
 
 
 
 
 
17296
  const output = await embedder(prefix + text, { pooling: 'mean', normalize: true });
17297
  results.push(Array.from(output.data));
17298
  }
 
2753
  <label style="display:flex;align-items:center;gap:8px;font-weight:400;cursor:pointer">
2754
  <input type="checkbox" id="ragRerankToggle"> Rerank retrieved memory <span style="font-weight:400;color:var(--gray-500)">(bge-reranker-v2-m3 re-scores the memories found for each message and for the memory tools, for sharper recall &mdash; a ~570&nbsp;MB on-device model, downloaded on first use)</span>
2755
  </label>
2756
+ <label style="display:flex;align-items:center;gap:8px;margin-top:8px;font-weight:400;cursor:pointer">
2757
+ <input type="checkbox" id="npuEmbedToggle"> Run the memory model on the Neural Engine <span style="font-weight:400;color:var(--gray-500)">(experimental; needs Chrome&rsquo;s WebNN flag, chrome://flags/#web-machine-learning-neural-network; downloads a 235 MB copy of multilingual-e5-small; uses the CPU when WebNN is missing; takes effect after a reload)</span>
2758
+ </label>
2759
+ <div id="npuEmbedStatus" style="font-size:0.75rem;color:var(--gray-500);margin-top:2px;min-height:1em"></div>
2760
  <label style="display:flex;align-items:center;gap:8px;margin-top:8px;font-weight:400;cursor:pointer">
2761
  <input type="checkbox" id="memoryWriterAutoToggle"> Write memories after each reply <span style="font-weight:400;color:var(--gray-500)">(when a reply finishes, the loaded model reads that exchange, saves new facts about you and updates or removes memories they change; it waits for the reply, and stops when you send the next message. Memory panel &rarr; &ldquo;Write memories from this chat&rdquo; does the same for the whole chat)</span>
2762
  </label>
 
10350
  const KEYLESS_WEB_KEY = 'lm_keyless_web';
10351
  // On by default (Chirag, 2026-10-29: "keyless web tools should be on by default"); each lookup still asks first
10352
  // through the approval card (approvalHost), and switching the setting off hides both tools.
10353
+ // The memory model (multilingual-e5-small) on the Neural Engine through WebNN (Chrome's WebNN flag); off by default.
10354
+ const NPU_EMBED_KEY = 'lm_npu_embed';
10355
+ function npuEmbedOn() { try { return localStorage.getItem(NPU_EMBED_KEY) === 'on'; } catch { return false; } }
10356
  function keylessWebOn() { try { return localStorage.getItem(KEYLESS_WEB_KEY) !== 'off'; } catch { return true; } }
10357
  async function fetchToolJson(url) {
10358
  const res = await fetch(url, { signal: AbortSignal.timeout(15000) });
 
12502
  try { localStorage.setItem('lm_rag_rerank', ragRerankToggle.checked ? '1' : '0'); } catch {}
12503
  });
12504
  }
12505
+ const npuEmbedToggle = document.getElementById('npuEmbedToggle');
12506
+ if (npuEmbedToggle) {
12507
+ npuEmbedToggle.checked = npuEmbedOn();
12508
+ npuEmbedToggle.addEventListener('change', () => { try { localStorage.setItem(NPU_EMBED_KEY, npuEmbedToggle.checked ? 'on' : 'off'); } catch {} });
12509
+ }
12510
 
12511
  // ── JavaScript API: chip, toggle, activity log ──────────────
12512
  const apiChip = document.getElementById('apiChip');
 
17055
  if (self._embReady) return;
17056
  if (self._embWorker) return self._ensureEmbWorker(); // another caller started it while the dialog was open
17057
  self._embWorker = createEmbeddingWorker();
17058
+ // Which hardware the memory model runs on: set at 'ready', changed by 'backend' if a Neural Engine run fails.
17059
+ self._embWorker.addEventListener('message', (e) => {
17060
+ if (e.data.type !== 'ready' && e.data.type !== 'backend') return;
17061
+ self.embedBackend = e.data.backend || 'wasm';
17062
+ const st = document.getElementById('npuEmbedStatus');
17063
+ if (st) st.textContent = self.embedBackend === 'webnn-npu' ? 'The memory model runs on the Neural Engine.'
17064
+ : npuEmbedOn() ? 'The memory model runs on the CPU: ' + (e.data.fallback || 'WebNN is not available in this browser.') : '';
17065
+ });
17066
  return new Promise((resolve, reject) => {
17067
  const handler = (e) => {
17068
  if (e.data.type === 'ready') {
 
17077
  }
17078
  };
17079
  self._embWorker.addEventListener('message', handler);
17080
+ self._embWorker.postMessage({ type: 'load', npu: npuEmbedOn() });
17081
  });
17082
  });
17083
  },
 
17276
  env,
17277
  pipeline,
17278
  AutoTokenizer,
17279
+ AutoModel,
17280
  AutoModelForSequenceClassification,
17281
  } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@4.3.2/+esm';
17282
 
 
17285
  env.allowRemoteModels = true;
17286
 
17287
  let embedder = null;
17288
+ // Neural Engine path (load message npu: true, WebNN present): e5-small fp16 through onnxruntime-web's WebNN provider.
17289
+ // WebNN needs fixed shapes, so a text is padded to the shortest of NPU_LENS that holds it (512 is the model's limit,
17290
+ // so nothing is cut that the pipeline would keep). Padding costs: 1.9 ms per text at 128 tokens, 14 ms at 512 (M4 Pro,
17291
+ // plan/2026-10-29-webnn-embed). Each length is its own session, made on first use. Pooling is the pipeline's: mean
17292
+ // over real tokens, then L2 norm.
17293
+ const NPU_LENS = [128, 512];
17294
+ let npuOn = false, npuTokenizer = null;
17295
+ const npuModels = {};
17296
+ async function npuSession(len) {
17297
+ npuModels[len] ||= AutoModel.from_pretrained('Xenova/multilingual-e5-small', {
17298
+ device: 'webnn-npu',
17299
+ dtype: 'fp16',
17300
+ session_options: { freeDimensionOverrides: { batch_size: 1, sequence_length: len } },
17301
+ progress_callback: (p) => self.postMessage({ type: 'progress', data: p }),
17302
+ });
17303
+ return npuModels[len];
17304
+ }
17305
+ async function npuEmbed(text) {
17306
+ const n0 = npuTokenizer(text, { truncation: true, max_length: NPU_LENS[NPU_LENS.length - 1] }).input_ids.dims[1];
17307
+ const len = NPU_LENS.find((l) => n0 <= l);
17308
+ const enc = npuTokenizer(text, { padding: 'max_length', truncation: true, max_length: len });
17309
+ const h = (await (await npuSession(len))(enc)).last_hidden_state, D = h.dims[2], m = enc.attention_mask.data, x = h.data;
17310
+ const v = new Float32Array(D); let n = 0;
17311
+ for (let t = 0; t < len; t++) if (Number(m[t])) { n++; for (let d = 0; d < D; d++) v[d] += Number(x[t * D + d]); }
17312
+ let s = 0; for (let d = 0; d < D; d++) { v[d] /= n; s += v[d] * v[d]; }
17313
+ s = Math.sqrt(s) || 1; for (let d = 0; d < D; d++) v[d] /= s;
17314
+ return Array.from(v);
17315
+ }
17316
+ async function loadWasmEmbedder() {
17317
+ embedder = await pipeline('feature-extraction', 'Xenova/multilingual-e5-small', {
17318
+ device: 'wasm',
17319
+ dtype: 'q8',
17320
+ progress_callback: (p) => self.postMessage({ type: 'progress', data: p }),
17321
+ });
17322
+ }
17323
  let reranker = null; // lazy cross-encoder (SequenceClassification) for rerank
17324
  let rerankTokenizer = null;
17325
  let rerankerId = null; // model id the reranker was loaded with
 
17335
  // smallest ONNX variant is ~541 MB (a per-user download on every RAG use),
17336
  // chat-model-sized for an embedder. The reranker below is the precision
17337
  // lever instead. See ROADMAP Β§"RAG embedder" if we revisit it.
17338
+ let fallback = null;
17339
+ if (e.data.npu && 'ml' in navigator) {
17340
+ try {
17341
+ npuTokenizer = await AutoTokenizer.from_pretrained('Xenova/multilingual-e5-small');
17342
+ npuOn = true;
17343
+ await npuEmbed('query: warm up');
17344
+ } catch (err) {
17345
+ npuOn = false;
17346
+ fallback = 'the Neural Engine path failed to load (' + (err.message || String(err)).slice(0, 160) + ').';
17347
+ }
17348
+ } else if (e.data.npu) {
17349
+ fallback = 'WebNN is not available in this browser (turn on chrome://flags/#web-machine-learning-neural-network).';
17350
+ }
17351
+ if (!npuOn) await loadWasmEmbedder();
17352
+ self.postMessage({ type: 'ready', backend: npuOn ? 'webnn-npu' : 'wasm', fallback });
17353
  } catch (err) {
17354
  self.postMessage({ type: 'error', message: err.message || String(err) });
17355
  }
 
17358
  const prefix = (kind === 'query') ? 'query: ' : 'passage: ';
17359
  const results = [];
17360
  for (const text of texts) {
17361
+ if (npuOn) {
17362
+ try { results.push(await npuEmbed(prefix + text)); continue; }
17363
+ catch (err) {
17364
+ // A Neural Engine run failed: switch to the CPU path for the rest of this session.
17365
+ npuOn = false;
17366
+ self.postMessage({ type: 'backend', backend: 'wasm', fallback: 'a Neural Engine run failed (' + (err.message || String(err)).slice(0, 160) + ').' });
17367
+ if (!embedder) await loadWasmEmbedder();
17368
+ }
17369
+ }
17370
  const output = await embedder(prefix + text, { pooling: 'mean', normalize: true });
17371
  results.push(Array.from(output.data));
17372
  }