Add new SentenceTransformer model.
Browse files- README.md +100 -145
- config_sentence_transformers.json +1 -1
README.md
CHANGED
@@ -6,180 +6,135 @@ tags:
|
|
6 |
- sentence-similarity
|
7 |
- feature-extraction
|
8 |
---
|
9 |
-
<div align="center">
|
10 |
-
<img src="https://raw.githubusercontent.com/Anditty/OASIS/refs/heads/main/Group.svg" width="60%" alt="Kwaipilot" />
|
11 |
-
</div>
|
12 |
-
<hr>
|
13 |
|
14 |
-
#
|
|
|
|
|
15 |
|
16 |
## Model Details
|
17 |
-
**Model Name**: OASIS (Optimized Augmentation Strategy for Improved code Search)
|
18 |
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
|
23 |
-
**
|
|
|
|
|
24 |
|
25 |
-
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
Kwaipilot upcoming initiatives include:
|
33 |
|
34 |
-
|
35 |
-
- Releasing technical reports.
|
36 |
-
- Releasing natural language processing models.
|
37 |
-
- ...
|
38 |
|
|
|
|
|
|
|
39 |
|
40 |
-
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
|
|
53 |
|
54 |
-
|
55 |
-
pip install -U torch
|
56 |
-
pip install -U transformers
|
57 |
-
```
|
58 |
|
59 |
-
|
|
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
import torch.nn.functional as F
|
64 |
-
|
65 |
-
from torch import Tensor
|
66 |
-
from transformers import AutoModel, AutoTokenizer
|
67 |
-
|
68 |
-
def last_token_pool(last_hidden_states: Tensor, attention_mask: Tensor) -> Tensor:
|
69 |
-
left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
|
70 |
-
if left_padding:
|
71 |
-
return last_hidden_states[:, -1]
|
72 |
-
else:
|
73 |
-
sequence_lengths = attention_mask.sum(dim=1) - 1
|
74 |
-
batch_size = last_hidden_states.shape[0]
|
75 |
-
return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
|
76 |
-
|
77 |
-
# Add query prompt
|
78 |
-
def get_query_prompt(query: str):
|
79 |
-
query_description = 'Given a code search query, retrieve relevant code snippet that answer the query'
|
80 |
-
prompt = f'Instruct: {query_description}\nQuery: {query}'
|
81 |
-
return prompt
|
82 |
-
|
83 |
-
query = "How to do quicksort in python?"
|
84 |
-
|
85 |
-
code1 = """def bubble_sort(arr):
|
86 |
-
n = len(arr)
|
87 |
-
for i in range(n):
|
88 |
-
swapped = False
|
89 |
-
for j in range(1, n - i):
|
90 |
-
if arr[j - 1] > arr[j]:
|
91 |
-
arr[j - 1], arr[j] = arr[j], arr[j - 1]
|
92 |
-
swapped = True
|
93 |
-
if not swapped:
|
94 |
-
break
|
95 |
-
return arr"""
|
96 |
-
|
97 |
-
code2 = """def quick_sort(arr):
|
98 |
-
if len(arr) <= 1:
|
99 |
-
return arr
|
100 |
-
else:
|
101 |
-
pivot = arr[0]
|
102 |
-
less = [x for x in arr[1:] if x <= pivot]
|
103 |
-
greater = [x for x in arr[1:] if x > pivot]
|
104 |
-
return quick_sort(less) + [pivot] + quick_sort(greater)"""
|
105 |
-
|
106 |
-
model = AutoModel.from_pretrained("Kwaipilot/OASIS-code-1.3B", output_hidden_states=True)
|
107 |
-
tokenizer = AutoTokenizer.from_pretrained("Kwaipilot/OASIS-code-1.3B")
|
108 |
-
|
109 |
-
# Tokenize and inference
|
110 |
-
inputs = tokenizer([get_query_prompt(query), code1, code2], max_length=8192, padding=True, truncation=True, return_tensors='pt')
|
111 |
-
outputs = model(**inputs)
|
112 |
-
|
113 |
-
# Last token pooling
|
114 |
-
embeddings = last_token_pool(outputs.hidden_states[-1], inputs['attention_mask'])
|
115 |
-
print(embeddings.shape)
|
116 |
-
# torch.Size([3, 2048])
|
117 |
|
118 |
-
|
119 |
-
similarity = embeddings @ embeddings.T
|
120 |
-
print(similarity[0, 1:])
|
121 |
-
# tensor([0.6495, 0.8036])
|
122 |
-
```
|
123 |
|
|
|
124 |
|
|
|
|
|
125 |
|
126 |
-
|
|
|
127 |
|
128 |
-
|
|
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
```
|
133 |
|
134 |
-
|
135 |
-
|
136 |
-
from sentence_transformers import SentenceTransformer
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
query = "How to do quicksort in python?"
|
142 |
-
|
143 |
-
code1 = """def bubble_sort(arr):
|
144 |
-
n = len(arr)
|
145 |
-
for i in range(n):
|
146 |
-
swapped = False
|
147 |
-
for j in range(1, n - i):
|
148 |
-
if arr[j - 1] > arr[j]:
|
149 |
-
arr[j - 1], arr[j] = arr[j], arr[j - 1]
|
150 |
-
swapped = True
|
151 |
-
if not swapped:
|
152 |
-
break
|
153 |
-
return arr"""
|
154 |
-
|
155 |
-
code2 = """def quick_sort(arr):
|
156 |
-
if len(arr) <= 1:
|
157 |
-
return arr
|
158 |
-
else:
|
159 |
-
pivot = arr[0]
|
160 |
-
less = [x for x in arr[1:] if x <= pivot]
|
161 |
-
greater = [x for x in arr[1:] if x > pivot]
|
162 |
-
return quick_sort(less) + [pivot] + quick_sort(greater)"""
|
163 |
|
164 |
-
|
165 |
-
|
166 |
-
code_embeddings = model.encode([code1, code2])
|
167 |
|
168 |
-
|
169 |
-
# (2, 2048)
|
170 |
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
|
|
|
|
|
|
|
|
177 |
|
178 |
### BibTeX
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
- sentence-similarity
|
7 |
- feature-extraction
|
8 |
---
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# SentenceTransformer
|
11 |
+
|
12 |
+
This is a [sentence-transformers](https://www.SBERT.net) model trained. It maps sentences & paragraphs to a 2048-dimensional dense vector space and can be used for semantic textual similarity, semantic search, paraphrase mining, text classification, clustering, and more.
|
13 |
|
14 |
## Model Details
|
|
|
15 |
|
16 |
+
### Model Description
|
17 |
+
- **Model Type:** Sentence Transformer
|
18 |
+
<!-- - **Base model:** [Unknown](https://huggingface.co/unknown) -->
|
19 |
+
- **Maximum Sequence Length:** 8192 tokens
|
20 |
+
- **Output Dimensionality:** 2048 tokens
|
21 |
+
- **Similarity Function:** Cosine Similarity
|
22 |
+
<!-- - **Training Dataset:** Unknown -->
|
23 |
+
<!-- - **Language:** Unknown -->
|
24 |
+
<!-- - **License:** Unknown -->
|
25 |
|
26 |
+
### Model Sources
|
27 |
|
28 |
+
- **Documentation:** [Sentence Transformers Documentation](https://sbert.net)
|
29 |
+
- **Repository:** [Sentence Transformers on GitHub](https://github.com/UKPLab/sentence-transformers)
|
30 |
+
- **Hugging Face:** [Sentence Transformers on Hugging Face](https://huggingface.co/models?library=sentence-transformers)
|
31 |
|
32 |
+
### Full Model Architecture
|
33 |
|
34 |
+
```
|
35 |
+
SentenceTransformer(
|
36 |
+
(0): Transformer({'max_seq_length': 8192, 'do_lower_case': False}) with Transformer model: LlamaModel
|
37 |
+
(1): Pooling({'word_embedding_dimension': 2048, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': False, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': True, 'include_prompt': True})
|
38 |
+
)
|
39 |
+
```
|
40 |
|
41 |
+
## Usage
|
42 |
|
43 |
+
### Direct Usage (Sentence Transformers)
|
|
|
44 |
|
45 |
+
First install the Sentence Transformers library:
|
|
|
|
|
|
|
46 |
|
47 |
+
```bash
|
48 |
+
pip install -U sentence-transformers
|
49 |
+
```
|
50 |
|
51 |
+
Then you can load this model and run inference.
|
52 |
+
```python
|
53 |
+
from sentence_transformers import SentenceTransformer
|
54 |
|
55 |
+
# Download from the 🤗 Hub
|
56 |
+
model = SentenceTransformer("Kwaipilot/OASIS-code-1.3B")
|
57 |
+
# Run inference
|
58 |
+
sentences = [
|
59 |
+
'The weather is lovely today.',
|
60 |
+
"It's so sunny outside!",
|
61 |
+
'He drove to the stadium.',
|
62 |
+
]
|
63 |
+
embeddings = model.encode(sentences)
|
64 |
+
print(embeddings.shape)
|
65 |
+
# [3, 2048]
|
66 |
|
67 |
+
# Get the similarity scores for the embeddings
|
68 |
+
similarities = model.similarity(embeddings, embeddings)
|
69 |
+
print(similarities.shape)
|
70 |
+
# [3, 3]
|
71 |
+
```
|
72 |
|
73 |
+
<!--
|
74 |
+
### Direct Usage (Transformers)
|
75 |
|
76 |
+
<details><summary>Click to see the direct usage in Transformers</summary>
|
|
|
|
|
|
|
77 |
|
78 |
+
</details>
|
79 |
+
-->
|
80 |
|
81 |
+
<!--
|
82 |
+
### Downstream Usage (Sentence Transformers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
+
You can finetune this model on your own dataset.
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
<details><summary>Click to expand</summary>
|
87 |
|
88 |
+
</details>
|
89 |
+
-->
|
90 |
|
91 |
+
<!--
|
92 |
+
### Out-of-Scope Use
|
93 |
|
94 |
+
*List how the model may foreseeably be misused and address what users ought not to do with the model.*
|
95 |
+
-->
|
96 |
|
97 |
+
<!--
|
98 |
+
## Bias, Risks and Limitations
|
|
|
99 |
|
100 |
+
*What are the known or foreseeable issues stemming from this model? You could also flag here known failure cases or weaknesses of the model.*
|
101 |
+
-->
|
|
|
102 |
|
103 |
+
<!--
|
104 |
+
### Recommendations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
+
*What are recommendations with respect to the foreseeable issues? For example, filtering explicit content.*
|
107 |
+
-->
|
|
|
108 |
|
109 |
+
## Training Details
|
|
|
110 |
|
111 |
+
### Framework Versions
|
112 |
+
- Python: 3.9.20
|
113 |
+
- Sentence Transformers: 3.1.1
|
114 |
+
- Transformers: 4.45.2
|
115 |
+
- PyTorch: 2.4.1+cu121
|
116 |
+
- Accelerate: 1.0.0
|
117 |
+
- Datasets: 3.0.1
|
118 |
+
- Tokenizers: 0.20.1
|
119 |
+
|
120 |
+
## Citation
|
121 |
|
122 |
### BibTeX
|
123 |
+
|
124 |
+
<!--
|
125 |
+
## Glossary
|
126 |
+
|
127 |
+
*Clearly define terms in order to be accessible across audiences.*
|
128 |
+
-->
|
129 |
+
|
130 |
+
<!--
|
131 |
+
## Model Card Authors
|
132 |
+
|
133 |
+
*Lists the people who create the model card, providing recognition and accountability for the detailed work that goes into its construction.*
|
134 |
+
-->
|
135 |
+
|
136 |
+
<!--
|
137 |
+
## Model Card Contact
|
138 |
+
|
139 |
+
*Provides a way for people who have updates to the Model Card, suggestions, or questions, to contact the Model Card authors.*
|
140 |
+
-->
|
config_sentence_transformers.json
CHANGED
@@ -8,5 +8,5 @@
|
|
8 |
"query": "Instruct: Given a code search query, retrieve relevant code snippet that answer the query\nQuery: "
|
9 |
},
|
10 |
"default_prompt_name": null,
|
11 |
-
"similarity_fn_name":
|
12 |
}
|
|
|
8 |
"query": "Instruct: Given a code search query, retrieve relevant code snippet that answer the query\nQuery: "
|
9 |
},
|
10 |
"default_prompt_name": null,
|
11 |
+
"similarity_fn_name": null
|
12 |
}
|