Views
No views yet
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).nomic-ai/nomic-embed-text-v1.5 (137M parameters, 768-dimensional embeddings)code-search-net/code_search_net (Python split). Trained on 50,000 positive (docstring, function) pairs using Multiple Negatives Ranking Loss (MNR).QInt8, activation optimized)| Config | Size | Mean Cosine Drift | NDCG@10 (Code Search) |
|---|---|---|---|
| Baseline Model | 530 MB | 0.0 | ~0.48 |
| Fine-Tuned FP32 ONNX | 530 MB | 0.0 | ~0.71 |
| Fine-Tuned INT8 ONNX | 100 MB | ~0.07 | ~0.68 |
pip install onnxruntime transformers numpy1import os
2import numpy as np
3import onnxruntime as ort
4from transformers import AutoTokenizer
5
6# Load tokenizer and ONNX session
7# Ensure config.json, tokenizer.json, vocab.txt, etc., are in the same directory
8model_dir = "./"
9tokenizer = AutoTokenizer.from_pretrained(model_dir)
10session = ort.InferenceSession(os.path.join(model_dir, "model_int8.onnx"))
11
12def embed(texts: list[str], max_length: int = 512) -> np.ndarray:
13 """Return L2-normalised sentence embeddings, shape (len(texts), 768)."""
14 encoded = tokenizer(
15 texts,
16 padding=True,
17 truncation=True,
18 max_length=max_length,
19 return_tensors="np",
20 )
21 outputs = session.run(
22 ["sentence_embedding"],
23 {
24 "input_ids": encoded["input_ids"].astype(np.int64),
25 "attention_mask": encoded["attention_mask"].astype(np.int64),
26 },
27 )
28 embeddings = outputs[0] # (batch, 768)
29 # L2 normalise so dot-product == cosine similarity
30 norms = np.linalg.norm(embeddings, axis=1, keepdims=True)
31 return embeddings / np.maximum(norms, 1e-12)
32
33# Embed query and snippets
34snippets = [
35 "def add(a, b): return a + b",
36 "def binary_search(arr, target): ...",
37 "SELECT * FROM users WHERE age > 18"
38]
39query = "function that sums two numbers"
40
41query_emb = embed([query])
42code_embs = embed(snippets)
43
44# Calculate similarity (dot product of L2-normalized embeddings)
45scores = (query_emb @ code_embs.T)[0]
46for idx, score in enumerate(scores):
47 print(f"[{score:.4f}] {snippets[idx]}")com.microsoft.onnxruntime:onnxruntime-android) for CPU inference.BertTokenizer.kt) to parse strings directly on-device without JVM-overhead Python dependencies.