Sentence Similarity
sentence-transformers
Safetensors
code
bert
feature-extraction
code-retrieval
code-search
linux-kernel
c
text-embeddings-inference
Instructions to use nethunter2023/kernel-code-embed with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use nethunter2023/kernel-code-embed with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("nethunter2023/kernel-code-embed") 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
minimal card
Browse files
README.md
CHANGED
|
@@ -16,23 +16,8 @@ language:
|
|
| 16 |
|
| 17 |
# kernel-code-embed
|
| 18 |
|
| 19 |
-
A
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
Symmetric bi-encoder — queries and code go through the **same** encoder with no
|
| 23 |
-
prefix or instruction prompt.
|
| 24 |
-
|
| 25 |
-
| | |
|
| 26 |
-
|---|---|
|
| 27 |
-
| parameters | 42.6M |
|
| 28 |
-
| layers / hidden / heads | 8 / 512 / 8 |
|
| 29 |
-
| vocabulary | 32,768 byte-level BPE, built for C |
|
| 30 |
-
| max sequence length | **320** |
|
| 31 |
-
| pooling | mean |
|
| 32 |
-
| output | 512-dim, L2-normalised |
|
| 33 |
-
| size on disk | 162 MB |
|
| 34 |
-
|
| 35 |
-
## Usage
|
| 36 |
|
| 37 |
```python
|
| 38 |
from sentence_transformers import SentenceTransformer
|
|
@@ -40,68 +25,13 @@ from sentence_transformers import SentenceTransformer
|
|
| 40 |
model = SentenceTransformer("nethunter2023/kernel-code-embed")
|
| 41 |
|
| 42 |
query = "how are free pages coalesced into larger blocks"
|
| 43 |
-
code = ""
|
| 44 |
-
static inline void __free_one_page(struct page *page, unsigned long pfn,
|
| 45 |
-
struct zone *zone, unsigned int order,
|
| 46 |
-
int migratetype, fpi_t fpi_flags)
|
| 47 |
-
{
|
| 48 |
-
...
|
| 49 |
-
}
|
| 50 |
-
"""
|
| 51 |
|
| 52 |
emb = model.encode([query, code], normalize_embeddings=True)
|
| 53 |
print(emb @ emb.T) # cosine similarity
|
| 54 |
```
|
| 55 |
|
| 56 |
-
|
| 57 |
-
degrades retrieval quality — in a spot check it moved a correct answer from
|
| 58 |
-
rank 12 to rank 40. Chunk longer functions instead.
|
| 59 |
-
|
| 60 |
-
## Evaluating this model
|
| 61 |
-
|
| 62 |
-
Enough detail to reproduce or compare the numbers below.
|
| 63 |
-
|
| 64 |
-
- **Embeddings:** `normalize_embeddings=True`, mean pooling, 512-dim.
|
| 65 |
-
- **Similarity:** cosine (equivalently, dot product on normalised vectors).
|
| 66 |
-
- **No prompt prefixes** on either side — encode raw query text and raw code.
|
| 67 |
-
- **Sequence length:** leave at 320.
|
| 68 |
-
- **Open retrieval below:** every `.c`/`.h` chunk in Linux v7.1-rc5 as the index
|
| 69 |
-
(914,554 chunks), 400 held-out kernel-doc anchors as queries, the documented
|
| 70 |
-
function as the single correct answer.
|
| 71 |
-
- **Hybrid** is dense retrieval fused with BM25 via reciprocal rank fusion.
|
| 72 |
-
- **Closed evaluation:** 2,000 held-out pairs against 4,000 candidates. A
|
| 73 |
-
deliberately hard setting: the distractors come from the same source files as
|
| 74 |
-
the answer, so lexical overlap alone does not separate them.
|
| 75 |
-
|
| 76 |
-
## Results
|
| 77 |
-
|
| 78 |
-
Open retrieval over the whole kernel — **914,554 candidate chunks**:
|
| 79 |
-
|
| 80 |
-
| metric | dense | hybrid (dense + BM25 RRF) |
|
| 81 |
-
|---|---|---|
|
| 82 |
-
| recall@1 | 0.8125 | **0.9050** |
|
| 83 |
-
| recall@5 | 0.9375 | **0.9725** |
|
| 84 |
-
| recall@10 | 0.9575 | **0.9775** |
|
| 85 |
-
| recall@50 | 0.9850 | **0.9950** |
|
| 86 |
-
| MRR | 0.8682 | **0.9374** |
|
| 87 |
-
| median rank | 1 | 1 |
|
| 88 |
-
|
| 89 |
-
The correct function ranks first out of 914,554 candidates 90% of the time.
|
| 90 |
-
|
| 91 |
-
Closed evaluation, against a lexical baseline:
|
| 92 |
-
|
| 93 |
-
| metric | BM25 | this model |
|
| 94 |
-
|---|---|---|
|
| 95 |
-
| accuracy@1 | 0.7115 | **0.9225** |
|
| 96 |
-
| NDCG@10 | 0.8297 | **0.9659** |
|
| 97 |
-
|
| 98 |
-
## Limitations
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
other languages or codebases.
|
| 103 |
-
- **Short context.** 320 tokens. Long functions must be chunked.
|
| 104 |
-
- **Evaluated on kernel-doc anchors**, which are written by kernel developers and
|
| 105 |
-
are more precise than typical end-user questions. Expect lower accuracy on
|
| 106 |
-
casual or ambiguous phrasing.
|
| 107 |
-
- Licensed GPL-2.0, consistent with its Linux kernel training data.
|
|
|
|
| 16 |
|
| 17 |
# kernel-code-embed
|
| 18 |
|
| 19 |
+
A 42.6M-parameter bi-encoder for retrieving **Linux kernel C** from
|
| 20 |
+
natural-language queries. Queries and code use the same encoder, no prefixes.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
```python
|
| 23 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 25 |
model = SentenceTransformer("nethunter2023/kernel-code-embed")
|
| 26 |
|
| 27 |
query = "how are free pages coalesced into larger blocks"
|
| 28 |
+
code = "static inline void __free_one_page(struct page *page, ...) { ... }"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
emb = model.encode([query, code], normalize_embeddings=True)
|
| 31 |
print(emb @ emb.T) # cosine similarity
|
| 32 |
```
|
| 33 |
|
| 34 |
+
512-dim output, mean pooling, L2-normalised — score with cosine similarity.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
`max_seq_length` is **320** and should not be raised; longer inputs degrade
|
| 37 |
+
retrieval. Chunk longer functions instead.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|