Spaces:
Running
Running
File size: 13,787 Bytes
6c3af4e 3bbbb17 6dcf170 3bbbb17 6c3af4e 3bbbb17 6dcf170 6c3af4e 6dcf170 3bbbb17 | 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | // @vitest-environment node
/**
* Exercises the real bi-encoder end to end. Downloads ~460MB on first run, so
* it is excluded from the default suite:
*
* npx vitest run --config vitest.integration.config.ts biEncoder
*/
import { monitorEventLoopDelay } from "node:perf_hooks";
import { Worker } from "node:worker_threads";
import type { Tokenizer } from "@huggingface/tokenizers";
import { type InferenceSession, Tensor } from "onnxruntime-node";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import {
getBiEncoderStatus,
scorePassages,
startBiEncoderService,
stopBiEncoderService,
} from "./biEncoderService";
import {
BATCH_ROWS,
type BiEncoderRequest,
type BiEncoderResponse,
} from "./biEncoderWorkerProtocol";
import { loadOnnxModel } from "./utils/onnxModelLoader";
const MODEL_HF_REPO =
"sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2";
const MODEL_HF_FILE = "onnx/model.onnx";
const MAX_SEQUENCE_LENGTH = 256;
const WORDS =
"alpha bravo charlie delta echo foxtrot golf hotel india juliet kilo lima mike november oscar papa quebec romeo sierra tango uniform victor whiskey xray yankee zulu".split(
" ",
);
/** Deterministic passage of `wordCount` words, distinct per `seed`. */
function makePassage(wordCount: number, seed: number): string {
const words: string[] = [];
for (let i = 0; i < wordCount; i++) {
words.push(WORDS[(i * 7 + seed * 13) % WORDS.length]);
}
return words.join(" ");
}
/**
* The per-text path this change replaced: one `session.run()` per text,
* `[1, L]` tensors, mean pooling over the tokenizer's own mask. Kept here as
* the reference the batched path must match.
*/
async function encodePerText(
refSession: InferenceSession,
refTokenizer: Tokenizer,
text: string,
): Promise<Float32Array> {
const { ids, attention_mask } = refTokenizer.encode(text);
const truncatedIds = ids.slice(0, MAX_SEQUENCE_LENGTH);
const truncatedMask = attention_mask.slice(0, MAX_SEQUENCE_LENGTH);
const length = truncatedIds.length;
const dimensions = [1, length];
const { last_hidden_state } = await refSession.run({
input_ids: new Tensor(
"int64",
BigInt64Array.from(truncatedIds, BigInt),
dimensions,
),
attention_mask: new Tensor(
"int64",
BigInt64Array.from(truncatedMask, BigInt),
dimensions,
),
token_type_ids: new Tensor("int64", new BigInt64Array(length), dimensions),
});
const embedding = last_hidden_state.data as Float32Array;
const dim = last_hidden_state.dims[2];
const pooled = new Float32Array(dim);
let count = 0;
for (let t = 0; t < length; t++) {
if (truncatedMask[t] === 0) continue;
const offset = t * dim;
for (let d = 0; d < dim; d++) {
pooled[d] += embedding[offset + d];
}
count++;
}
if (count > 0) {
for (let d = 0; d < dim; d++) {
pooled[d] /= count;
}
}
let norm = 0;
for (let d = 0; d < dim; d++) {
norm += pooled[d] * pooled[d];
}
norm = Math.sqrt(norm);
if (norm > 0) {
for (let d = 0; d < dim; d++) {
pooled[d] /= norm;
}
}
return pooled;
}
function dot(a: Float32Array, b: Float32Array): number {
let sum = 0;
for (let d = 0; d < a.length; d++) {
sum += a[d] * b[d];
}
return sum;
}
describe("biEncoderService", () => {
beforeAll(async () => {
await startBiEncoderService();
});
afterAll(async () => {
await stopBiEncoderService();
});
it("reports itself ready", async () => {
expect(await getBiEncoderStatus()).toBe(true);
});
it("scores a relevant passage above an unrelated one", async () => {
const [relevant, unrelated] = await scorePassages(
"how to bake sourdough bread at home",
[
"Mix the starter with flour and water, let the dough rise overnight, then bake it in a hot Dutch oven.",
"Antarctica has no permanent residents and its ice sheet holds most of the planet's fresh water.",
],
);
expect(relevant).toBeGreaterThan(unrelated);
expect(relevant).toBeGreaterThan(0.3);
});
it("matches across languages", async () => {
const [portuguese, unrelated] = await scorePassages(
"what is the capital of France",
[
"A capital da França é Paris, situada às margens do rio Sena.",
"Bubble sort repeatedly swaps adjacent elements until the list is ordered.",
],
);
expect(portuguese).toBeGreaterThan(unrelated);
});
it("returns one score per passage", async () => {
const scores = await scorePassages("query", ["one", "two", "three"]);
expect(scores).toHaveLength(3);
for (const score of scores) {
expect(score).toBeGreaterThanOrEqual(-1.001);
expect(score).toBeLessThanOrEqual(1.001);
}
});
});
describe("biEncoderService batched scoring (#2715)", () => {
let refSession: InferenceSession;
let refTokenizer: Tokenizer;
beforeAll(async () => {
// The first describe's afterAll stopped the service; these tests need it
// running again, alongside a second session used as the per-text
// reference.
await startBiEncoderService();
const loaded = await loadOnnxModel(MODEL_HF_REPO, MODEL_HF_FILE);
refSession = loaded.session;
refTokenizer = loaded.tokenizer;
});
afterAll(async () => {
await stopBiEncoderService();
await refSession?.release();
});
it("keeps batched scores within 1e-5 of the per-text path", async () => {
const wordCounts = [
3, 8, 15, 40, 90, 150, 250, 400, 5, 20, 60, 120, 200, 300, 12, 35, 75,
175, 350, 7, 45, 110, 230, 9,
];
const passages = wordCounts.map((n, i) => makePassage(n, i + 100));
const query = "alpha bravo charlie delta echo";
const batchedScores = await scorePassages(query, passages);
const queryEmbedding = await encodePerText(refSession, refTokenizer, query);
let maxAbsDiff = 0;
for (let i = 0; i < passages.length; i++) {
const passageEmbedding = await encodePerText(
refSession,
refTokenizer,
passages[i],
);
const referenceScore = dot(queryEmbedding, passageEmbedding);
maxAbsDiff = Math.max(
maxAbsDiff,
Math.abs(batchedScores[i] - referenceScore),
);
expect(Math.abs(batchedScores[i] - referenceScore)).toBeLessThan(1e-5);
}
// Sanity: the fixture actually exercised the comparison rather than
// matching an all-zero result — real magnitude and real spread. An
// all-zero or all-equal score vector fails here.
const maxAbsScore = Math.max(...batchedScores.map((s) => Math.abs(s)));
expect(maxAbsScore).toBeGreaterThan(0.1);
expect(new Set(batchedScores).size).toBeGreaterThan(1);
expect(maxAbsDiff).toBeLessThan(1e-5);
});
it("returns scores in the original passage order, not the length-sorted order", async () => {
// Deliberately out of length order: long, short, medium, short, long...
const passages = [
makePassage(220, 1),
makePassage(4, 2),
makePassage(60, 3),
makePassage(6, 4),
makePassage(180, 5),
makePassage(40, 6),
makePassage(5, 7),
makePassage(140, 8),
makePassage(3, 9),
makePassage(300, 10),
];
const query = "whiskey xray yankee zulu";
const batchedScores = await scorePassages(query, passages);
const queryEmbedding = await encodePerText(refSession, refTokenizer, query);
for (let i = 0; i < passages.length; i++) {
const passageEmbedding = await encodePerText(
refSession,
refTokenizer,
passages[i],
);
const referenceScore = dot(queryEmbedding, passageEmbedding);
// A permutation of the length-sorted order would miss by far more
// than the padding tolerance.
expect(Math.abs(batchedScores[i] - referenceScore)).toBeLessThan(1e-5);
}
// The fixture is non-monotonic in length, so the sorted order differs
// from the input order and the check above is a real order test.
const lengths = passages.map((p) => p.length);
const isSorted = lengths.every(
(len, i) => i === 0 || lengths[i - 1] <= len,
);
expect(isSorted).toBe(false);
});
});
/**
* Runs one scoring request against a worker of its own and returns its reply.
*
* The session lives in the worker now, so a `session.run` spy on this thread
* would see nothing. The worker reports the dims of each forward pass instead,
* which is what the bucketing assertions read.
*/
async function scoreInOwnWorker(
query: string,
passages: string[],
): Promise<Extract<BiEncoderResponse, { type: "scores" }>> {
const worker = new Worker(new URL("./biEncoderWorker.ts", import.meta.url));
try {
await new Promise<void>((resolve, reject) => {
worker.once("message", (message: BiEncoderResponse) => {
if (message.type === "ready") resolve();
else reject(new Error(`Expected "ready", got "${message.type}"`));
});
worker.once("error", reject);
});
return await new Promise((resolve, reject) => {
worker.once("message", (message: BiEncoderResponse) => {
if (message.type === "scores") resolve(message);
else if (message.type === "failed") reject(new Error(message.message));
});
worker.once("error", reject);
const request: BiEncoderRequest = {
type: "score",
id: 1,
query,
passages,
};
worker.postMessage(request);
});
} finally {
await worker.terminate();
}
}
describe("biEncoderWorker bucketing (#2715, #2730)", () => {
it("runs one forward pass per length bucket, not one per passage", async () => {
// Zigzag lengths with the sort's guarantee removed: bucket 1's last
// row (190 words) is longer than bucket 2's last row (25 words), so
// an unsorted pass sets bucket widths from those rows and they come
// out decreasing — the non-decreasing-width assertion below then
// fails. With the sort, widths are non-decreasing by construction.
const wordCounts = [
200, 10, 150, 5, 180, 20, 120, 8, 90, 160, 30, 110, 60, 140, 190, 170, 15,
130, 70, 25,
];
const passages = wordCounts.map((n, i) => makePassage(n, i));
const { scores, runDimensions } = await scoreInOwnWorker(
"how do alpha bravo passages score",
passages,
);
expect(scores).toHaveLength(passages.length);
// The query rides in the batch, so 21 rows go through ceil(21 / B)
// bucketed runs instead of 21 per-passage runs.
const expectedBuckets = Math.ceil((passages.length + 1) / BATCH_ROWS);
expect(runDimensions.length).toBe(expectedBuckets);
expect(runDimensions.length).toBeLessThan(passages.length + 1);
let totalRows = 0;
for (const dims of runDimensions) {
expect(dims.length).toBe(2);
expect(dims[0]).toBeLessThanOrEqual(BATCH_ROWS);
totalRows += dims[0];
}
expect(totalRows).toBe(passages.length + 1);
// Every non-final bucket is full, so it really carries multiple rows.
// Only the final bucket may be short — with BATCH_ROWS of 10 or 20 a
// 21-row pool leaves it exactly one row, so the >1 check must not
// depend on the tuning knob.
for (let i = 0; i < runDimensions.length - 1; i++) {
expect(runDimensions[i][0]).toBeGreaterThan(1);
}
// Bucket widths are non-decreasing across runs: the sort by token
// length put the short rows first. Delete the `.sort()` in encodeBatch
// and this fails.
for (let i = 1; i < runDimensions.length; i++) {
expect(runDimensions[i][1]).toBeGreaterThanOrEqual(
runDimensions[i - 1][1],
);
}
});
});
describe("biEncoderService main-thread cost (#2730)", () => {
beforeAll(async () => {
await startBiEncoderService();
});
afterAll(async () => {
await stopBiEncoderService();
});
/**
* The acceptance measurement from #2730. On the main thread the same pass
* blocked for ~815 ms (x86_64, 32 logical cores); with the session in the
* worker it measures ~1.5 ms. The bar is left at the issue's 20 ms so the
* test reports a regression rather than host-to-host noise.
*/
it("keeps the main-thread event-loop block under 20 ms for a 200-passage pass", async () => {
const passages = Array.from({ length: 200 }, (_, i) =>
makePassage(5 + ((i * 37) % 300), i),
);
const query = "how do alpha bravo passages score";
// One untimed pass, so the worker's lazily allocated arenas are not
// charged to the measurement.
await scorePassages(query, passages.slice(0, 20));
const histogram = monitorEventLoopDelay({ resolution: 1 });
histogram.enable();
const scores = await scorePassages(query, passages);
histogram.disable();
expect(scores).toHaveLength(passages.length);
expect(histogram.max / 1e6).toBeLessThan(20);
});
});
describe("biEncoderService when the worker goes away (#2730)", () => {
it("answers in-flight scoring with empty scores and reports not ready", async () => {
await startBiEncoderService();
expect(await getBiEncoderStatus()).toBe(true);
const passages = Array.from({ length: 200 }, (_, i) =>
makePassage(5 + ((i * 37) % 300), i),
);
// Fired but not awaited: the worker is taken away underneath it. An
// unanswered request would hang the page-content read that made it, so
// it has to come back empty, which is the signal to rank lexically.
const pending = scorePassages(
"how do alpha bravo passages score",
passages,
);
await stopBiEncoderService();
await expect(pending).resolves.toEqual([]);
expect(await getBiEncoderStatus()).toBe(false);
expect(await scorePassages("query", ["one"])).toEqual([]);
});
});
|