Safetensors
yliu279 commited on
Commit
808340d
·
verified ·
1 Parent(s): d347eca

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +86 -0
README.md ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: cc-by-nc-4.0
3
+ ---
4
+ <h1 align="center">Salesforce/SFR-Embedding-Code-2B_R</h1>
5
+
6
+ **SFR-Embedding by Salesforce Research.**
7
+
8
+ The Salesforce/SFR-Embedding-Code is a generalist embedding model family for multilingual and multi-task code and Text retrieval. It demonstrates superior performance compared to various open-source code embedding models across multiple code retrieval tasks.
9
+
10
+ Check out our [paper](https://arxiv.org/abs/2411.12644) for more details!
11
+
12
+ We also offer 400M-size model [Salesforce/SFR-Embedding-Code-400_R](https://huggingface.co/Salesforce/SFR-Embedding-Code-400M_R)
13
+
14
+ ### Ethical Considerations
15
+ This release is for research purposes only in support of an academic paper. Our models, datasets, and code are not specifically designed or evaluated for all downstream purposes. We strongly recommend users evaluate and address potential concerns related to accuracy, safety, and fairness before deploying this model. We encourage users to consider the common limitations of AI, comply with applicable laws, and leverage best practices when selecting use cases, particularly for high-risk scenarios where errors or misuse could significantly impact people’s lives, rights, or safety. For further guidance on use cases, refer to our AUP and AI AUP.
16
+
17
+ ### License Statement:
18
+ Users need to make their own assessment regarding any obligations or responsibilities under the corresponding licenses or terms and conditions pertaining to the original datasets and data. This release is for research purposes only in support of an academic paper.
19
+
20
+ ### Performance on CoIR Benchmark
21
+ | Model | Model Size | CoIR AVG (NDCG@10) |
22
+ |-----------------------|------------|---------------------|
23
+ | **SFR-Embedding-Code** | 2B | 67.4 |
24
+ | CodeSage-Large-v2 | 1.3B | 64.2 |
25
+ | CodeSage-Large | 1.3B | 61.0 |
26
+ | **SFR-Embedding-Code** | 400M | 61.9 |
27
+ | CodeRankEmbed | 137M | 60.1 |
28
+ | CodeSage-Base | 356M | 57.5 |
29
+ | Voyage-Code-002 | - | 56.3 |
30
+ | CodeSage-Small | 130M | 54.4 |
31
+
32
+
33
+ SFR-Embedding Team
34
+ * Ye Liu
35
+ * Rui Meng
36
+ * Shafiq Rayhan Joty
37
+ * Silvio Savarese
38
+ * Caiming Xiong
39
+ * Yingbo Zhou
40
+ * Semih Yavuz
41
+
42
+ ## How to run
43
+
44
+ #### Transformers
45
+ ```python
46
+ import torch.nn.functional as F
47
+ from transformers import AutoTokenizer, AutoModel
48
+
49
+ # Each query needs to be accompanied by an corresponding instruction describing the task.
50
+ query_instruction_example = "Given Code or Text, retrieval relevant content"
51
+ queries = [
52
+ "how to implement quick sort in Python?"
53
+ ]
54
+
55
+ # No instruction needed for retrieval passages
56
+ passages = [
57
+ "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)",
58
+ "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"
59
+ ]
60
+
61
+ # load model with tokenizer
62
+ model = AutoModel.from_pretrained('Salesforce/SFR-Embedding-Code-2B_R', trust_remote_code=True)
63
+
64
+ # get the embeddings
65
+ max_length = 32768
66
+ query_embeddings = model.encode_queries(queries, instruction=query_instruction_example, max_length=max_length)
67
+ passage_embeddings = model.encode_corpus(passages, max_length=max_length)
68
+
69
+ # normalize embeddings
70
+ query_embeddings = F.normalize(query_embeddings, p=2, dim=1)
71
+ passage_embeddings = F.normalize(passage_embeddings, p=2, dim=1)
72
+
73
+ scores = (query_embeddings @ passage_embeddings.T) * 100
74
+ print(scores.tolist())
75
+ ```
76
+
77
+ ### Citation
78
+ ```bibtex
79
+ @article{liu2024codexembed,
80
+ title={CodeXEmbed: A Generalist Embedding Model Family for Multiligual and Multi-task Code Retrieval},
81
+ author={Liu, Ye and Meng, Rui and Jot, Shafiq and Savarese, Silvio and Xiong, Caiming and Zhou, Yingbo and Yavuz, Semih},
82
+ journal={arXiv preprint arXiv:2411.12644},
83
+ year={2024}
84
+ }
85
+ ```
86
+