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 AutoModel, AutoTokenizer
3
4input_texts = [
5 "how to implement quick sort in Python?",
6 "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)",
7 "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",
8]
9
10model_path = 'Salesforce/SFR-Embedding-Code-400M_R'
11tokenizer = AutoTokenizer.from_pretrained(model_path)
12model = AutoModel.from_pretrained(model_path, trust_remote_code=True)
13
14# Tokenize the input texts
15batch_dict = tokenizer(input_texts, max_length=8192, padding=True, truncation=True, return_tensors='pt')
16
17outputs = model(**batch_dict)
18embeddings = outputs.last_hidden_state[:, 0]
19
20# normalize embeddings
21embeddings = F.normalize(embeddings, p=2, dim=1)
22scores = (embeddings[:1] @ embeddings[1:].T) * 100
23print("Similarity Scores:", scores.tolist())
24# Similarity Scores: [[74.84745025634766, 65.39266967773438]]1from sentence_transformers import SentenceTransformer
2from sentence_transformers.util import cos_sim
3
4sentences = [
5 "how to implement quick sort in Python?",
6 "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)",
7 "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",
8]
9
10model = SentenceTransformer('Salesforce/SFR-Embedding-Code-400M_R', trust_remote_code=True)
11embeddings = model.encode(sentences)
12similarities = cos_sim(embeddings[0], embeddings[1:])
13print(similarities)
14# tensor([[0.7485, 0.6539]])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}