perf: switch to fastembed ONNX backend + user-selectable model/backend#29
Merged
perf: switch to fastembed ONNX backend + user-selectable model/backend#29
Conversation
…kend - Replace sentence-transformers module-level import with dual-backend lazy loading system (fastembed ONNX default, torch opt-in) - Startup time: ~6.6s → ~1.25s (5× improvement) - ONNX model load: ~100ms vs ~2-3s for PyTorch on first embed() call - Register isuruwijesiri/all-MiniLM-L6-v2-code-search-512 as custom fastembed model via TextEmbedding.add_custom_model() with ONNX files from HuggingFace - Add VECGREP_BACKEND env var (onnx|torch) for backend selection - Add VECGREP_MODEL env var for custom HuggingFace model selection - Add fastembed>=0.4.0 to runtime dependencies - Update tests to cover torch backend and all device detection paths - Document new env vars in README Closes #28 Closes #27
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request
Type of Change
Description
The MCP server was slow to start (~6.6s) because
sentence_transformers(PyTorch) was imported at module level. This delayed every Claude Code session on the first tool call.This PR replaces the default embedding backend with fastembed (ONNX Runtime), bringing startup from ~6.6s down to ~1.25s. It also adds user-selectable backend and model via environment variables.
Related Issues / PRs
Closes #27
Closes #28
Changes Made
src/vecgrep/embedder.py— full rewrite with dual-backend lazy loading:VECGREP_BACKEND=onnx(default): fastembed + ONNX Runtime, ~100ms model loadVECGREP_BACKEND=torch: sentence-transformers + PyTorch, supports any HF modelVECGREP_MODEL: override the default HuggingFace modelembed()callisuruwijesiri/all-MiniLM-L6-v2-code-search-512as a custom fastembed ONNX modelpyproject.toml— addfastembed>=0.4.0runtime dependencytests/test_embedder.py— addTestTorchBackendclass + fixTestDetectDevicefor lazy import patternREADME.md— add Configuration section documentingVECGREP_BACKENDandVECGREP_MODELTesting
All 110 existing tests pass. New tests added:
TestTorchBackend: validates shape (1, 384) and unit-norm vectors viaVECGREP_BACKEND=torchTestDetectDevice: covers cuda/mps/cpu paths by patchingtorch.cuda.is_availableandtorch.backends.mps.is_availabledirectlyStartup benchmark:
Checklist
ruffpasses)