[Bug]: DefaultEmbeddingFunction reloads the ONNX model on every generate() call
What happened?
DefaultEmbeddingFunction.generate() in @chroma-core/default-embed calls pipeline() from @huggingface/transformers on every call:
public async generate(texts: string[]): Promise<number[][]> {
const pipe = await pipeline("feature-extraction", this.modelName, { ... });
...
}pipeline() keeps no cache. Only the model file is cached on disk. Each call re-reads the ~90 MB ONNX model, rebuilds the tokenizer and creates a new ONNX Runtime session. So every collection.query({ queryTexts }), add() and upsert() pays the full model load, although the actual inference takes only a few ms.
Production impact: real-world numbers, end to end (embedding + collection query), from a 1 vCPU / 1 GB server running two Node API processes:
Before (stock DefaultEmbeddingFunction) |
After (pipeline reused) | |
|---|---|---|
| Memory search per chat message | 600 ms – 1.2 s | 135–222 ms |
| One-time model load at startup | none | 649 ms |
| API process RSS (2 processes) | 405 / 486 MB | 381 / 369 MB † |
† Our deployment also disables the ONNX CPU memory arena (enableCpuMemArena: false), which is not part of the PR, and "after" RSS was taken shortly after a restart. So treat the memory row as indicative only.
Embeddings are bit-identical before and after (max abs diff 0), so existing collections need no re-embedding.
Isolated reproduction: Xenova/all-MiniLM-L6-v2 (fp32) embedding one short sentence, Node 22, same machine:
| Scenario | Time |
|---|---|
DefaultEmbeddingFunction.generate() (current) |
~155–175 ms on every call (first call ~1.1 s) |
| same pipeline created once and reused | ~4–6 ms |
| 4 concurrent calls, current | ~650 ms wall |
| 4 concurrent calls, reused pipeline | ~60 ms wall |
Process RSS also grew by ~150 MB per call during the first few calls (237 MB to 830 MB after 6 calls) before levelling off.
Expected: the pipeline is loaded once and reused, as in the Python client. The Python equivalent, ONNXMiniLM_L6_V2, already does this: tokenizer and model are @cached_property, so the InferenceSession is created once per instance and reused across calls:
Only the JS DefaultEmbeddingFunction rebuilds it on every call.
Workaround: subclass DefaultEmbeddingFunction and override generate() to reuse a module-level pipeline promise.
Fix with tests: #7792
Versions
@chroma-core/default-embed 0.1.9 (latest), @huggingface/transformers 3.8.1, chromadb JS client, Node 22.16, Windows 11 and Ubuntu
Relevant log output
DefaultEmbeddingFunction.generate 0 1099 ms
DefaultEmbeddingFunction.generate 1 153 ms
DefaultEmbeddingFunction.generate 2 163 ms
DefaultEmbeddingFunction.generate 3 174 ms
DefaultEmbeddingFunction.generate 4 162 ms
DefaultEmbeddingFunction.generate 5 177 ms
pipeline create once 156 ms
cached pipe 0 6 ms
cached pipe 1 6 ms
cached pipe 2 5 ms
cached pipe 3 5 ms
cached pipe 4 5 ms
cached pipe 5 4 msSource: chroma-core/chroma