File size: 3,138 Bytes
d18636b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Web Worker: carga SigLIP 2 (torres de visión y de texto por separado) y calcula embeddings. Nada sale del navegador.
import { AutoTokenizer, AutoProcessor, SiglipTextModel, SiglipVisionModel, RawImage, env } from '@huggingface/transformers';
import { MODEL_ID, MODEL_REVISION, VISION_DTYPE, TEXT_DTYPE, TEXT_MAX_TOKENS } from './config.js';

env.allowLocalModels = false;
env.allowRemoteModels = true;
env.useBrowserCache = true;

let tok, proc, textModel, visionModel, device;

function configureWasm(wasmBase, wantWebGPU) {
  const suffix = wantWebGPU ? '.asyncify' : '';
  env.backends.onnx.wasm.wasmPaths = { mjs: `${wasmBase}ort-wasm-simd-threaded${suffix}.mjs`, wasm: `${wasmBase}ort-wasm-simd-threaded${suffix}.wasm` };
  const cores = self.navigator?.hardwareConcurrency || 2;
  env.backends.onnx.wasm.numThreads = self.crossOriginIsolated ? Math.min(4, cores) : 1;
}

async function load({ wantWebGPU, wasmBase }) {
  configureWasm(wasmBase, wantWebGPU);
  const progress_callback = (p) => self.postMessage({ type: 'progress', data: { status: p.status, file: p.file, loaded: p.loaded, total: p.total } });
  const opts = { revision: MODEL_REVISION, progress_callback };
  tok = await AutoTokenizer.from_pretrained(MODEL_ID, opts);
  proc = await AutoProcessor.from_pretrained(MODEL_ID, opts);
  const dev = wantWebGPU ? 'webgpu' : 'wasm';
  try {
    visionModel = await SiglipVisionModel.from_pretrained(MODEL_ID, { ...opts, dtype: VISION_DTYPE, device: dev });
    textModel = await SiglipTextModel.from_pretrained(MODEL_ID, { ...opts, dtype: TEXT_DTYPE, device: dev });
  } catch (e) {
    if (wantWebGPU) { self.postMessage({ type: 'webgpu-failed', reason: String(e?.message || e).slice(0, 200) }); return; }
    throw e;
  }
  device = dev;
  self.postMessage({ type: 'ready', device, threads: env.backends.onnx.wasm.numThreads });
}

function norm(arr) { let s = 0; for (const x of arr) s += x * x; s = Math.sqrt(s) || 1; return Float32Array.from(arr, (x) => x / s); }

async function embedImage({ id, width, height, buffer }) {
  const image = new RawImage(new Uint8ClampedArray(buffer), width, height, 4).rgb();
  const t0 = performance.now();
  const inputs = await proc(image);
  const out = await visionModel(inputs);
  const vec = norm(out.pooler_output.data);
  self.postMessage({ type: 'vector', id, vec, ms: performance.now() - t0, device }, [vec.buffer]);
}

async function embedText({ id, text }) {
  const t0 = performance.now();
  const inputs = tok([text], { padding: 'max_length', max_length: TEXT_MAX_TOKENS, truncation: true });
  const out = await textModel(inputs);
  const vec = norm(out.pooler_output.data);
  self.postMessage({ type: 'vector', id, vec, ms: performance.now() - t0, device }, [vec.buffer]);
}

self.addEventListener('message', async (ev) => {
  const msg = ev.data || {};
  try {
    if (msg.type === 'load') await load(msg);
    else if (msg.type === 'image') await embedImage(msg);
    else if (msg.type === 'text') await embedText(msg);
  } catch (e) {
    self.postMessage({ type: 'error', id: msg.id, stage: msg.type, message: String(e?.message || e).slice(0, 400) });
  }
});