jamie8johnson commited on
Commit
0ef940e
·
verified ·
1 Parent(s): aa6ca26

Add model card with benchmarks, training details, and usage examples

Browse files
Files changed (1) hide show
  1. README.md +168 -0
README.md ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ - code
5
+ library_name: sentence-transformers
6
+ pipeline_tag: sentence-similarity
7
+ tags:
8
+ - sentence-transformers
9
+ - feature-extraction
10
+ - code-search
11
+ - code-retrieval
12
+ - onnx
13
+ - bert
14
+ - e5
15
+ - lora
16
+ - rag
17
+ datasets:
18
+ - code_search_net
19
+ license: apache-2.0
20
+ base_model: intfloat/e5-base-v2
21
+ model-index:
22
+ - name: e5-base-v2-code-search
23
+ results:
24
+ - task:
25
+ type: Retrieval
26
+ dataset:
27
+ type: code_search_net
28
+ name: CodeSearchNet
29
+ metrics:
30
+ - type: ndcg_at_10
31
+ value: 0.671
32
+ name: NDCG@10 (avg, 6 languages)
33
+ - task:
34
+ type: Retrieval
35
+ dataset:
36
+ type: cosqa
37
+ name: CosQA
38
+ metrics:
39
+ - type: ndcg_at_10
40
+ value: 0.334
41
+ name: NDCG@10
42
+ ---
43
+
44
+ # e5-base-v2-code-search
45
+
46
+ A fine-tuned code search embedding model based on [intfloat/e5-base-v2](https://huggingface.co/intfloat/e5-base-v2) (110M parameters). Optimized for natural language → code retrieval across 6 programming languages.
47
+
48
+ Built for [cqs](https://github.com/jamie8johnson/cqs), a semantic code search tool for AI agents.
49
+
50
+ ## Key Results
51
+
52
+ | Benchmark | Metric | Score | vs Base E5 |
53
+ |-----------|--------|-------|------------|
54
+ | CodeSearchNet (6 lang avg) | NDCG@10 | **0.671** | **+4.4pp** |
55
+ | CosQA (out-of-distribution) | NDCG@10 | **0.334** | **+0.5pp** |
56
+ | Hard eval (55 confusable pairs) | Recall@1 | **92.7%** | +1.8pp* |
57
+
58
+ *Hard eval improvement from full pipeline (LoRA + NL enrichment + doc comments), not LoRA alone.
59
+
60
+ ### Per-Language NDCG@10 (CodeSearchNet)
61
+
62
+ | Language | Base E5 | This Model | Delta |
63
+ |----------|---------|------------|-------|
64
+ | Go | 0.624 | **0.746** | +0.122 |
65
+ | Java | 0.571 | **0.621** | +0.050 |
66
+ | JavaScript | 0.487 | **0.535** | +0.048 |
67
+ | Ruby | 0.526 | **0.592** | +0.066 |
68
+ | Python | 0.888 | **0.909** | +0.021 |
69
+ | PHP | 0.601 | **0.623** | +0.022 |
70
+
71
+ ### CoIR Leaderboard Context
72
+
73
+ Among 110M-class models on the [CoIR benchmark](https://archersama.github.io/coir/) (ACL 2025), E5-base-v2 is already #7 out of 13 entries. This LoRA fine-tune extends the lead over other 110M models (BGE-Base 42.77, GTE-Base 36.75, UniXcoder 37.33) while remaining CPU-runnable in <100ms per query.
74
+
75
+ ## Training
76
+
77
+ - **Method:** LoRA (rank 16) fine-tuning of E5-base-v2
78
+ - **Data:** 50,000 CodeSearchNet triplets (query, positive code, negative code) + docstring-as-query pairs
79
+ - **Training time:** 15 minutes on RTX 4090
80
+ - **Epochs:** 1
81
+ - **Loss:** Triplet loss with in-batch negatives
82
+
83
+ ### Why LoRA v3?
84
+
85
+ We trained 6 LoRA variants with different data sizes (10k–200k), ranks (16, 32), and data mixes (CSN-only, CSN+CosQA+SO). v3 (50k+docs/1ep) was the sweet spot:
86
+
87
+ | Config | CSN NDCG@10 | CosQA (transfer) |
88
+ |--------|-------------|-------------------|
89
+ | Base E5 | 0.627 | 0.329 |
90
+ | **v3 (50k+docs/1ep)** | **0.671** | **0.334** |
91
+ | v4 (200k/3ep) | 0.680 | 0.305 (over-specialized) |
92
+ | v5 (100k/3ep) | 0.678 | 0.348 |
93
+
94
+ v4 achieves higher CSN but regresses on out-of-distribution queries. v3 balances specialization with transfer.
95
+
96
+ ## Usage
97
+
98
+ ### With sentence-transformers
99
+
100
+ ```python
101
+ from sentence_transformers import SentenceTransformer
102
+
103
+ model = SentenceTransformer("jamie8johnson/e5-base-v2-code-search")
104
+
105
+ queries = ["query: find all files matching a glob pattern"]
106
+ passages = ["passage: def glob_match(pattern, path): ..."]
107
+
108
+ q_emb = model.encode(queries, normalize_embeddings=True)
109
+ p_emb = model.encode(passages, normalize_embeddings=True)
110
+
111
+ similarity = q_emb @ p_emb.T
112
+ ```
113
+
114
+ ### With cqs (semantic code search CLI)
115
+
116
+ ```bash
117
+ # Uses this model by default
118
+ cqs "find files matching glob" --json
119
+
120
+ # Override with base E5
121
+ CQS_EMBEDDING_MODEL=intfloat/e5-base-v2 cqs "find files matching glob"
122
+ ```
123
+
124
+ ### With ONNX Runtime
125
+
126
+ ```python
127
+ import onnxruntime as ort
128
+ from transformers import AutoTokenizer
129
+
130
+ tokenizer = AutoTokenizer.from_pretrained("jamie8johnson/e5-base-v2-code-search")
131
+ session = ort.InferenceSession("model.onnx")
132
+
133
+ inputs = tokenizer("query: sort array by key", return_tensors="np", padding=True, truncation=True)
134
+ outputs = session.run(None, dict(inputs))
135
+ embeddings = outputs[0][:, 0, :] # CLS token
136
+ ```
137
+
138
+ ## Enrichment Pipeline
139
+
140
+ This model achieves its best results when combined with cqs's NL enrichment pipeline, which prepends structured metadata to code before embedding:
141
+
142
+ 1. **Type-aware signatures** (free) — append `fn foo(x: i32) -> Result<()>` to NL description
143
+ 2. **Call graph context** (free) — append caller/callee function names
144
+ 3. **LLM discriminating summaries** (optional, ~$0.15/3k functions) — "what makes this function unique"
145
+ 4. **Doc comment generation** (optional, ~$1.50/3k functions) — structured parameter/return docs
146
+
147
+ The enrichment transforms code into richer text before the embedding model sees it. This is why a 110M model can compete with specialized models 3-20x larger — the intelligence is in the text transformation, not the model size.
148
+
149
+ ## Limitations
150
+
151
+ - Trained primarily on CodeSearchNet (Go, Java, JavaScript, Ruby, Python, PHP). Performance on other languages (Rust, C++, TypeScript) relies on E5's base multilingual capability.
152
+ - English queries only. E5-base-v2's multilingual capability is untested for code search.
153
+ - 512 token max sequence length. Long functions are truncated.
154
+
155
+ ## Citation
156
+
157
+ ```bibtex
158
+ @software{cqs2026,
159
+ title={cqs: Semantic Code Search with Local Embeddings},
160
+ author={Johnson, Jamie},
161
+ url={https://github.com/jamie8johnson/cqs},
162
+ year={2026}
163
+ }
164
+ ```
165
+
166
+ ## License
167
+
168
+ Apache 2.0 (same as base E5-base-v2).