Files
lancedb/nodejs/__test__/transformers.test.ts
T
Divyam TalwarandDivyam Talwar ea2d9a0440 fix(node): exclude padding from Transformers embedding pooling (#5282)
## Summary

Use Transformers.js mean pooling with the tokenizer's attention mask so
padding positions do not contribute to source and query embeddings.

## Validation

- A deterministic model and tokenizer fixture uses actual
Transformers.js Tensor objects and compares the same short text embedded
alone and in a padded batch. On main its vector changes from `[1, 5]` to
`[50.5, 102.5]`. With the fix it stays `[1, 5]` and the longer text's
expected vector remains `[4, 8]`.
- Existing Transformers and embedding Jest suites on Node 24: 20 passed,
1 skipped (OpenAI key required). TypeScript build and Biome checks
passed.
- Used Transformers.js 4.3.2 with the ONNX Web tensor runtime for
fixture-only tests. Model downloads and inference were disabled.

Fixes https://github.com/lancedb/lancedb/issues/5281

---------

Co-authored-by: Divyam Talwar <DivyamTalwar@users.noreply.github.com>
2026-11-30 20:13:20 +08:00

35 lines
1.0 KiB
TypeScript

// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
import { execFileSync } from "node:child_process";
import { resolve } from "node:path";
describe("Transformers embedding tokenizer selection", () => {
// Run init's native ESM import outside Jest's CommonJS sandbox, like a user.
// The fixture mocks model loading and forbids local/remote model access.
test.each([
["default model", 0],
["default tokenizer", 1],
["custom tokenizer", 2],
["tokenizer registry variable", 3],
["tokenizer loading error", 4],
])("handles %s", (_name, testCase) => {
execFileSync(
process.execPath,
[
resolve(__dirname, "fixtures", "transformers_tokenizer.cjs"),
String(testCase),
],
{ stdio: "pipe", timeout: 15_000 },
);
});
});
test("Transformers embeddings ignore padding from longer texts in a batch", () => {
execFileSync(
process.execPath,
[resolve(__dirname, "fixtures", "transformers_pooling.cjs")],
{ stdio: "pipe", timeout: 15_000 },
);
});