Tom Aarsen commited on
Commit
979b19f
·
1 Parent(s): b0a525a

Update README; modeling_gemma2.py; overwrite

Browse files
Files changed (2) hide show
  1. README.md +41 -6
  2. modeling_gemma2.py +3 -0
README.md CHANGED
@@ -1,5 +1,11 @@
1
  ---
2
  license: cc-by-nc-4.0
 
 
 
 
 
 
3
  ---
4
  <h1 align="center">Salesforce/SFR-Embedding-Code-2B_R</h1>
5
 
@@ -12,7 +18,7 @@ Check out our [paper](https://arxiv.org/abs/2411.12644) for more details!
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.
@@ -32,14 +38,14 @@ This released model is a fine-tuned version of Gemma and Gemma is provided under
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
 
@@ -52,7 +58,7 @@ from transformers import AutoTokenizer, AutoModel
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 = [
@@ -74,6 +80,35 @@ 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
 
1
  ---
2
  license: cc-by-nc-4.0
3
+ pipeline_tag: feature-extraction
4
+ tags:
5
+ - transformers
6
+ - sentence-transformers
7
+ - code
8
+ - retrieval
9
  ---
10
  <h1 align="center">Salesforce/SFR-Embedding-Code-2B_R</h1>
11
 
 
18
  We also offer 400M-size model [Salesforce/SFR-Embedding-Code-400_R](https://huggingface.co/Salesforce/SFR-Embedding-Code-400M_R)
19
 
20
  ### Ethical Considerations
21
+ 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).
22
 
23
  ### License Statement:
24
  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.
 
38
  | CodeSage-Small | 130M | 54.4 |
39
 
40
 
41
+ SFR-Embedding Team (ÔÇá indicates co-leaders)
42
  * Ye Liu
43
  * Rui Meng
44
  * Shafiq Rayhan Joty
45
  * Silvio Savarese
46
+ * Caiming Xiong ÔÇá
47
+ * Yingbo Zhou ÔÇá
48
+ * Semih Yavuz ÔÇá
49
 
50
  ## How to run
51
 
 
58
  query_instruction_example = "Given Code or Text, retrieval relevant content"
59
  queries = [
60
  "how to implement quick sort in Python?"
61
+ ]
62
 
63
  # No instruction needed for retrieval passages
64
  passages = [
 
80
 
81
  scores = (query_embeddings @ passage_embeddings.T) * 100
82
  print(scores.tolist())
83
+ # [[52.76957702636719, 26.118698120117188]]
84
+ ```
85
+
86
+ #### Sentence Transformers
87
+
88
+ ```python
89
+ from sentence_transformers import SentenceTransformer
90
+
91
+ # Each query needs to be accompanied by an corresponding instruction describing the task.
92
+ query_instruction_example = "Instruct: Given Code or Text, retrieval relevant content\nQuery: "
93
+ queries = ["how to implement quick sort in Python?"]
94
+
95
+ # No instruction needed for retrieval passages
96
+ passages = [
97
+ "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)",
98
+ "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"
99
+ ]
100
+
101
+ # Load the Sentence Transformer model, including pooling
102
+ model = SentenceTransformer('Salesforce/SFR-Embedding-Code-2B_R', trust_remote_code=True)
103
+
104
+ # Compute the embeddings for both queries and passages. Use 'prompt' for queries only
105
+ query_embeddings = model.encode(queries, prompt=query_instruction_example)
106
+ passage_embeddings = model.encode(passages)
107
+
108
+ # Compute the similarities between the queries and passages
109
+ similarities = model.similarity(query_embeddings, passage_embeddings)
110
+ print(similarities)
111
+ # tensor([[0.5277, 0.2612]])
112
  ```
113
 
114
  ### Citation
modeling_gemma2.py CHANGED
@@ -1350,6 +1350,9 @@ class CodeXEmbedModel2B(PreTrainedModel):
1350
  self.tokenizer.pad_token = self.tokenizer.eos_token
1351
  self.tokenizer.padding_side = 'right'
1352
 
 
 
 
1353
  def last_token_pool(self, model_output, attention_mask):
1354
  last_hidden_states = model_output.last_hidden_state
1355
  sequence_lengths = attention_mask.sum(dim=1) - 1
 
1350
  self.tokenizer.pad_token = self.tokenizer.eos_token
1351
  self.tokenizer.padding_side = 'right'
1352
 
1353
+ def forward(self, **kwargs):
1354
+ return self.model(**kwargs)
1355
+
1356
  def last_token_pool(self, model_output, attention_mask):
1357
  last_hidden_states = model_output.last_hidden_state
1358
  sequence_lengths = attention_mask.sum(dim=1) - 1