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

Sync from GitHub 2026-10-29T08:12:22Z

Browse files
Files changed (1) hide show
  1. index.html +21 -8
index.html CHANGED
@@ -2754,7 +2754,7 @@
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">
@@ -10350,9 +10350,16 @@ self.addEventListener('message', async (e) => {
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) });
@@ -17050,7 +17057,7 @@ self.addEventListener('message', async (e) => {
17050
  });
17051
  }
17052
  // Memory and documents need the embedding model: ask before its first download (issue #7).
17053
- return askDownload(EMBED_DOWNLOAD).then((ok) => {
17054
  if (!ok) throw new Error('memory needs the multilingual-e5-small model (118 MB), and its download was declined. Reload the page to be asked again.');
17055
  if (self._embReady) return;
17056
  if (self._embWorker) return self._ensureEmbWorker(); // another caller started it while the dialog was open
@@ -17060,8 +17067,9 @@ self.addEventListener('message', async (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) => {
@@ -17291,6 +17299,11 @@ let embedder = null;
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) {
@@ -17340,13 +17353,13 @@ self.addEventListener('message', async (e) => {
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 });
 
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" checked> Run the memory model on the Neural Engine when the browser allows it <span style="font-weight:400;color:var(--gray-500)">(WebNN; in Chrome it needs chrome://flags/#web-machine-learning-neural-network. With WebNN it downloads a 235 MB copy of multilingual-e5-small; without it, or if a check fails, the CPU runs it as before. 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">
 
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, on unless switched off: a browser
10354
+ // without WebNN never downloads the fp16 copy and runs the CPU path as before (Chirag 2026-10-29: leave it on).
10355
  const NPU_EMBED_KEY = 'lm_npu_embed';
10356
+ function npuEmbedOn() { try { return localStorage.getItem(NPU_EMBED_KEY) !== 'off'; } catch { return true; } }
10357
+ // The Neural Engine path loads the fp16 file (235 MB) instead of the q8 one (118 MB); ask with the size it fetches.
10358
+ function embedDownloadSpec() {
10359
+ return npuEmbedOn() && typeof navigator !== 'undefined' && 'ml' in navigator
10360
+ ? { ...EMBED_DOWNLOAD, bytes: 235e6, label: 'the multilingual-e5-small memory model (fp16, for the Neural Engine)' }
10361
+ : EMBED_DOWNLOAD;
10362
+ }
10363
  function keylessWebOn() { try { return localStorage.getItem(KEYLESS_WEB_KEY) !== 'off'; } catch { return true; } }
10364
  async function fetchToolJson(url) {
10365
  const res = await fetch(url, { signal: AbortSignal.timeout(15000) });
 
17057
  });
17058
  }
17059
  // Memory and documents need the embedding model: ask before its first download (issue #7).
17060
+ return askDownload(embedDownloadSpec()).then((ok) => {
17061
  if (!ok) throw new Error('memory needs the multilingual-e5-small model (118 MB), and its download was declined. Reload the page to be asked again.');
17062
  if (self._embReady) return;
17063
  if (self._embWorker) return self._ensureEmbWorker(); // another caller started it while the dialog was open
 
17067
  if (e.data.type !== 'ready' && e.data.type !== 'backend') return;
17068
  self.embedBackend = e.data.backend || 'wasm';
17069
  const st = document.getElementById('npuEmbedStatus');
17070
+ // Quiet unless the Neural Engine runs it or tried and failed: most browsers have no WebNN yet.
17071
  if (st) st.textContent = self.embedBackend === 'webnn-npu' ? 'The memory model runs on the Neural Engine.'
17072
+ : e.data.fallback ? 'The memory model runs on the CPU: ' + e.data.fallback : '';
17073
  });
17074
  return new Promise((resolve, reject) => {
17075
  const handler = (e) => {
 
17299
  // plan/2026-10-29-webnn-embed). Each length is its own session, made on first use. Pooling is the pipeline's: mean
17300
  // over real tokens, then L2 norm.
17301
  const NPU_LENS = [128, 512];
17302
+ // Known answer: the fp32 CPU embedding of the warm-up sentence (4 decimals). A Neural Engine whose vector for it falls
17303
+ // below NPU_MIN_COS falls back to the CPU, so a driver that returns wrong numbers without an error cannot reach memory.
17304
+ // fp16 on the M4 Pro: 0.99997; the CPU's own q8 model: 0.996 (plan/2026-10-29-webnn-embed).
17305
+ const NPU_WARMUP = 'query: warm up', NPU_MIN_COS = 0.995;
17306
+ const NPU_WARMUP_REF = [0.0596,-0.0109,-0.0616,-0.0651,0.0823,-0.0153,-0.0033,0.0104,0.0459,-0.0075,0.0499,0.041,0.0657,-0.0456,-0.0182,0.0161,0.0763,-0.0048,-0.0411,-0.0631,0.0148,-0.0188,-0.018,0.0091,0.0422,0.0106,-0.0098,0.0219,0.0841,-0.0426,-0.0343,-0.0377,0.1199,-0.057,0.0703,0.0261,-0.008,-0.0682,0.0482,-0.0466,-0.0015,0.0308,0.0238,0.073,0.0903,0.062,0.0194,0.0276,-0.0267,-0.0486,-0.0759,0.0701,0.013,0.0756,0.0485,-0.0676,-0.072,-0.1271,-0.0811,0.0737,0.0809,-0.004,0.0163,0.0062,0.1011,0.0524,0.0254,0.0024,-0.0925,0.0076,-0.0574,0.0535,-0.0042,-0.0722,0.0212,0.0389,-0.0051,-0.054,0.0516,-0.0685,-0.0588,0.0088,-0.0165,0.0518,-0.0472,0.0529,0.0308,-0.1036,0.0549,-0.0533,0.0373,0.0088,-0.0679,-0.0472,-0.0671,-0.0269,-0.0334,0.0343,0.0248,-0.083,0.0827,-0.0475,0.0408,-0.1019,-0.0504,0.0471,0.0095,-0.0782,0.0493,-0.0542,-0.0408,0.0196,0.0378,0.058,-0.0642,-0.0235,-0.0495,-0.0192,0.0286,-0.0371,0.0918,-0.025,-0.0581,-0.0745,-0.0713,-0.0451,-0.0303,0.0227,0.0136,0.0492,0.0407,0.0617,0.0289,-0.0167,0.0659,0.0847,-0.0713,0.004,0.0031,-0.0417,-0.059,0.0681,-0.03,0.0912,0.0828,0.0621,0.1008,-0.0059,0.1104,-0.0121,0.0529,-0.0581,0.0463,0.0138,0.0701,-0.0386,-0.0809,-0.0363,0.0467,0.0065,-0.0859,-0.0788,-0.0913,0.0006,-0.035,-0.0718,0.0288,0.0475,-0.0475,-0.0283,-0.0516,0.0467,-0.0255,0.0194,-0.0054,0.0606,-0.0335,0.0604,0.0353,0.0251,-0.0156,0.0117,-0.0783,-0.0682,-0.056,-0.0302,-0.0529,0.0357,0.0315,-0.0844,0.0313,0.0326,-0.0103,-0.0735,-0.0502,0.0207,-0.0705,0.0492,0.0101,0.0382,-0.0424,-0.0349,0.0544,0.0106,0.0345,0.0458,-0.0258,0.0362,-0.0145,0.0106,0.0868,-0.1099,-0.0839,0.0558,-0.0767,-0.0194,-0.0078,0.0971,-0.0362,0.0181,0.0433,-0.0017,0.0167,-0.0605,-0.0562,0.0166,-0.0113,-0.0824,-0.054,0.0447,-0.0218,-0.0328,-0.0415,-0.0203,-0.036,-0.0499,0.0049,0.0604,0.0493,-0.0112,-0.0115,-0.0722,0.0511,-0.0521,0.0264,-0.0726,-0.0305,-0.0118,-0.0228,0.0123,0.0475,-0.0537,-0.0038,-0.0109,-0.0189,0.0149,0.0571,0.0763,-0.0415,0.0333,0.0301,0.0085,0.0907,0.09,0.0371,0.0387,-0.0303,-0.0153,-0.073,-0.0686,-0.0919,0.016,0.0143,0.0045,-0.0767,-0.0634,0.0671,0.0829,-0.0137,-0.0336,-0.0034,0.055,0.0595,0.0804,0.0496,0.0197,-0.0592,0.0566,-0.0626,-0.0412,-0.0543,-0.0135,0.0494,-0.0364,0.0837,0.0468,-0.0239,0.0459,-0.0213,0.0847,0.0087,-0.0316,0.0268,0.0451,-0.0504,0.0321,-0.018,0.0865,0.0148,0.0218,0.036,0.0471,-0.0659,-0.0439,0.0373,0.0481,0.0112,0.0586,-0.058,-0.0694,-0.0175,-0.0476,-0.0428,-0.0694,0.0504,0.0453,-0.1057,-0.0675,-0.0058,-0.0298,0.0683,-0.0555,-0.0728,0.0287,-0.0308,-0.0047,-0.0144,0.0331,-0.0401,-0.0362,0.0357,0.0688,-0.0393,0.0679,-0.0009,-0.0288,0.0592,-0.0443,-0.0633,0.0526,0.0386,-0.0907,0.0289,0.0431,0.0031,0.0142,-0.0474,-0.0444,0.0636,0.0463,-0.0805,-0.0269,0.0202,0.0208,0.0508,0.011,0.0038,-0.0071,0.0275,-0.0355,0.0664,0.0501,-0.0683,0.014,-0.057,-0.0488,-0.0461,0.0618,-0.056,-0.062,0.0779,0.064,0.0436,0.0365];
17307
  let npuOn = false, npuTokenizer = null;
17308
  const npuModels = {};
17309
  async function npuSession(len) {
 
17353
  try {
17354
  npuTokenizer = await AutoTokenizer.from_pretrained('Xenova/multilingual-e5-small');
17355
  npuOn = true;
17356
+ const w = await npuEmbed(NPU_WARMUP);
17357
+ const cos = w.reduce((s, x, d) => s + x * NPU_WARMUP_REF[d], 0);
17358
+ if (!(cos >= NPU_MIN_COS)) throw new Error('its vector disagrees with the CPU reference (cosine ' + cos.toFixed(4) + ')');
17359
  } catch (err) {
17360
  npuOn = false;
17361
  fallback = 'the Neural Engine path failed to load (' + (err.message || String(err)).slice(0, 160) + ').';
17362
  }
 
 
17363
  }
17364
  if (!npuOn) await loadWasmEmbedder();
17365
  self.postMessage({ type: 'ready', backend: npuOn ? 'webnn-npu' : 'wasm', fallback });