Add Sentence Transformers integration

#7
by tomaarsen HF staff - opened
1_Pooling/config.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ab191715904c1ed56db7a92f4e898117b778d055cc4ba96dad8044f0c10ca9e4
3
+ size 306
README.md CHANGED
@@ -1,88 +1,3 @@
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](https://www.salesforce.com/content/dam/web/en_us/www/documents/legal/Agreements/policies/ExternalFacing_Services_Policy.pdf) and [AI AUP](https://www.salesforce.com/content/dam/web/en_us/www/documents/legal/Agreements/policies/ai-acceptable-use-policy.pdf).
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
- This released model is a fine-tuned version of Gemma and Gemma is provided under and subject to the Gemma Terms of Use found at ai.google.dev/gemma/terms. Additionally, the use of this model is restricted as set forth in the Gemma Prohibited Use Policy at ai.google.dev/gemma/prohibited_use_policy ("Prohibited Use Policy"), which is hereby incorporated by reference into this Agreement.
21
-
22
- ### Performance on CoIR Benchmark
23
- | Model | Model Size | CoIR AVG (NDCG@10) |
24
- |-----------------------|------------|---------------------|
25
- | **SFR-Embedding-Code** | 2B | 67.4 |
26
- | CodeSage-Large-v2 | 1.3B | 64.2 |
27
- | CodeSage-Large | 1.3B | 61.0 |
28
- | **SFR-Embedding-Code** | 400M | 61.9 |
29
- | CodeRankEmbed | 137M | 60.1 |
30
- | CodeSage-Base | 356M | 57.5 |
31
- | Voyage-Code-002 | - | 56.3 |
32
- | CodeSage-Small | 130M | 54.4 |
33
-
34
-
35
- SFR-Embedding Team († indicates co-leaders)
36
- * Ye Liu
37
- * Rui Meng
38
- * Shafiq Rayhan Joty
39
- * Silvio Savarese
40
- * Caiming Xiong †
41
- * Yingbo Zhou †
42
- * Semih Yavuz †
43
-
44
- ## How to run
45
-
46
- #### Transformers
47
- ```python
48
- import torch.nn.functional as F
49
- from transformers import AutoTokenizer, AutoModel
50
-
51
- # Each query needs to be accompanied by an corresponding instruction describing the task.
52
- query_instruction_example = "Given Code or Text, retrieval relevant content"
53
- queries = [
54
- "how to implement quick sort in Python?"
55
- ]
56
-
57
- # No instruction needed for retrieval passages
58
- passages = [
59
- "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)",
60
- "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"
61
- ]
62
-
63
- # load model with tokenizer
64
- model = AutoModel.from_pretrained('Salesforce/SFR-Embedding-Code-2B_R', trust_remote_code=True)
65
-
66
- # get the embeddings
67
- max_length = 32768
68
- query_embeddings = model.encode_queries(queries, instruction=query_instruction_example, max_length=max_length)
69
- passage_embeddings = model.encode_corpus(passages, max_length=max_length)
70
-
71
- # normalize embeddings
72
- query_embeddings = F.normalize(query_embeddings, p=2, dim=1)
73
- passage_embeddings = F.normalize(passage_embeddings, p=2, dim=1)
74
-
75
- scores = (query_embeddings @ passage_embeddings.T) * 100
76
- print(scores.tolist())
77
- ```
78
-
79
- ### Citation
80
- ```bibtex
81
- @article{liu2024codexembed,
82
- title={CodeXEmbed: A Generalist Embedding Model Family for Multiligual and Multi-task Code Retrieval},
83
- author={Liu, Ye and Meng, Rui and Jot, Shafiq and Savarese, Silvio and Xiong, Caiming and Zhou, Yingbo and Yavuz, Semih},
84
- journal={arXiv preprint arXiv:2411.12644},
85
- year={2024}
86
- }
87
- ```
88
-
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2949f38d0775e2f910608a1093922e3ed9f582f53323eb09d8727ef21e178dc7
3
+ size 6432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modeling_gemma2.py CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:f131d619523ba705182c1e6811475d606532ecd5d10a3c53aa0098146c01fce1
3
- size 63465
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a4e96bee517326ec4be10c5c2c6dc4cd33087edbed6dbf441d423fd982108342
3
+ size 63535
modules.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:27e3a229cecaf4d6ec90fb8e439d6bd2da7b2e22ff49a23e0b0a293a5f3e413c
3
+ size 242
sentence_bert_config.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:262c2d8dc4213f45a861f558d2034b50e49b387d33f40988a7e6d476f0310c44
3
+ size 58