Feature Extraction
sentence-transformers
ONNX
nomic_bert
sentence-similarity
code-search
on-device
custom_code
text-embeddings-inference
Instructions to use KingLLM/nomic-codesearch-onnx with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use KingLLM/nomic-codesearch-onnx with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("KingLLM/nomic-codesearch-onnx", trust_remote_code=True) sentences = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium." ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [3, 3] - Notebooks
- Google Colab
- Kaggle
Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
tags:
|
| 4 |
+
- sentence-transformers
|
| 5 |
+
- feature-extraction
|
| 6 |
+
- onnx
|
| 7 |
+
- sentence-similarity
|
| 8 |
+
- code-search
|
| 9 |
+
- on-device
|
| 10 |
+
pipeline_tag: feature-extraction
|
| 11 |
+
library_name: sentence-transformers
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# nomic-codesearch-onnx (INT8 Quantized)
|
| 15 |
+
|
| 16 |
+
This model is a fine-tuned version of `nomic-ai/nomic-embed-text-v1.5` trained specifically for **semantic code search** on Python code snippets, then exported to ONNX and dynamically quantized to INT8 for efficient on-device execution (CPU/Mobile).
|
| 17 |
+
|
| 18 |
+
The final quantized model is compressed from **530 MB to 100 MB** (a ~5x reduction) while maintaining high retrieval performance, making it perfect for on-device deployment on Android, iOS, or other resource-constrained environments.
|
| 19 |
+
|
| 20 |
+
---
|
| 21 |
+
|
| 22 |
+
## Model Details
|
| 23 |
+
|
| 24 |
+
- **Base Model:** `nomic-ai/nomic-embed-text-v1.5` (137M parameters, 768-dimensional embeddings)
|
| 25 |
+
- **Fine-Tuning Dataset:** `code-search-net/code_search_net` (Python split). Trained on 50,000 positive `(docstring, function)` pairs using Multiple Negatives Ranking Loss (MNR).
|
| 26 |
+
- **Training Acceleration:** Apple Silicon (M4 MPS)
|
| 27 |
+
- **Export Format:** ONNX (Opset 17)
|
| 28 |
+
- **Quantization:** Dynamic INT8 Quantization (weights quantized to `QInt8`, activation optimized)
|
| 29 |
+
- **Dimensions:** 768 (supports Matryoshka Representation Learning down to 256 dimensions)
|
| 30 |
+
|
| 31 |
+
---
|
| 32 |
+
|
| 33 |
+
## Metrics
|
| 34 |
+
|
| 35 |
+
| Config | Size | Mean Cosine Drift | NDCG@10 (Code Search) |
|
| 36 |
+
|---|---|---|---|
|
| 37 |
+
| Baseline Model | 530 MB | 0.0 | ~0.48 |
|
| 38 |
+
| Fine-Tuned FP32 ONNX | 530 MB | 0.0 | **~0.71** |
|
| 39 |
+
| Fine-Tuned INT8 ONNX | 100 MB | ~0.07 | ~0.68 |
|
| 40 |
+
|
| 41 |
+
---
|
| 42 |
+
|
| 43 |
+
## Python Quickstart
|
| 44 |
+
|
| 45 |
+
To run semantic code search or generate embeddings locally using this ONNX model:
|
| 46 |
+
|
| 47 |
+
### 1. Install Dependencies
|
| 48 |
+
```bash
|
| 49 |
+
pip install onnxruntime transformers numpy
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
### 2. Run Inference
|
| 53 |
+
```python
|
| 54 |
+
import os
|
| 55 |
+
import numpy as np
|
| 56 |
+
import onnxruntime as ort
|
| 57 |
+
from transformers import AutoTokenizer
|
| 58 |
+
|
| 59 |
+
# Load tokenizer and ONNX session
|
| 60 |
+
# Ensure config.json, tokenizer.json, vocab.txt, etc., are in the same directory
|
| 61 |
+
model_dir = "./"
|
| 62 |
+
tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
| 63 |
+
session = ort.InferenceSession(os.path.join(model_dir, "model_int8.onnx"))
|
| 64 |
+
|
| 65 |
+
def embed(texts: list[str], max_length: int = 512) -> np.ndarray:
|
| 66 |
+
"""Return L2-normalised sentence embeddings, shape (len(texts), 768)."""
|
| 67 |
+
encoded = tokenizer(
|
| 68 |
+
texts,
|
| 69 |
+
padding=True,
|
| 70 |
+
truncation=True,
|
| 71 |
+
max_length=max_length,
|
| 72 |
+
return_tensors="np",
|
| 73 |
+
)
|
| 74 |
+
outputs = session.run(
|
| 75 |
+
["sentence_embedding"],
|
| 76 |
+
{
|
| 77 |
+
"input_ids": encoded["input_ids"].astype(np.int64),
|
| 78 |
+
"attention_mask": encoded["attention_mask"].astype(np.int64),
|
| 79 |
+
},
|
| 80 |
+
)
|
| 81 |
+
embeddings = outputs[0] # (batch, 768)
|
| 82 |
+
# L2 normalise so dot-product == cosine similarity
|
| 83 |
+
norms = np.linalg.norm(embeddings, axis=1, keepdims=True)
|
| 84 |
+
return embeddings / np.maximum(norms, 1e-12)
|
| 85 |
+
|
| 86 |
+
# Embed query and snippets
|
| 87 |
+
snippets = [
|
| 88 |
+
"def add(a, b): return a + b",
|
| 89 |
+
"def binary_search(arr, target): ...",
|
| 90 |
+
"SELECT * FROM users WHERE age > 18"
|
| 91 |
+
]
|
| 92 |
+
query = "function that sums two numbers"
|
| 93 |
+
|
| 94 |
+
query_emb = embed([query])
|
| 95 |
+
code_embs = embed(snippets)
|
| 96 |
+
|
| 97 |
+
# Calculate similarity (dot product of L2-normalized embeddings)
|
| 98 |
+
scores = (query_emb @ code_embs.T)[0]
|
| 99 |
+
for idx, score in enumerate(scores):
|
| 100 |
+
print(f"[{score:.4f}] {snippets[idx]}")
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
---
|
| 104 |
+
|
| 105 |
+
## On-Device Deployment (Android)
|
| 106 |
+
|
| 107 |
+
This model has been successfully deployed inside a native Android application using:
|
| 108 |
+
1. **ONNX Runtime Android AAR** (`com.microsoft.onnxruntime:onnxruntime-android`) for CPU inference.
|
| 109 |
+
2. **Custom WordPiece Tokenizer in Kotlin** (`BertTokenizer.kt`) to parse strings directly on-device without JVM-overhead Python dependencies.
|
| 110 |
+
3. **Coroutines-based asynchronous loading** to load the 100 MB model in the background without blocking the UI thread.
|
| 111 |
+
|
| 112 |
+
For complete Android source files (MainActivity, OnnxEmbedder, and BertTokenizer), please refer to the GitHub repository: [CoderOMaster/nomic-codesearch-android](https://github.com/CoderOMaster/nomic-codesearch-android).
|
| 113 |
+
|
| 114 |
+
---
|
| 115 |
+
|
| 116 |
+
## License
|
| 117 |
+
|
| 118 |
+
This project is licensed under the Apache 2.0 License.
|