WrenAI icon indicating copy to clipboard operation
WrenAI copied to clipboard

feat(wren memory): torch-free ONNX embedding backend to cut install size and cold start

Open goldmedal opened this issue 3 months ago • 1 comments

Problem

wrenai[memory] is currently ["lancedb>=0.6", "sentence-transformers>=3.0.0"]. That pulls in torch, and on linux-x86_64 the default index resolves the CUDA build: torch plus the whole nvidia-* stack, roughly 2.7 GB of wheels and 6–7 GB installed, where a CPU-only build is about 200 MB. It also imposes a per-process floor before the first result: importing torch and loading the model weights, which is the dominant cost in #2611.

A CPU-only pin is not available to us: an extra cannot pin a package index, so wrenai[memory] cannot express "torch, but the CPU wheel". Every user on Linux pays the CUDA download unless they know to reinstall torch from the PyTorch CPU index afterwards. Telling users to do that by hand is a workaround, not a fix.

Proposal

Add a torch-free embedding backend based on onnxruntime, selectable at install and runtime, and keep sentence-transformers as the existing option.

  • New extra, e.g. wrenai[memory-onnx]: lancedb + onnxruntime + tokenizer, no torch.
  • Runtime selection via an env var, e.g. WREN_EMBEDDING_BACKEND=onnx|sentence-transformers, defaulting to whichever is installed (ONNX preferred when both are).
  • Slot it in behind the existing embedding abstraction in core/wren/src/wren/memory/embeddings.py so MemoryStore is unchanged: the backend only has to provide compute_source_embeddings / compute_query_embeddings.
  • Keep the default model's 384 dimensions and multilingual coverage (paraphrase-multilingual-MiniLM-L12-v2 equivalent). Existing LanceDB tables are typed with a fixed-size 384 vector, so preserving the dimension means existing indexes keep working; a different dimension forces every user to wren memory reset && wren memory index.

What the evaluation needs to answer

  • Cold-start time to first embedding, compared with the current sentence-transformers path.
  • Install footprint of the ONNX extra vs. the current one, on linux-x86_64 specifically (where the CUDA resolution happens).
  • Retrieval parity: the same set of fetch / recall queries should return comparable results — this must not quietly degrade Chinese/multilingual retrieval quality.
  • Where the ONNX weights come from and how they are cached offline (the local-first resolution behaviour must be preserved).
  • Platform coverage: linux/macOS/Windows, x86_64 and arm64.

Acceptance criteria

  • pip install wrenai[memory-onnx] installs no torch and no nvidia-* packages.
  • wren memory index / fetch / recall work end-to-end on the ONNX backend, at 384 dimensions, against an index built by either backend.
  • Model resolution stays local-first, so a cached model needs no network access.
  • Backend selection is explicit and reported by wren memory status.
  • The evaluation numbers above are recorded on this issue before the default is changed.

Context

Follow-up from #2611. This is the item that removes both the multi-GB install and the per-process load floor, rather than working around them.

goldmedal avatar Aug 05 '26 04:08 goldmedal

Opened #2707 for this.

Short version of the approach: the ONNX export already lives in the same HF repo as the torch weights, so there is nothing to convert — the backend just reproduces the sentence-transformers pipeline (tokenize to max_seq_length, encode, attention-masked mean pooling, L2 normalize) through onnxruntime. Vectors come out equal to the sentence-transformers backend to float32 rounding (worst-case cosine 1.00000000, max delta 6.11e-07, checked across English, Traditional Chinese, Japanese and Korean), so existing LanceDB tables need no reindex.

On the numbers the issue asked for: cold start to first embedding drops from 20.3 s to 3.0 s, installed size from 817 MB to 133 MB on macOS arm64, and the linux-x86_64 resolution loses torch, triton and all sixteen nvidia-* packages.

Two questions on scope, since CONTRIBUTING asks to align on larger changes and I have already written this:

  • The PR also regates the store/WrenMemory test suites on "lancedb plus some embedding backend" instead of sentence-transformers specifically, and adds a CI job that installs memory-onnx and asserts torch is absent. Both are separable — say the word and I will split them into a follow-up.
  • Two other edits are not separable, and I have flagged them in the PR as such: _extra_available() had to stop testing for sentence_transformers by name (otherwise a memory-onnx install silently downgrades to Grep and the extra does nothing), and three sentence-transformers-specific tests had to stop going through get_embedding_function, which now dispatches.

Happy to reshape any of it.

audi0417 avatar Aug 28 '26 12:08 audi0417