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
kernel-code-embed
Browse files- 1_Pooling/config.json +10 -0
- README.md +107 -0
- config.json +25 -0
- config_sentence_transformers.json +10 -0
- model.safetensors +3 -0
- modules.json +20 -0
- sentence_bert_config.json +4 -0
- special_tokens_map.json +37 -0
- tokenizer.json +0 -0
- tokenizer_config.json +53 -0
1_Pooling/config.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"word_embedding_dimension": 512,
|
| 3 |
+
"pooling_mode_cls_token": false,
|
| 4 |
+
"pooling_mode_mean_tokens": true,
|
| 5 |
+
"pooling_mode_max_tokens": false,
|
| 6 |
+
"pooling_mode_mean_sqrt_len_tokens": false,
|
| 7 |
+
"pooling_mode_weightedmean_tokens": false,
|
| 8 |
+
"pooling_mode_lasttoken": false,
|
| 9 |
+
"include_prompt": true
|
| 10 |
+
}
|
README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: gpl-2.0
|
| 3 |
+
library_name: sentence-transformers
|
| 4 |
+
pipeline_tag: sentence-similarity
|
| 5 |
+
tags:
|
| 6 |
+
- sentence-transformers
|
| 7 |
+
- feature-extraction
|
| 8 |
+
- sentence-similarity
|
| 9 |
+
- code-retrieval
|
| 10 |
+
- code-search
|
| 11 |
+
- linux-kernel
|
| 12 |
+
- c
|
| 13 |
+
language:
|
| 14 |
+
- code
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
# kernel-code-embed
|
| 18 |
+
|
| 19 |
+
A compact **C code embedding model for Linux kernel retrieval**. Ask a question
|
| 20 |
+
in English; it ranks kernel functions by how well they answer it.
|
| 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
|
| 39 |
+
|
| 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 |
+
**Do not raise `max_seq_length` above 320.** Feeding longer inputs measurably
|
| 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 |
+
- **Domain-specific.** Trained on Linux kernel C only. It is not a general-purpose
|
| 101 |
+
code or text embedding model, and it should not be expected to transfer to
|
| 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.
|
config.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "artifacts/bert-kernel-mlm/phase2",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"BertModel"
|
| 5 |
+
],
|
| 6 |
+
"attention_probs_dropout_prob": 0.1,
|
| 7 |
+
"classifier_dropout": null,
|
| 8 |
+
"hidden_act": "gelu",
|
| 9 |
+
"hidden_dropout_prob": 0.1,
|
| 10 |
+
"hidden_size": 512,
|
| 11 |
+
"initializer_range": 0.02,
|
| 12 |
+
"intermediate_size": 2048,
|
| 13 |
+
"layer_norm_eps": 1e-12,
|
| 14 |
+
"max_position_embeddings": 512,
|
| 15 |
+
"model_type": "bert",
|
| 16 |
+
"num_attention_heads": 8,
|
| 17 |
+
"num_hidden_layers": 8,
|
| 18 |
+
"pad_token_id": 0,
|
| 19 |
+
"position_embedding_type": "absolute",
|
| 20 |
+
"torch_dtype": "float32",
|
| 21 |
+
"transformers_version": "4.47.1",
|
| 22 |
+
"type_vocab_size": 1,
|
| 23 |
+
"use_cache": true,
|
| 24 |
+
"vocab_size": 32768
|
| 25 |
+
}
|
config_sentence_transformers.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"__version__": {
|
| 3 |
+
"sentence_transformers": "3.3.1",
|
| 4 |
+
"transformers": "4.47.1",
|
| 5 |
+
"pytorch": "2.5.1+cu121"
|
| 6 |
+
},
|
| 7 |
+
"prompts": {},
|
| 8 |
+
"default_prompt_name": null,
|
| 9 |
+
"similarity_fn_name": "cosine"
|
| 10 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cef78bc1945c263922a5d7d451b1f72d7c4c38009d89751fa122cdcf75ccf336
|
| 3 |
+
size 170105544
|
modules.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"idx": 0,
|
| 4 |
+
"name": "0",
|
| 5 |
+
"path": "",
|
| 6 |
+
"type": "sentence_transformers.models.Transformer"
|
| 7 |
+
},
|
| 8 |
+
{
|
| 9 |
+
"idx": 1,
|
| 10 |
+
"name": "1",
|
| 11 |
+
"path": "1_Pooling",
|
| 12 |
+
"type": "sentence_transformers.models.Pooling"
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
"idx": 2,
|
| 16 |
+
"name": "2",
|
| 17 |
+
"path": "2_Normalize",
|
| 18 |
+
"type": "sentence_transformers.models.Normalize"
|
| 19 |
+
}
|
| 20 |
+
]
|
sentence_bert_config.json
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"max_seq_length": 320,
|
| 3 |
+
"do_lower_case": false
|
| 4 |
+
}
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": {
|
| 3 |
+
"content": "[CLS]",
|
| 4 |
+
"lstrip": false,
|
| 5 |
+
"normalized": false,
|
| 6 |
+
"rstrip": false,
|
| 7 |
+
"single_word": false
|
| 8 |
+
},
|
| 9 |
+
"mask_token": {
|
| 10 |
+
"content": "[MASK]",
|
| 11 |
+
"lstrip": false,
|
| 12 |
+
"normalized": false,
|
| 13 |
+
"rstrip": false,
|
| 14 |
+
"single_word": false
|
| 15 |
+
},
|
| 16 |
+
"pad_token": {
|
| 17 |
+
"content": "[PAD]",
|
| 18 |
+
"lstrip": false,
|
| 19 |
+
"normalized": false,
|
| 20 |
+
"rstrip": false,
|
| 21 |
+
"single_word": false
|
| 22 |
+
},
|
| 23 |
+
"sep_token": {
|
| 24 |
+
"content": "[SEP]",
|
| 25 |
+
"lstrip": false,
|
| 26 |
+
"normalized": false,
|
| 27 |
+
"rstrip": false,
|
| 28 |
+
"single_word": false
|
| 29 |
+
},
|
| 30 |
+
"unk_token": {
|
| 31 |
+
"content": "[UNK]",
|
| 32 |
+
"lstrip": false,
|
| 33 |
+
"normalized": false,
|
| 34 |
+
"rstrip": false,
|
| 35 |
+
"single_word": false
|
| 36 |
+
}
|
| 37 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "[PAD]",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"1": {
|
| 12 |
+
"content": "[UNK]",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"2": {
|
| 20 |
+
"content": "[CLS]",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"3": {
|
| 28 |
+
"content": "[SEP]",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"4": {
|
| 36 |
+
"content": "[MASK]",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"clean_up_tokenization_spaces": false,
|
| 45 |
+
"cls_token": "[CLS]",
|
| 46 |
+
"extra_special_tokens": {},
|
| 47 |
+
"mask_token": "[MASK]",
|
| 48 |
+
"model_max_length": 320,
|
| 49 |
+
"pad_token": "[PAD]",
|
| 50 |
+
"sep_token": "[SEP]",
|
| 51 |
+
"tokenizer_class": "PreTrainedTokenizerFast",
|
| 52 |
+
"unk_token": "[UNK]"
|
| 53 |
+
}
|