forked from ruvnet/RuVector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel-embedder.mjs
More file actions
168 lines (145 loc) · 5.25 KB
/
parallel-embedder.mjs
File metadata and controls
168 lines (145 loc) · 5.25 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Parallel ONNX Embedder using Worker Threads
*
* Distributes embedding work across multiple CPU cores for true parallelism.
*/
import { Worker } from 'worker_threads';
import { cpus } from 'os';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { ModelLoader, DEFAULT_MODEL } from './loader.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
export class ParallelEmbedder {
constructor(options = {}) {
this.numWorkers = options.numWorkers || Math.max(1, cpus().length - 1);
this.workers = [];
this.readyWorkers = [];
this.pendingRequests = new Map();
this.requestId = 0;
this.initialized = false;
this.modelData = null;
}
/**
* Initialize the parallel embedder with a model
*/
async init(modelName = DEFAULT_MODEL) {
if (this.initialized) return;
console.log(`🚀 Initializing ${this.numWorkers} worker threads...`);
// Load model once in main thread
const loader = new ModelLoader({ cache: false });
const { modelBytes, tokenizerJson, config } = await loader.loadModel(modelName);
// Store as transferable data
this.modelData = {
modelBytes: Array.from(modelBytes), // Convert to regular array for transfer
tokenizerJson,
config
};
this.dimension = config.dimension;
// Spawn workers
const workerPromises = [];
for (let i = 0; i < this.numWorkers; i++) {
workerPromises.push(this._spawnWorker(i));
}
await Promise.all(workerPromises);
this.initialized = true;
console.log(`✅ ${this.numWorkers} workers ready`);
}
async _spawnWorker(index) {
return new Promise((resolve, reject) => {
const worker = new Worker(join(__dirname, 'parallel-worker.mjs'), {
workerData: this.modelData
});
worker.on('message', (msg) => {
if (msg.type === 'ready') {
this.readyWorkers.push(worker);
resolve();
} else if (msg.type === 'result') {
const { id, embeddings } = msg;
const pending = this.pendingRequests.get(id);
if (pending) {
pending.resolve(embeddings);
this.pendingRequests.delete(id);
this.readyWorkers.push(worker);
this._processQueue();
}
} else if (msg.type === 'error') {
const { id, error } = msg;
const pending = this.pendingRequests.get(id);
if (pending) {
pending.reject(new Error(error));
this.pendingRequests.delete(id);
this.readyWorkers.push(worker);
this._processQueue();
}
}
});
worker.on('error', reject);
this.workers.push(worker);
});
}
_processQueue() {
// Process any queued requests when workers become available
}
/**
* Embed texts in parallel across worker threads
*/
async embedBatch(texts) {
if (!this.initialized) {
throw new Error('ParallelEmbedder not initialized. Call init() first.');
}
// Split texts into chunks for each worker
const chunkSize = Math.ceil(texts.length / this.numWorkers);
const chunks = [];
for (let i = 0; i < texts.length; i += chunkSize) {
chunks.push(texts.slice(i, i + chunkSize));
}
// Send to workers in parallel
const promises = chunks.map((chunk, i) => {
return new Promise((resolve, reject) => {
const id = this.requestId++;
const worker = this.readyWorkers.shift() || this.workers[i % this.workers.length];
this.pendingRequests.set(id, { resolve, reject });
worker.postMessage({ type: 'embed', id, texts: chunk });
});
});
// Wait for all results
const results = await Promise.all(promises);
// Flatten results
return results.flat();
}
/**
* Embed a single text (uses one worker)
*/
async embedOne(text) {
const results = await this.embedBatch([text]);
return new Float32Array(results[0]);
}
/**
* Compute similarity between two texts
*/
async similarity(text1, text2) {
const [emb1, emb2] = await this.embedBatch([text1, text2]);
return this._cosineSimilarity(emb1, emb2);
}
_cosineSimilarity(a, b) {
let dot = 0, normA = 0, normB = 0;
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i];
normA += a[i] * a[i];
normB += b[i] * b[i];
}
return dot / (Math.sqrt(normA) * Math.sqrt(normB));
}
/**
* Shutdown all workers
*/
async shutdown() {
for (const worker of this.workers) {
worker.postMessage({ type: 'shutdown' });
}
this.workers = [];
this.readyWorkers = [];
this.initialized = false;
}
}
export default ParallelEmbedder;