Views
No views yet
| Model | Model Size | CoIR AVG (NDCG@10) |
|---|---|---|
| SFR-Embedding-Code | 2B | 67.4 |
| CodeSage-Large-v2 | 1.3B | 64.2 |
| CodeSage-Large | 1.3B | 61.0 |
| SFR-Embedding-Code | 400M | 61.9 |
| CodeRankEmbed | 137M | 60.1 |
| CodeSage-Base | 356M | 57.5 |
| Voyage-Code-002 | - | 56.3 |
| CodeSage-Small | 130M | 54.4 |
1import torch.nn.functional as F
2from transformers import AutoTokenizer, AutoModel
3
4# Each query needs to be accompanied by an corresponding instruction describing the task.
5query_instruction_example = "Given Code or Text, retrieval relevant content"
6queries = [
7 "how to implement quick sort in Python?"
8]
9
10# No instruction needed for retrieval passages
11passages = [
12 "def quick_sort(arr):\n if len(arr) <= 1:\n return arr\n pivot = arr[len(arr) // 2]\n left = [x for x in arr if x < pivot]\n middle = [x for x in arr if x == pivot]\n right = [x for x in arr if x > pivot]\n return quick_sort(left) + middle + quick_sort(right)",
13 "def bubble_sort(arr):\n n = len(arr)\n for i in range(n):\n for j in range(0, n-i-1):\n if arr[j] > arr[j+1]:\n arr[j], arr[j+1] = arr[j+1], arr[j]\n return arr"
14]
15
16# load model with tokenizer
17model = AutoModel.from_pretrained('Salesforce/SFR-Embedding-Code-2B_R', trust_remote_code=True)
18
19# get the embeddings
20max_length = 32768
21query_embeddings = model.encode_queries(queries, instruction=query_instruction_example, max_length=max_length)
22passage_embeddings = model.encode_corpus(passages, max_length=max_length)
23
24# normalize embeddings
25query_embeddings = F.normalize(query_embeddings, p=2, dim=1)
26passage_embeddings = F.normalize(passage_embeddings, p=2, dim=1)
27
28scores = (query_embeddings @ passage_embeddings.T) * 100
29print(scores.tolist())
30# [[69.26929473876953, 58.41606903076172]]1from sentence_transformers import SentenceTransformer
2
3# Each query needs to be accompanied by an corresponding instruction describing the task.
4query_instruction_example = "Instruct: Given Code or Text, retrieval relevant content\nQuery: "
5queries = ["how to implement quick sort in Python?"]
6
7# No instruction needed for retrieval passages
8passages = [
9 "def quick_sort(arr):\n if len(arr) <= 1:\n return arr\n pivot = arr[len(arr) // 2]\n left = [x for x in arr if x < pivot]\n middle = [x for x in arr if x == pivot]\n right = [x for x in arr if x > pivot]\n return quick_sort(left) + middle + quick_sort(right)",
10 "def bubble_sort(arr):\n n = len(arr)\n for i in range(n):\n for j in range(0, n-i-1):\n if arr[j] > arr[j+1]:\n arr[j], arr[j+1] = arr[j+1], arr[j]\n return arr"
11]
12
13# Load the Sentence Transformer model, including pooling
14model = SentenceTransformer('Salesforce/SFR-Embedding-Code-2B_R', trust_remote_code=True)
15
16# Compute the embeddings for both queries and passages. Use 'prompt' for queries only
17query_embeddings = model.encode(queries, prompt=query_instruction_example)
18passage_embeddings = model.encode(passages)
19
20# Compute the similarities between the queries and passages
21similarities = model.similarity(query_embeddings, passage_embeddings)
22print(similarities)
23# tensor([[0.6927, 0.5842]])1@article{liu2024codexembed,
2 title={CodeXEmbed: A Generalist Embedding Model Family for Multiligual and Multi-task Code Retrieval},
3 author={Liu, Ye and Meng, Rui and Jot, Shafiq and Savarese, Silvio and Xiong, Caiming and Zhou, Yingbo and Yavuz, Semih},
4 journal={arXiv preprint arXiv:2411.12644},
5 year={2024}
6}